What Is an API Key? How It Works and How to Protect It
Scrapeless Scraping API authenticates requests through the x-api-token header, where a Scrapeless account API key identifies the calling client.
TL;DR
- An API key is a credential issued to a client or project. The service uses it to identify the caller, apply access rules, measure usage, and enforce quotas.
- An API key is usually a secret. Anyone who obtains an unrestricted key may be able to call the API under the owner’s account.
- Keys should travel in headers over HTTPS. URLs leak into browser history, server logs, analytics systems, referrer data, and monitoring tools.
- A key needs a lifecycle. Issue, scope, store, observe, rotate, revoke, and audit credentials rather than treating them as permanent strings.
- API keys are not delegated user authorization. OAuth is usually a better fit when a third-party application needs limited access on behalf of a person.
What Is an API Key?
An API key is a value an API provider issues to a software client, project, account, or integration. The client includes the key with a request. The API gateway or application looks up the credential, checks its status and restrictions, applies policy, and associates the request with an owner for access, usage, and audit purposes.
The word “key” can be misleading. An API key is not necessarily a cryptographic key used to encrypt or sign data. In many systems it is an opaque random credential. Some designs use a public identifier plus a secret component; others encode a recognizable prefix that helps operators identify the credential type without revealing the secret.
API keys are common for server-to-server APIs, developer tools, data services, maps, messaging, automation, and internal services. Their appeal is operational simplicity: the client sends one credential, and the provider can attribute the call. That simplicity is safe only when the key is generated with enough entropy, transmitted securely, stored as a secret, and restricted to the smallest useful authority.
How API Key Authentication Works
- The provider issues a credential. A dashboard or administrative API creates a key for a named project, service account, or integration.
- The client stores the secret. Server applications read it from a secret manager, protected environment injection, or another approved credential store.
- The client sends a request. The key normally appears in an HTTP header over TLS.
- The service resolves the key. A key identifier or secure lookup finds the stored credential record without exposing unrelated secrets.
- The service evaluates policy. It checks active status, scopes, resource restrictions, network conditions, quotas, and other controls.
- The service records safe audit data. Logs use a key ID or fingerprint, not the full secret, to attribute the operation.
A request can be authenticated yet unauthorized. Authentication answers which client presented the credential. Authorization answers whether that client may perform this action on this resource. A service should make both decisions explicitly.
Where Should an API Key Be Sent?
An API-specific header or the standard Authorization header is usually the right location. Scrapeless Scraping API uses this header shape:
x-api-token: REDACTED
The request must use HTTPS so the transport protects headers from passive network observation and authenticates the server. TLS does not protect a key after it reaches application logs, debug traces, browser extensions, or compromised endpoints, so storage and observability controls remain necessary.
The OWASP REST Security Cheat Sheet advises against placing API keys and other security tokens in URLs because URLs are captured by many systems. Query-string authentication may exist for compatibility, but a new API should prefer headers.
What an API Key Can and Cannot Prove
A valid key proves that the caller possesses that credential at request time. It does not prove which human initiated the request, whether the device is trustworthy, or whether the software presenting the key is the intended software. A copied key can often be replayed from another machine unless the provider adds restrictions or sender-bound proof.
Keys embedded in public web pages, desktop binaries, browser extensions, and mobile applications cannot be kept as durable secrets because users control those environments. Obfuscation may slow casual discovery but does not change the trust model. Public clients should call a controlled backend or use an authorization design created for public software.
An API key also should not replace user-level permissions for sensitive resources. If every user action shares one project key, the service cannot reliably distinguish individual consent, roles, or revocation. User authentication and authorization belong in a separate layer.
API Key vs OAuth vs Bearer Token
| Concept | What It Describes | Typical Use |
|---|---|---|
| API key | A credential issued to a client, project, or integration | Service identification, metering, quotas, and application access |
| OAuth | An authorization framework for obtaining limited access tokens | Delegated access on behalf of a user or client access under defined grants |
| Bearer token | A possession-based way to use a token | Calling a protected resource through an Authorization header |
| JWT | A token format containing signed or protected claims | Self-contained identity or authorization assertions under a defined profile |
| Session cookie | A browser session credential managed through cookie rules | Maintaining a signed-in web session |
These categories overlap. An OAuth access token is commonly used as a bearer token. An API key might also be presented with a bearer scheme, though that presentation does not turn the system into OAuth. A JWT may be used as a bearer token, but opaque bearer tokens are also common.
API Key Design
A well-designed key is generated from a cryptographically secure random source and is long enough to resist guessing. The value should not encode sensitive account data. A recognizable prefix can help secret scanners and operators identify the provider and credential type, but the unpredictable portion must carry the security.
Many services show the full secret only once. The backend stores a one-way verifier or a protected secret representation rather than keeping every plaintext credential available for display. A separate non-secret key ID supports lookup, logs, and administration.
The system should allow several active keys per project so deployments can change credentials without one shared secret across every environment. Each key needs a name, owner, creation event, last-used information, restrictions, and revocation control.
How to Store API Keys
Production services should retrieve keys from a centralized secret-management system or an approved platform secret store. Access should be limited to the workload identity that needs the secret. Human access, exports, and administrative changes should be auditable.
The OWASP Secrets Management Cheat Sheet covers central storage, provisioning, auditing, rotation, and lifecycle controls. Environment variables can be a delivery mechanism, but they are not automatically private: process inspection, crash reports, build logs, or careless diagnostics may expose them.
Do not commit keys to source control, place them in container images, paste them into issue trackers, or share them through chat. The CWE-798 entry on hard-coded credentials explains why credentials embedded in software create broad exposure and are difficult to change.
Scoping and Restrictions
A key should grant only the APIs, operations, resources, and environments its workload needs. Separate development, staging, and production credentials. Use read-only authority when a process never writes. Limit quotas so a leak cannot create unbounded cost or traffic.
Network restrictions, allowed origins, application signatures, or service identities can add useful context, but each control has limitations. Source IP rules are difficult for mobile networks and shared egress. Browser origin restrictions protect only participating browser flows and do not make an exposed key secret. Treat restrictions as layers, not substitutes for credential protection.
Rotation and Revocation
Rotation replaces an old credential with a new one. A safe process creates a second key, updates the workload through the secret-delivery path, confirms traffic under the new key ID, and then revokes the old key. Multiple-key support prevents a forced simultaneous deployment across every instance.
Revoke a key immediately when exposure is suspected, an owner leaves, an integration is retired, or the credential no longer has a justified purpose. Incident handling should identify affected resources and actions through safe audit records, then examine how the value escaped so the delivery path can be corrected.
Expiration limits the life of forgotten credentials. Short validity is useful only if renewal is automated and observed. A system that quietly replaces expired keys with permanent emergency credentials defeats the control.
Monitoring Without Leaking Keys
Logs should record a non-secret key ID, project, decision, endpoint, response class, timestamp, and request correlation identifier. Never write the full key. Redaction should run before data leaves the application process and should cover headers, exception messages, traces, and support bundles.
Alert on unusual endpoints, regions, traffic volume, error patterns, and use from an environment that does not match the key’s purpose. Last-used timestamps help find dormant keys, but absence of observed use should be confirmed before revocation because monitoring coverage may be incomplete.
Common API Key Mistakes
- One key for every environment. A development leak can then affect production.
- Keys in URLs. Query strings spread through logs, analytics, browser history, and referrers.
- Keys in frontend code. Public clients cannot protect a shared long-lived secret.
- Permanent unrestricted credentials. Excess authority expands the impact of exposure.
- Full secrets in logs. Central observability systems become credential repositories.
- No owner or purpose. Nobody can decide whether an old key is still required.
- Silent automatic type conversion. Treating a key as a number can alter leading characters or precision; credentials are opaque strings.
When to Use an API Key
Server-to-Server Data Access
A controlled backend identifies its project to a data API and stores the key in a workload-accessible secret manager.
Developer Tools
A CLI uses a per-user or per-project credential stored outside source code and exposes a safe command for key replacement.
Usage Metering
An API associates requests with a plan and quota while keeping resource authorization as a separate policy decision.
Delegated User Access
An API key alone is usually the wrong choice; OAuth can represent user approval, scopes, and token revocation without sharing a password.
Conclusion
An API key is a straightforward credential for identifying a software client or project. The string itself is only one part of the system. Security comes from high-entropy generation, header-based TLS transport, narrow scope, protected storage, safe logging, observed use, planned rotation, and immediate revocation. API keys fit controlled application access; they should not be stretched into a user-delegation system or embedded where a public client can expose them.
Ready to Make an Authenticated Data Request?
Create a Scrapeless account, protect the issued API key, and use it from a controlled backend to access structured web data.
Sign up today and get $5 in free credit — no credit card required.
Claim Your $5 Credit →FAQ
Is an API key a password?
An API key is password-like because possession may grant access, but it normally identifies a software client or project rather than a human account login. It still requires secret handling.
Should an API key be sent in a URL?
No, headers over HTTPS are preferred because URLs are copied into many logs, histories, analytics tools, and referrer paths.
Can an API key be stored in browser JavaScript?
A long-lived secret API key should not be embedded in browser JavaScript because every user can inspect and copy it. Put the credential in a controlled backend or use a public-client authorization design.
Is an API key the same as a bearer token?
No, an API key is a credential category, while bearer describes a possession-based usage model. An API key can be presented through different header schemes, and OAuth access tokens are often bearer tokens.
How often should API keys be rotated?
Rotate keys according to risk, policy, and automated operational capability, and replace them immediately after suspected exposure. The service should support overlapping active keys so replacement does not require downtime.