In this guide
What is an API key?
An API key is a long string of random characters that uniquely identifies your application to an API. Think of it as a password for your code — instead of a human logging in with a username and password, your application presents this key with every request it makes.
API keys serve three purposes: identification (who is making this request?), authorisation (are they allowed to do this?), and rate limiting (have they exceeded their quota?). They're the most common way to authenticate with public APIs.
How API keys work
When you sign up for an API, the provider generates a unique key for you. You include this key in every HTTP request you send. The server checks the key against its database before processing your request.
API keys are sent in one of two places:
In an HTTP header (most common)
GET https://api.example.com/data
X-API-Key: sk_live_a1b2c3d4e5f6g7h8i9j0
As a query parameter
GET https://api.example.com/data?api_key=a1b2c3d4e5f6g7h8i9j0
Header-based keys are generally preferred because the URL (including query parameters) can appear in browser history, server logs, and referrer headers. Headers are more private.
API key vs OAuth vs Bearer token
Not all APIs use simple API keys. Here are the four main authentication patterns you'll encounter:
Authorization: Bearer <token>. Expires and must be refreshed. Best for: microservices, B2B integrations.In the Find an API catalog, every API listing shows its authentication type so you know what to expect before you start integrating.
Where to store your API key
The right way to handle API keys depends on where your code runs:
.env file locally, secrets manager in production). Never hardcode. Access as process.env.MY_API_KEY in Node.js or os.environ.get() in Python..env.example file instead that lists the variable names without the values, so collaborators know what they need to provide.
How to get an API key
The process is similar for almost every API:
-
1Sign up for an account on the API provider's website. Most free-tier APIs just need an email address.
-
2Navigate to the developer dashboard or settings. Look for sections labelled "API", "Developer", "Keys", or "Credentials".
-
3Create a new key or application. Some providers let you create multiple keys — useful for separating development and production environments.
-
4Copy the key immediately. Many APIs show the full key only once. If you miss it, you'll need to revoke and regenerate.
-
5Store it in your environment variables — never in your code. Test with a quick API call to confirm the key works.
What to do if your key is compromised
If you accidentally expose your API key — pushed it to GitHub, pasted it in a chat, logged it somewhere public — act immediately:
-
1Revoke the key immediately in the provider's dashboard. An exposed key should be treated as already compromised, even if you caught it quickly.
-
2Generate a new key and update it in all your environments (development, staging, production).
-
3Check your usage logs for any unusual activity between when the key was exposed and when you revoked it.
-
4If it was in a Git commit, note that deleting the file isn't enough — the key will still be in the commit history. Use
git filter-branchor BFG Repo Cleaner to scrub it, or treat the history as permanently exposed.