Skip to content

key-auth

The key-auth plugin validates API keys from incoming requests and associates them with registered consumers.

  • Phase: Request
  • Purpose: Authentication via API key
  • Behavior: Rejects requests without valid API key
{
"pluginName": "key-auth",
"config": {
"headerName": "X-API-Key",
"queryParam": "api_key",
"required": true
}
}
OptionTypeDefaultDescription
headerNamestringX-API-KeyHeader containing the API key
queryParamstringapi_keyQuery parameter alternative
requiredbooleantrueReject if key not found

The plugin checks for the API key in this order:

  1. Header: X-API-Key (or custom header name)
  2. Query parameter: api_key (or custom param name)
{
"success": false,
"error": {
"code": "AUTH_MISSING_KEY",
"message": "API key is required"
}
}
{
"success": false,
"error": {
"code": "AUTH_INVALID_KEY",
"message": "Invalid API key"
}
}

When authentication succeeds, the consumer is available in plugin context:

// In subsequent plugins
ctx.consumer; // The authenticated consumer
ctx.consumer.id;
ctx.consumer.username;
ctx.consumer.customId;