Unkey provides two TypeScript packages:
- @unkey/api: Manage keys, APIs, and rate limits (server-side, requires root key)
- @unkey/ratelimit: Standalone rate limiting (server-side, requires root key)
For framework-specific wrappers, see @unkey/nextjs and @unkey/hono.
Installation#
Quick Start#
Initialize the client#
Never expose your root key in client-side code. These SDKs are for server-side use only.
Verify an API Key#
The most common operation, check if a user's API key is valid:
Verification response#
| Field | Type | Description |
|---|---|---|
valid | boolean | Whether the key passed all checks |
code | string | Status code (VALID, NOT_FOUND, RATE_LIMITED, etc.) |
keyId | string | The key's unique identifier |
name | string? | Human-readable name of the key |
meta | object? | Custom metadata associated with the key |
expires | number? | Unix timestamp (in milliseconds) when the key will expire (if set) |
credits | number? | Remaining uses (if usage limits set) |
enabled | boolean | Whether the key is enabled |
roles | string[]? | Roles attached to the key |
permissions | string[]? | Permissions attached to the key |
identity | object? | Identity info if externalId was set when creating the key |
ratelimits | object[]? | Rate limit states (if rate limiting configured) |
Check permissions during verification#
Create API Keys#
Issue new keys for your users:
The full API key is only returned once at creation. Unkey stores only a hash. Make sure to display it to your user immediately.
Update Keys#
Modify an existing key's configuration:
Delete Keys#
Permanently revoke a key:
Or disable temporarily (can re-enable later):
Rate Limiting#
Use @unkey/ratelimit for standalone rate limiting:
Cost-based rate limiting#
Expensive operations can consume more of the limit:
Error Handling#
Use try/catch to handle errors:
Resilient verification#
For production, handle edge cases:
TypeScript Types#
The SDK is fully typed. Import types as needed:
Framework Guides#
Full Reference#
Complete auto-generated API reference