validateApiKey validates an API key sent via Bearer Authentication in the Authorization header. This is useful for securing public API endpoints with WorkOS API keys.
Usage
Signature
Parameters
This function takes no parameters. It automatically reads theAuthorization header from the incoming request.
Returns
The validated API key object from WorkOS, or
null if no valid key is found.Examples
Basic API endpoint protection
Public API with rate limiting
Webhook endpoint validation
Logging API key usage
Multi-tenant API
Custom error messages
Scoped API access
How it works
- The function reads the
Authorizationheader from the incoming request - It extracts the Bearer token value using the pattern
Bearer <token> - It calls the WorkOS API to validate the token
- Returns the validated API key object or
nullif validation fails
Notes
- API keys must be sent in the
Authorizationheader with theBearerscheme - Returns
{ apiKey: null }if the header is missing, malformed, or contains an invalid key - This function is specifically for validating WorkOS API keys, not user access tokens
- API keys are typically used for server-to-server or public API authentication
- For user authentication in your app, use
withAuthinstead