An API key is a long-lived secret that identifies the calling application or project; OAuth 2.0 is a protocol that gives an application a short-lived, scoped access token, usually so it can act on behalf of a user who approved it. Use an API key for a server calling an API as itself, and use OAuth whenever an app needs access to someone else's account or data.
API key vs OAuth at a glance
| API key | OAuth 2.0 access token | |
|---|---|---|
| Identifies | The app, project or account that owns the key | The app, plus the user who granted access (or the app alone, with client credentials) |
| Lifetime | Until someone rotates or deletes it | Usually short; a refresh token gets a new one |
| Permissions | Whatever the key's owner configured | Scopes requested by the app and approved by the user |
| Revoke one app's access | Delete that key | Revoke that app's grant; the user's password is untouched |
| Setup effort | Copy a string into a header | Authorization server, redirects, token exchange, refresh |
| Typical use | Server-to-server calls, usage metering, public maps or search keys | "Connect your Google Drive", third-party apps, user-facing APIs |
What an API key actually proves
An API key proves that the request came from something holding that string. It says nothing about which human is involved. Google Cloud's documentation puts it plainly: a standard API key associates requests with a project for billing and quota, and "the API key doesn't identify a principal." The provider can count your requests, charge you and rate-limit you. It cannot tell whether a request came from your server or from someone who copied the key out of a public repository.
A typical call looks like this (the header name varies by provider):
curl https://api.example.com/v1/forecast?city=Vilnius \
-H "X-API-Key: $EXAMPLE_API_KEY"
That simplicity is the appeal. There is no flow to implement, and any HTTP client can do it. The weaknesses all follow from the key being a long-lived bearer secret:
- It usually doesn't expire, so a leaked key works until someone notices. If that happens, see what to do when an API key leaks.
- It often has every permission the account has, unless the provider supports per-key restrictions.
- It can't represent a user, so it can't express "this app may read Alex's calendar but not Sam's".
Google's documentation recommends two kinds of restriction on every key: API restrictions (which APIs the key can call) and application restrictions (which websites, IP addresses or apps can use it). It also warns that "Unrestricted API keys are insecure."
What OAuth 2.0 solves
RFC 6749 starts from the problem OAuth was built for. Without it, a third-party app that needs your data would ask for your password and store it. That gives the app full access to everything, makes the service keep accepting passwords from third parties, and leaves you only one way to cut off a single app: change your password and break all the others.
OAuth replaces the password with a token. It defines four roles: the resource owner (the user), the client (the app), the authorization server (which issues tokens) and the resource server (the API). The common flow, the authorization code grant, goes like this:
- The app sends you to the authorization server with the scopes it wants, for example read-only access to your calendar.
- You sign in there, not in the app, and approve the request.
- The authorization server redirects back to the app with a one-time code.
- The app's backend exchanges the code for an access token, and often a refresh token.
- The app calls the API with the access token.
The API call itself looks almost the same as with an API key. RFC 6750 defines the header:
Authorization: Bearer mF_9.B5f-4.1JqM
The difference is what stands behind the string. RFC 6749 describes an access token as "a string denoting a specific scope, lifetime, and other access attributes". It is limited to the approved scopes, it expires, and the user can revoke that one app's grant at any time. Refresh tokens let the app get new access tokens without asking you again, and RFC 6749 says they are never sent to resource servers.
Both are bearer credentials. RFC 6750 defines a bearer token as one that "any party in possession of the token" can use, so OAuth tokens need the same care as API keys: HTTPS only, never in URLs (RFC 6750 says bearer tokens "SHOULD NOT be passed in page URLs"), never in logs.
OAuth without a user: client credentials
If the caller is a program acting for itself, OAuth's client credentials grant fits. RFC 6749 describes it for clients accessing resources on their own behalf. The service exchanges its client ID and secret for an access token:
curl -X POST https://auth.example.com/oauth/token \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-d "grant_type=client_credentials&scope=reports.read"
The client secret is still a long-lived secret, like an API key. What you gain is that the secret goes only to the token endpoint, while the credential sent with every API request expires and carries explicit scopes.
Which OAuth flows not to use
RFC 9700 (Best Current Practice for OAuth 2.0 Security, January 2025) updates the original advice:
- The resource owner password credentials grant, where the app collects your password and trades it for a token, "MUST NOT be used".
- Clients "SHOULD NOT use the implicit grant", which returned tokens directly in the browser redirect.
- Use the authorization code grant with PKCE. PKCE is required for public clients (mobile apps, single-page apps) and recommended for confidential ones.
How to choose for your own API
- Internal service calling another internal service: an API key or client credentials. Prefer client credentials, or your platform's workload identity, if you can.
- Public API for developers who call it from their own servers: API keys, with per-key scopes, rotation and usage dashboards.
- Any app acting on another user's account: OAuth with the authorization code grant and PKCE. Never ask for the user's password.
- Your own website's login: neither. Use a session cookie; see JWT vs session cookies.
Whichever you pick, keep the secret out of source control and client-side code. If one has already been committed, removing it from Git history isn't enough on its own: rotate it as well.
Frequently asked questions
- Is OAuth more secure than an API key?
- For acting on a user's data, yes: OAuth tokens are scoped to what the user approved, expire, and can be revoked per app without changing the user's password. For a server calling an API as itself, a well-protected API key kept on the server is acceptable for many APIs. The weaker point of API keys is that they rarely expire, so a leaked key works until someone rotates it.
- Can I use an API key in frontend JavaScript?
- Only keys designed to be public, such as a browser maps key locked to your domain and to one API. Anything in client-side code can be read by anyone who opens the developer tools. Google's documentation recommends restricting every key by application and by API, and warns that unrestricted keys are insecure.
- Is OAuth the same as logging in with Google?
- Not exactly. OAuth 2.0 is about authorization: giving an app a token to access an API. Logging in to an app with an existing account is the job of OpenID Connect, which its specification describes as 'a simple identity layer on top of the OAuth 2.0 protocol'. It adds an ID token, a JWT with claims about who signed in. People often say OAuth when they mean that login flow.
- What is the OAuth client credentials grant?
- It is the OAuth flow for a program acting on its own behalf, with no user involved. The client sends its client ID and secret to the token endpoint and gets back a short-lived access token. It is the OAuth equivalent of an API key, with the benefit that the token sent with each API call expires.