Skip to main content
KeyAuth authenticates requests using Unkey API keys. It is the only policy type executed by the sentinel engine.

Fields

key_space_ids
string[]
List of keyspace IDs the key must belong to. If the key belongs to a keyspace not in this list, authentication fails with Sentinel.Auth.InvalidKey.
locations
KeyLocation[]
Ordered list of locations to extract the API key from. Sentinel tries each location in order and uses the first non-empty key. If omitted, defaults to extracting a Bearer token from the Authorization header.
permission_query
string
Optional RBAC query evaluated against the key’s permissions. If the key does not satisfy the query, authentication fails with Sentinel.Auth.InsufficientPermissions.

Examples

{
  "policies": [
    {
      "id": "api-auth",
      "name": "Authenticate API keys",
      "enabled": true,
      "match": [],
      "keyauth": {
        "key_space_ids": ["ks_abc123"],
        "locations": [
          { "bearer": {} }
        ]
      }
    }
  ]
}

Key extraction

Sentinel supports three key extraction locations:
LocationBehavior
BearerReads the Authorization header and strips the Bearer prefix (case-insensitive)
HeaderReads a named header. Optionally strips a prefix (case-insensitive)
Query parameterReads a named query parameter. Security risk: query strings are recorded in server logs, proxy logs, CDN logs, browser history, and Referer headers. Use only as a last resort for trusted-internal traffic; prefer bearer or header locations.
When multiple locations are configured, sentinel tries each in order and uses the first non-empty result.

Verification flow

  1. Extract the key from the request using configured locations.
  2. Hash the key using SHA-256.
  3. Look up the hash in the key cache (fresh: 10s, stale: 10min, max: 100k entries).
  4. Validate key status. Keys that are not found, disabled, expired, or belong to a disabled workspace are rejected.
  5. Verify the key belongs to one of the configured key_space_ids.
  6. Parse the permission query (if configured) and build verify options.
  7. Call verifier.Verify() with 1 credit deduction per request.
  8. Write rate limit headers (regardless of success or failure).
  9. Check post-verification status (rate limit, usage exceeded, permissions).
  10. Build and return the principal on success.

Response headers

KeyAuth writes rate limit headers on every response, including rejected requests:
HeaderValue
X-RateLimit-LimitRate limit ceiling
X-RateLimit-RemainingRemaining requests in the window
X-RateLimit-ResetUnix timestamp when the window resets
Retry-AfterSeconds until retry (only on 429 responses, minimum 1 second)

Error responses

ScenarioStatusCode
No key found in request401Sentinel.Auth.MissingCredentials
Key not found in database401Sentinel.Auth.InvalidKey
Key disabled401Sentinel.Auth.InvalidKey
Key expired401Sentinel.Auth.InvalidKey
Key not in allowed keyspace401Sentinel.Auth.InvalidKey
Workspace disabled401Sentinel.Auth.InvalidKey
Permission query not satisfied403Sentinel.Auth.InsufficientPermissions
Rate limit exceeded429Sentinel.Auth.RateLimited
Usage limit exceeded429Sentinel.Auth.RateLimited
Invalid permission query syntax500Sentinel.Internal.InvalidConfiguration