Validate Api Key
5-Line Core
identity = await api_key_provider.validate_api_key(api_key)
if identity:
print(f"Valid! Scopes: {identity.scopes}")
else:
print("Invalid key")Step-by-Step
1
# Typically from X-API-Key header
api_key = request.headers.get("X-API-Key")
if not api_key:
raise HTTPException(status_code=401, detail="Missing API key")2
identity = await api_key_provider.validate_api_key(api_key)3
if identity is None:
raise HTTPException(status_code=401, detail="Invalid API key")
print(f"Key ID: {identity.api_key_id}")
print(f"Tier: {identity.tier}")
print(f"Scopes: {identity.scopes}")4
if not identity.has_scope("api:write"):
raise HTTPException(status_code=403, detail="Insufficient scope")
# Proceed with operation5
Key ID: 550e8400-e29b-41d4-a716-446655440000
Tier: organization
Scopes: ['api:read', 'api:write']Complete Example
FastAPI Integration
Common Pitfalls
Pitfall
Solution
Last updated
Was this helpful?