Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Protect Sports API Keys on an ESP32 Scoreboard

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to keep your sports API keys out of your ESP32 scoreboard's firmware and away from anyone who gets their hands on the device or its source code. You'll learn where the keys actually leak from, how to store them safely, and how to build a small proxy layer that keeps the secret off the ESP32 entirely. You'll need an ESP32 board, the Arduino IDE or PlatformIO, and a free or paid sports data API account that issues an API key.

What You'll Need

  • ESP32 development board (any variant works)
  • USB cable for programming
  • Arduino IDE or PlatformIO installed
  • A sports API account (e.g., API-Football, TheSportsDB, or similar)
  • A text editor for server-side code (if you choose the proxy method)
  • Basic familiarity with Arduino sketches and JSON parsing

Step-by-Step

  1. Understand where keys leak. The biggest mistake is hardcoding the key in your sketch as a string literal like const char* apiKey = "abc123";. Anyone who downloads your compiled firmware can extract that string with a hex editor or a tool like strings. Even if you obfuscate it, the key is still in the binary. So step one is accepting that the key cannot live in the firmware at all if you want real protection.
  2. Use a backend proxy (the recommended approach). Create a small server-side script (Node.js, Python Flask, PHP, or even a serverless function) that holds your API key. The ESP32 sends requests to your server, and your server forwards them to the sports API with the key attached. The ESP32 never sees the key. Example flow: ESP32 → your server → sports API → your server → ESP32.
  3. Write the proxy endpoint. On your server, create an endpoint like /api/scores that accepts a league or team parameter, calls the sports API with your key, and returns only the data the scoreboard needs. Cache responses for 30–60 seconds to avoid hitting rate limits.
  4. Point the ESP32 at your proxy. In your Arduino sketch, replace the sports API URL with your proxy URL. Use HTTPClient or WiFiClientSecure to make a GET request to https://your-server.com/api/scores?league=nba. Parse the JSON response with the ArduinoJson library.
  5. If you must keep the key on the device, use NVS (Non-Volatile Storage). This is the fallback when a proxy isn't possible. Store the key in ESP32's NVS partition using a provisioning sketch, then read it at runtime. This keeps the key out of the compiled binary, though it's still extractable from the flash chip if someone has physical access and the right tools.
  6. Write a provisioning sketch. Create a separate sketch that connects to Wi-Fi and writes your API key to NVS using Preferences.h. Run it once, then flash your main scoreboard sketch. The main sketch reads the key from NVS with preferences.getString("apiKey", "").
  7. Restrict the API key's permissions. Most sports APIs let you set allowed referrers, IP ranges, or usage quotas. If your proxy runs on a fixed IP, restrict the key to that IP. If the API supports it, set a daily quota that's just enough for your scoreboard's polling frequency.
  8. Add rate limiting and caching on the proxy. Your scoreboard probably polls every 30–60 seconds. Add a simple in-memory cache on the proxy so you don't hit the API on every request. This reduces the chance of your key being rate-limited or banned.
  9. Use HTTPS everywhere. Your proxy must be served over HTTPS (use Let's Encrypt or a managed host). The ESP32 should connect to your proxy over TLS. If you're using NVS, at least encrypt the traffic between the ESP32 and your proxy so the key isn't sent in plaintext.
  10. Rotate keys periodically. Even with a proxy, keys get compromised. Most sports APIs let you generate a new key and revoke the old one. Set a calendar reminder to rotate the key every 3–6 months, and update the proxy's environment variable or config file.
  11. Keep the key out of version control. If you use Git, add your proxy's config file (e.g., .env or config.json) to .gitignore. Never commit the key to a public or private repository.
  12. Consider a hardware secure element (advanced). For high-security projects, use an ATECC608A crypto chip over I2C. It stores the key in tamper-resistant hardware and can sign requests. This is overkill for most scoreboards but is the strongest option if the device is deployed in a hostile environment.

Common Mistakes

  • Hardcoding the key in the sketch — the single most common and most damaging mistake.
  • Putting the key in a .h file that gets committed to GitHub (even a private repo can be leaked).
  • Using HTTP instead of HTTPS, which lets anyone on the same Wi-Fi sniff the key in transit.
  • Not caching responses on the proxy, causing the API to throttle or ban your key.
  • Storing the key in NVS but leaving the provisioning sketch on the device, so anyone can re-flash it and read the key.
  • Sharing the key in a public forum or Discord when asking for help debugging.

Tips

  • Use environment variables on your server for the API key, not hardcoded values in the proxy code.
  • Add a simple authentication header (e.g., a shared secret) between the ESP32 and your proxy so random internet users can't abuse your proxy endpoint.
  • Log requests on the proxy for a few days to see how often the scoreboard polls and whether you're close to rate limits.
  • If your sports API offers a free tier with a lower rate limit, set your polling interval to 60 seconds or more to stay under it.
  • Use the ArduinoJson library's filter feature to only parse the fields you need, reducing memory usage on the ESP32.
  • Test your proxy with a browser first — if it returns the right JSON, then move to the ESP32.

Troubleshooting

The scoreboard shows no data. First, test your proxy URL directly in a browser. If it works, check the ESP32's serial output for HTTP status codes. A 401 or 403 means the proxy is rejecting the request — check your shared secret or IP allowlist. A 200 with empty JSON means the API returned no games — check your league/team parameter.

The ESP32 can't connect to the proxy. Verify the proxy URL is correct and that the server is reachable from the same Wi-Fi network. Try pinging the server from a computer on the same network. If the proxy uses a self-signed certificate, the ESP32 will reject it — use a proper certificate from Let's Encrypt or disable certificate verification only for testing (never in production).

The API key works in the browser but not from the ESP32. This usually means the API is checking the Referer or User-Agent header. Your proxy should set these headers to match what the API expects. Some APIs block requests that don't come from a browser-like user agent.

You see "Invalid API key" in the logs. Check that the key in your server's environment variable doesn't have a trailing newline or space. Copy it directly from the API dashboard and paste it into your config file, then restart the server.

The ESP32 runs out of memory when parsing JSON. Sports API responses can be large. Use ArduinoJson's deserializeJson(doc, response, DeserializationOption::Filter(filter)) to only keep the fields you need. Also increase the HTTP buffer size or use stream parsing instead of loading the whole response into memory.

You accidentally committed the key to Git. Immediately revoke the key in the API dashboard and generate a new one. Then remove the file from Git history using git filter-branch or BFG Repo-Cleaner — but revoking the key is the only real fix.

Frequently Asked Questions

Can I just obfuscate the key in the firmware? No. Obfuscation only slows down a determined attacker by minutes. The key is still in the binary and can be extracted with simple tools. Use a proxy or NVS instead.

Is the NVS method safe enough for a home scoreboard? For a device that stays in your living room, yes. The risk is someone physically stealing the board and dumping the flash. For a public display, use a proxy.

Does the proxy add noticeable latency? Minimal — usually 10–30ms per request. The API call itself takes longer. Caching on the proxy actually reduces perceived latency because the ESP32 gets a fast response from your server instead of the sometimes-slower sports API.

What if my sports API doesn't allow server-side calls? Some APIs are client-side only. In that case, you can't protect the key — the API provider has designed it that way. Consider switching to an API that supports server-side access.

Can I use a free hosting service for the proxy? Yes, services like Vercel, Netlify Functions, or Cloudflare Workers have free tiers that work well for this. Just be aware of cold-start latency and monthly request limits.

How often should the ESP32 poll the API? For live scores, every 30–60 seconds is enough. More frequent polling wastes battery and risks rate limits. Your proxy's cache can smooth out the polling interval.