API Key Authentication
What is API Key Authentication in HTTP?
Section titled “What is API Key Authentication in HTTP?”API Key Authentication in HTTP is a security scheme that involves sending a unique identifier (key) with each HTTP request to authenticate the client. Unlike the basic API Key scheme, the HTTP variant specifically defines where in the HTTP request the key should be placed: in the headers, query parameters, or cookies.
This approach provides a simple yet effective way to identify and authenticate API clients. The API key serves as both a unique identifier and a secret token for authentication and authorization in HTTP-based communications.
When to Use API Key Authentication in HTTP
Section titled “When to Use API Key Authentication in HTTP”API Key Authentication in HTTP is suitable for:
- RESTful APIs and HTTP-based services requiring simple authentication
- Microservices architectures where services communicate over HTTP
- Public APIs with rate limiting requirements
- Internal or private HTTP APIs with limited access
- Web applications that need to authenticate third-party services
- Scenarios where you need to track API usage by different clients
- Development and testing environments for HTTP services
When Not to Use API Key Authentication in HTTP
Section titled “When Not to Use API Key Authentication in HTTP”API Key Authentication in HTTP is not recommended for:
- High-security applications handling sensitive data
- Public-facing APIs that require strong user authentication
- Scenarios requiring user-specific permissions or roles
- Applications where you need to verify the identity of the end-user
- Production environments with strict security requirements
- Applications requiring OAuth flows or delegated authentication
- Scenarios where the API key might be exposed to client-side code
Pros and Cons
Section titled “Pros and Cons”- Simplicity: Easy to implement and use in HTTP-based services
- Flexibility: Can be placed in headers, query parameters, or cookies
- Low overhead: Minimal processing required for validation
- Stateless: No need to maintain session information
- Revocable: Can be easily revoked and replaced if compromised
- Rate limiting: Can be used to enforce usage limits per client
- Standard HTTP integration: Works with existing HTTP infrastructure
- Limited security: Less secure than OAuth 2.0 or other advanced authentication methods
- No expiration: Typically don’t expire automatically, requiring manual rotation
- No granular permissions: Usually provide all-or-nothing access
- Transmission risks: Can be intercepted if not sent over HTTPS
- Key management: Requires proper key management practices
- Query parameter exposure: Keys in query parameters may be logged in server logs
- Cookie limitations: Cookie-based keys face same-origin policy restrictions
Examples
Section titled “Examples”Here’s how to define an API Key HTTP security scheme in AsyncAPI with the key in the header:
{ "type": "httpApiKey", "in": "header", "name": "X-API-Key", "description": "API Key for HTTP authentication"}Another example with the API key in a query parameter:
{ "type": "httpApiKey", "in": "query", "name": "api_key", "description": "API Key passed as a query parameter"}And an example with the API key in a cookie:
{ "type": "httpApiKey", "in": "cookie", "name": "SessionKey", "description": "API Key stored in a cookie"}Implementation Example
Section titled “Implementation Example”When implementing API Key HTTP authentication in your application:
- Generate a secure, random API key for each client
- Store the API key securely (hashed in a database)
- Validate the API key with each HTTP request
- Implement rate limiting based on the API key
- Use HTTPS to protect the API key during transmission
- For header-based keys, use a custom header (e.g.,
X-API-Key) - For query parameter keys, consider the security implications of keys appearing in logs
- For cookie-based keys, set appropriate security attributes (HttpOnly, Secure, SameSite)
In an AsyncAPI document
Define the scheme (type: httpApiKey) under components.securitySchemes and reference it from every server that needs it. In AsyncAPI 3.0 a server's security lists the schemes themselves; in 2.x it lists requirement objects by name, with the scopes they need.
asyncapi: 3.0.0info: title: Orders version: 1.0.0servers: production: host: api.example.com protocol: wss security: - $ref: '#/components/securitySchemes/apiKey'components: securitySchemes: apiKey: type: httpApiKey name: api_key in: query description: API key in the query string of the WebSocket URLasyncapi: 2.6.0info: title: Orders version: 1.0.0servers: production: url: api.example.com protocol: wss security: - apiKey: []channels: {}components: securitySchemes: apiKey: type: httpApiKey name: api_key in: query description: API key in the query string of the WebSocket URLFields
| Field | Type | Description |
|---|---|---|
description | string | A short description for security scheme. CommonMark syntax MAY be used for rich text representation |
typerequired | stringhttpApiKey | The type of the security scheme |
inrequired | stringheaderquerycookie | The location of the API key |
namerequired | string | The name of the header, query or cookie parameter to be used |
Validate with JSON Schema
The HTTP API Key Authentication JSON Schema from the AsyncAPI JSON Schema registry: https://schemas.asyncapi.pavelon.dev/draft-07/schemas/v3/security/http/apiKeyHTTPSecuritySchema
Questions
Where can an HTTP API key go?
In a header, query parameter or cookie: set in to one of these and name to the parameter name, for example name: X-API-Key, in: header.