An API key is a long string of letters and numbers that identifies your software to another service, working like a password that a program types for you. You put it in a request header or a URL parameter, and the service uses it to decide what your code is allowed to do. A typical key looks like sk_live_4f9a2b7c8d1e6f3a — long, random, and never meant to be shown to anyone.
Here is a concrete example. A small weather app on your site needs today's forecast. The weather service gives you a key when you sign up. Your code sends the key in a header such as Authorization: Bearer YOUR_KEY with each request. The service checks the key, counts the request against your account, and returns the data. No key, no data.
Put the Key Where the Service Expects It
Most developers lose an afternoon to a 401 or 403 error because the key was correct but placed in the wrong spot. Every service documents three things: the key, where it goes, and what happens when it is missing. Read those three lines before you write any code.
- Copy the key exactly. Leading and trailing spaces are the most common copy-paste mistake.
- Decide the transport. Many services want a header like
Authorization: Bearer KEYor a custom header such asX-API-Key: KEY. Some older services accept a query parameter like?api_key=KEY. - Send one test request with a tool like curl or the service's own console, outside your app.
- Read the response. A 200 means the key and placement are right. A 401 usually means a bad or missing key. A 403 usually means a valid key without permission for that endpoint.
- Only then move the key into your code, and load it from a settings file or environment variable rather than typing it into the source.
Headers, Query Strings and Why the Difference Matters
A key in a header stays out of the visible URL. A key in a query string ends up in browser history, server logs, proxy logs and referrer headers. Prefer the header whenever the service offers it. If a service only supports the query parameter, treat that key as easier to leak and rotate it more often.
Keep the Key Out of Code That Others Can See
A key in a public repository is public. Automated scanners look for key-like strings in new commits, and a key exposed this way can be used within minutes. The rule is simple: code sees the key at runtime, but the key is not written in the code.
The usual pattern is an environment variable. Your app reads a value named something like WEATHER_API_KEY from the environment. On your machine, a local file such as .env holds the value. That file goes in .gitignore so it never reaches the repository. A committed file called .env.example lists the names with blank values, so a new teammate knows what to set.
Two more habits close the common gaps:
- Do not put a secret key in front-end code. Anything shipped to a browser can be read by anyone using developer tools. If a browser needs data, route the request through your own server, which holds the key.
- Do not paste keys into chat, tickets, screenshots or support emails. If you must share one for debugging, use the service's test-mode key, not the live one.
Leak, Detect, Rotate
Imagine a key that is allowed 1,000 requests per day in test mode. A developer commits it by accident at 9:00. At 9:10 a scanner finds it. By 9:30 the daily limit is used up and your own tests start failing with a rate-limit error. The fix has three parts, in this order: revoke the old key in the dashboard, create a new one, and replace the value in your settings. Then remove the string from the code and, if the commit is already pushed, treat the key as burned even after you edit the file. History keeps old versions.
Notice that rotation is the actual repair. Deleting the line is not enough, because the old key still works until you revoke it.
Give Each Key the Smallest Job It Can Do
Many services let you create more than one key, each with its own permissions, limits or environment. Use that. A key used by a scheduled report does not need permission to delete records. A key used on a staging server should be a separate test-mode key, not the one your customers' data flows through.
A practical layout for a small project:
- One test key for local development, with low daily limits.
- One key for staging, also test mode if the service offers it.
- One live key for production, stored only in the hosting provider's secret settings.
- One key per outside integration, so revoking one does not break the others.
Labels help more than they seem to. Name keys by purpose and date, such as reports-nightly, so the dashboard tells you what will break before you revoke something.
Plan for the Day a Key Stops Working
Keys expire, get revoked and hit rate limits. A calm system expects this. Know where the key lives, who can reach the dashboard, and how long a replacement takes. Write those three facts down somewhere your team can find them without asking.
Set a reminder before expiry if the service shows an end date. Watch for the error codes that mean a key problem rather than a code problem: 401 for authentication, 403 for permission, 429 for too many requests. When a 429 appears, back off and retry after a short wait instead of resending immediately, which only makes the limit worse.
If a key is compromised, act in this order: revoke, replace, redeploy, then check logs for use you did not make. Revoking first limits the damage while you work on the rest.
Why a Key Is Not a User Login
A common misconception is that an API key proves who a person is. It does not. A key identifies an application, and anyone holding it acts as that application. User sign-in uses separate mechanisms, such as tokens tied to a person and an expiry time. Keep the two ideas apart, and never treat a key as proof that a particular human made a request.
Decide Between a Key, a Token and OAuth
API keys suit server-to-server calls where one program talks to another and permissions are broad. Tokens and OAuth fit cases where a person grants access to their own data, where access must expire, or where permissions differ per user. If a service offers both, check its documentation for which fits your case rather than assuming the key is enough.
A key sent over plain HTTP can be read on the network. Always use HTTPS. If a service's documentation still shows an HTTP example, test against a current endpoint and ask the provider which address to use.
Where Should the Key Live in a Shared Project?
Put it in your hosting provider's secret or environment settings, then let the app read it from there. Teammates who need it for local work get their own test key from the dashboard, which keeps the production key limited to the systems that run production. When someone leaves, revoke their personal keys; the shared production key does not need to change.
What If a Key Appears in a Log?
Logs are copied, downloaded and shared, so a key in a log is close to a key in public. Turn off verbose logging that prints headers, and mask the key when you must log a request, showing only the last four characters. If a full key did reach a shared log, revoke it and make a new one, then check whether the log storage allows deletion.
Can One Key Serve Several Apps?
It can, but then a limit or a revocation hits everything at once, and usage reports blur together. Separate keys per app make it possible to see which app spends the requests and to shut one down without touching the rest.