unkey.py (version ). The examples use FastAPI, but the client calls are the same in Flask or Django. For a fuller FastAPI setup, see FastAPI authentication.
You need a root key with the permissions listed on this page. Create one in the dashboard under Settings > Root Keys, and pass it as
Authorization: Bearer <root key>. See Permission reference for every permission.Create a root key and a keyspace
In the dashboard, go to Settings > Root Keys and create a root key with Keep the
api.*.create_api, api.*.create_key, and api.*.verify_key. Put it in your server’s UNKEY_ROOT_KEY environment variable. Never send it to a browser or mobile app.Then create a keyspace, which holds your keys. Use Keyspaces (APIs) in the dashboard, or apis.createApi:create a keyspace
data.apiId from the response. You need it to create keys. See Root keys for more on root keys.Install the SDK
Unkey(root_key=...). It works as a context manager, and every method has an _async version for asyncio.Create a key for a user
Create a key from your backend when a user signs up or asks for one. Every field is described in Creating keys.
externalId links the key to the user, so verification tells you who’s calling. meta comes back on every verification. You get data.key only once: show it to the user and store only data.keyId.Verify the key in a dependency
The dependency reads the bearer token, calls For an async app, create the client with
keys.verify_key, and raises HTTPException with the right status when data.valid is false. If the call itself fails, it raises errors.UnkeyError, or an httpx error for a timeout or connection failure. Catch both and return 503, so callers can tell an outage from a rejected key.main.py
async with Unkey(...) and call await unkey.keys.verify_key_async(key=key).Handle the outcome codes
keys.verifyKey returns HTTP 200 for every outcome. Check data.valid, then data.code for the reason, and return the matching status from your API:Remaining credits are in
data.credits, and each checked rate limit is in data.ratelimits with remaining and reset. The call only fails with an HTTP error (which the SDKs throw) when the call itself is wrong, such as a bad root key or a malformed body. See Verifying keys for every field.