Skip to content

API Key Authentication

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
  • 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

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"
}

When implementing API Key HTTP authentication in your application:

  1. Generate a secure, random API key for each client
  2. Store the API key securely (hashed in a database)
  3. Validate the API key with each HTTP request
  4. Implement rate limiting based on the API key
  5. Use HTTPS to protect the API key during transmission
  6. For header-based keys, use a custom header (e.g., X-API-Key)
  7. For query parameter keys, consider the security implications of keys appearing in logs
  8. 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.yaml
asyncapi: 3.0.0
info:
title: Orders
version: 1.0.0
servers:
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 URL

Fields

FieldTypeDescription
descriptionstringA short description for security scheme. CommonMark syntax MAY be used for rich text representation
typerequiredstring
httpApiKey
The type of the security scheme
inrequiredstring
headerquerycookie
The location of the API key
namerequiredstringThe 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.