Skip to main content

Overview

Complete guide to using the Unkey Python SDK for issuing keys, verifying requests, and rate limiting in your Python applications and APIs.
2 min read

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#

FieldTypeDescription
validboolWhether the key passed all checks
codestrStatus code (VALID, NOT_FOUND, RATE_LIMITED, etc.)
key_idstrThe key's unique identifier
namestr?Human-readable name of the key
metadict?Custom metadata associated with the key
expiresint?Unix timestamp (in milliseconds) when the key will expire (if set)
creditsint?Remaining uses (if usage limits set)
enabledboolWhether the key is enabled
roleslist[str]?Roles attached to the key
permissionslist[str]?Permissions attached to the key
identitydict?Identity info if external_id was set when creating the key
ratelimitslist[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