The unkey.py SDK provides full access to Unkey's API for managing keys, verifying requests, and rate limiting.
Installation#
Requirements: Python 3.9+
Quick Start#
Initialize the client#
Warning
Never expose your root key in client-side code or commit it to version control.
Verify an API Key#
Check if a user's API key is valid:
Verification response fields#
| Field | Type | Description |
|---|---|---|
valid | bool | Whether the key passed all checks |
code | str | Status code (VALID, NOT_FOUND, RATE_LIMITED, etc.) |
key_id | str | The key's unique identifier |
name | str? | Human-readable name of the key |
meta | dict? | Custom metadata associated with the key |
expires | int? | Unix timestamp (in milliseconds) when the key will expire (if set) |
credits | int? | Remaining uses (if usage limits set) |
enabled | bool | Whether the key is enabled |
roles | list[str]? | Roles attached to the key |
permissions | list[str]? | Permissions attached to the key |
identity | dict? | Identity info if external_id was set when creating the key |
ratelimits | list[dict]? | Rate limit states (if rate limiting configured) |
FastAPI example#
Flask example#
Create API Keys#
Issue new keys for your users:
Warning
The full API key is only returned once at creation. Unkey stores only a hash.
Update Keys#
Modify an existing key:
Delete Keys#
Permanently revoke a key:
Or disable temporarily:
Async Support#
All methods have async variants with _async suffix:
Async FastAPI#
Error Handling#
Rate Limiting#
Rate limiting is included in key verification, but you can also use the standalone rate limit API:
Full Reference#
GitHub
Complete auto-generated API reference