Software

What Is an API Key? A Guide With Real API Responses

An API key is a secret string that identifies the app calling an API. A dummy key in a request, scope and expiry errors from a test API, and safe storage.

An API key is a long, random secret string that an application sends with each API request so the service knows which project or account is calling. The service uses it to authorize the request, apply rate limits and bill usage. Treat it like a password: keep it on the server, never in front-end code or a public repository, give it the narrowest permissions possible, and rotate it if it leaks.

What a key looks like in a request

Most modern APIs expect the key in the Authorization header. This is a dummy key for a test server:

GET /api/reports HTTP/1.1
Host: api.example.test
Authorization: Bearer sk_test_valid_0001

Some APIs use a custom header such as X-API-Key instead. Avoid putting keys in the URL query string where possible: URLs end up in server logs, browser history and analytics.

Prefixes such as sk_ (secret) or pk_ (publishable), and test or live, are common conventions that make keys easier to recognize and to catch with secret scanners.

What we got back from a test API

We ran a small API on localhost that issues keys with an expiry date and scopes, then called /api/reports (which requires the reports:read scope) four ways. These are the actual responses:

$ curl -i http://127.0.0.1:9201/api/reports
HTTP/1.1 401 Unauthorized
www-authenticate: Bearer realm="api"
{"error":"missing_credentials"}

$ curl -i -H 'Authorization: Bearer sk_test_expired_0002' …
HTTP/1.1 401 Unauthorized
www-authenticate: Bearer realm="api", error="invalid_token", error_description="key expired"
{"error":"invalid_token","detail":"key expired"}

$ curl -i -H 'Authorization: Bearer sk_test_typo_000' …
HTTP/1.1 401 Unauthorized
www-authenticate: Bearer realm="api", error="invalid_token"
{"error":"invalid_token"}

$ curl -i -H 'Authorization: Bearer sk_test_valid_0001' …
HTTP/1.1 403 Forbidden
www-authenticate: Bearer realm="api", error="insufficient_scope", scope="reports:read"
{"error":"insufficient_scope","required":"reports:read"}

The pattern follows the HTTP and Bearer token standards: 401 means “I don’t know who you are” (missing, wrong or expired key); 403 means “I know who you are, but this key may not do that”. The error values come from RFC 6750.

DiagnoseMap an API error to the key problem

What status did the API return?

  • 401 Unauthorized →

    Is the Authorization header present in the request the server received?

    Log the outgoing request or use curl -v. Proxies and redirects can drop the header.

    • No →

      The key never arrived.

      Set the header in the client. If you follow a redirect (e.g. http→https), many clients strip Authorization on the new request; call the final URL directly.

    • Yes →

      Is the key exactly right and in the expected format?

      Check for 'Bearer ' prefix, stray spaces or quotes, and test vs live keys.

      • Yes →

        The key was revoked, rotated or expired.

        Check the provider's dashboard for the key's status. Create a new key, update the secret store, and redeploy.

      • Not sure →

        Fix the key or header format.

        Copy the key again without whitespace, send 'Authorization: Bearer KEY' (or the header the API documents), and make sure a test-mode key is used against the test environment.

  • 403 Forbidden →

    The key is valid but lacks permission.

    Add the required scope or role to the key (the WWW-Authenticate header or error body often names it), or use a key created for this purpose.

  • 429 Too Many Requests →

    The key is fine; you are calling too often.

    Respect Retry-After, add backoff, and cache results. Separate keys per environment keep a test script from consuming production limits.

API key vs token vs password

API keyOAuth access tokenPassword
Identifiesan application or projecta user’s grant to an applicationa person
Lifetimelong, until rotatedshort (minutes to hours)long
Typical scopeproject-wide, sometimes restrictedlimited to what the user approvedeverything the user can do
Where it livesserver-side secret storeissued at runtimethe user’s memory or manager

API keys are simple, which is why they are popular for server-to-server calls. When an app acts on behalf of a user, OAuth tokens are the better fit.

Keep keys safe

  • Never ship a secret key to the browser or a mobile app. Anything in client code can be extracted. Call the API from your server instead, or use a publishable key designed for the client.
  • Store keys in environment variables or a secret manager, not in source code. Add .env to .gitignore.
  • Restrict each key: scopes, allowed IP addresses or referrers, and separate keys per environment. Google Cloud, for example, lets you restrict a key to specific APIs and applications.
  • Rotate on a schedule and immediately after a leak. Create the new key, deploy it, then revoke the old one.
  • Watch usage. A sudden spike often means a leaked key.

Broken authentication is a recurring item in OWASP’s API Security Top 10. When a call fails with 401, the 401 Unauthorized guide walks through the checks; for event-driven integrations, see how webhooks are signed.

Sources

  1. RFC 6750: The OAuth 2.0 Authorization Framework: Bearer Token Usage — IETF. WWW-Authenticate error codes invalid_token, insufficient_scope
  2. RFC 9110: HTTP Semantics — IETF. 401 and 403
  3. OWASP API Security Top 10 (2023) — OWASP
  4. Manage API keys — Google Cloud

How we made this

Reproduced in a local lab. We ran a small Node.js API on localhost that accepts Bearer keys with scopes and expiry dates, then called it with curl using dummy keys: no key, an expired key, a mistyped key and a valid key without the needed scope. Responses are quoted verbatim. The keys are fake and were never valid anywhere else.

Performed by Claude, an AI assistant working in the BinConvert repository (macOS, Node.js 25.8.2, curl 8.7.1), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; the curl calls were re-run against the test server and the responses compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!