To store API keys securely, keep them out of everything that gets copied: source code, repositories, firmware images, URLs and logs. Read each key at runtime from a secret store or a file only the service can read, give it the smallest set of permissions, and be ready to revoke and replace it.
A key leaks through its copies, so each rule below is about where a copy ends up.
| Where the key sits | How it gets copied | Rule |
|---|---|---|
| A URL query string | Browser history, server and proxy logs | Send it in a header, over TLS |
| Source code and repositories | Every clone, and the history | Read it at runtime; revoke it if it was committed |
| Environment variables | Other processes, logs, system dumps | Prefer a secret store or a protected file |
| Firmware images | Every device flashed with the image | One credential per device, no outside API keys |
Is it safe to put an API key in a URL?
No. The OAuth bearer token standard, RFC 6750, says bearer tokens should not be passed in page URLs, because browsers, web servers and other software may not adequately secure URLs in browser history, server logs and other data structures.
An API key in a query string is exposed in the same places, so I apply the same rule: put the key in a request header and send it only over TLS, which RFC 6750 requires for bearer tokens. If a service accepts its key only as a query parameter, I treat that key as half-exposed and give it the narrowest permissions on offer.
Should API keys be in environment variables?
They are better than source code, but they are a fallback. The OWASP Secrets Management Cheat Sheet says environment variables are generally accessible to all processes and may be included in logs or system dumps, and recommends them only when other methods are not possible.
In integration code, I read the key at start-up from a secret store or a file only the service account can read, and never log the loaded configuration. A development .env file stays out of the repository. Once committed, a key is in every clone and in the history.
How do you keep credentials out of firmware?
Do not compile them in. The consumer IoT baseline ETSI EN 303 645 says hard-coded critical security parameters in device software source code shall not be used, because reverse engineering can easily find credentials, including API keys, and simple obfuscation can be trivially broken.
Industrial devices are outside that standard’s scope, but the argument holds on a plant floor. Give each device its own credential at commissioning, keep it in protected storage, and ship one firmware image that carries no secret, so a credential taken from one device opens only that device (more on why shared passwords stop scaling). A sensor should not hold a key to an outside API at all: it sends data to your platform, and the server makes the outside call (see how another system should hear about new device data).
Hermetry connects field devices to live dashboards, rules and alerts: IoT telemetry and SCADA in one platform. In its automation flows, a block that calls an outside system never holds the address or password itself; it names a connection, and the HTTP request block takes scheme, host, port, authentication and TLS only from that connection, never from the message. Secrets in connections are stored encrypted, cannot be read back after saving, and never reach messages, run records, logs, the audit log or exports, so an imported flow needs its secrets entered again.
Scope every key to one integration and one job (see least privilege for people, programs and devices). One key per integration tells you what a leak exposes and lets you revoke it without stopping everything else.
Frequently asked questions
What should I do if an API key was committed to a repository?
Revoke it first. The OWASP Secrets Management Cheat Sheet gives the containment order as revocation of the exposed key, then rotation to a new secret, then removing the revoked secret from code and logs. Rewriting history alone does not help, because clones and forks may already hold the old commit.
How often should API keys be rotated?
OWASP says to rotate secrets regularly so that a stolen credential works only for a short time, and to automate rotation where possible, because manual rotation is challenging and can lead to mistakes. Pick an interval your integration can absorb without downtime, and rotate at once whenever a key may have leaked.
Is encrypting a key inside firmware enough?
Not if the device can decrypt it on its own. ETSI EN 303 645 notes that simple obfuscation methods used to obscure or encrypt hard-coded information can be trivially broken. If the device can recover the key without outside help, so can anyone who extracts its firmware.

