Overview
Key management endpoints are the core of the Unkey API, handling creation, verification, updates, permissions, and roles for API keys.Key Changes in v2:
- Response format: Direct responses →
{meta, data}
envelope - Owner ID:
ownerId
field removed, useexternalId
only - Credits:
remaining
+refill
→credits
object - Rate limits:
ratelimit
object →ratelimits
array - Permission queries: Object syntax → string syntax
Migration Impact:
- Existing in v1: Full key CRUD operations with permissions, roles, and rate limiting
- Enhanced in v2: Improved response format, simplified field structures, and string-based queries
- Maintained in v2: All core key management functionality with backward-compatible request formats
POST /v1/keys.createKey → POST /v2/keys.createKey
Key Changes:- Remove
ownerId
field, useexternalId
instead - Restructure
remaining
+refill
→credits
object - Change
ratelimit
object →ratelimits
array - Response format: Direct response →
{meta, data}
envelope
- Request Structure Changes
- Response Changes
- cURL Commands
Key Creation Request Diff
POST /v1/keys.verifyKey → POST /v2/keys.verifyKey
Key Changes:- CRITICAL: v2 requires root key authentication with
api.*.verify_key
permission - CRITICAL:
apiId
parameter is no longer accepted in v2 - Remove
authorization
wrapper for permissions - Use string-based permission queries instead of object syntax
- Change
remaining
→credits
for cost parameters - Add support for multiple named rate limits
- Response format: Direct response →
{meta, data}
envelope
Major Authentication Change in v2The biggest change in v2 is that key verification now requires authentication with a root key that has the
api.*.verify_key
permission. This enables fine-grained access control:- Wildcard permission:
api.*.verify_key
allows verifying keys from any API in your workspace - Specific API permission:
api.api_123.verify_key
allows verifying only keys from APIapi_123
- No apiId parameter: Unlike v1, you cannot specify which API’s keys to verify - this is controlled by the root key’s permissions
- Request Diff
- Response Diff
Key Verification Request Changes
v1 cURL
v2 cURL
- Request Changes
- cURL Examples
Permission Query Syntax
- Request Changes
- cURL Examples
Credits & Rate Limits Structure
Understanding v2 Root Key Permissions for Key Verification
The v2keys.verifyKey
endpoint introduces a powerful permission system that gives you granular control over which services can verify keys from which APIs.
Setting Up Root Key Permissions
When creating a root key for key verification, you need to grant it the appropriateapi.*.verify_key
permission:
- Wildcard Permission (All APIs)
- Specific API Permission
- Multiple Specific APIs
Root Key with Permission to Verify Any API Key
Migration from v1 apiId Parameter
In v1, you could specify which API’s keys to verify using theapiId
parameter:
v1: Explicit API Selection
v2: Permission-Based API Selection
- Better Security: Only authorized services can verify keys
- Granular Control: Workspace owners control which services can verify keys from which APIs
- Simpler Integration: No need to manage
apiId
parameters in your application code - Audit Trail: All key verifications are tied to specific root keys with known permissions
GET /v1/keys.getKey → POST /v2/keys.getKey
Key Changes:- HTTP method: GET → POST
- Request body format required instead of query parameters
- Response format: Direct response →
{meta, data}
envelope
- Method & Format Changes
- Response Changes
- Complete Examples
HTTP Method Change
POST /v1/keys.deleteKey → POST /v2/keys.deleteKey
Purpose: Permanently delete an API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Added
permanent
parameter for hard deletion - Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Delete Key Request
POST /v1/keys.updateKey → POST /v2/keys.updateKey
Purpose: Update an existing API key’s properties. Key Changes:- Same structural changes as
createKey
(credits, ratelimits, no ownerId) - Response format: Direct response →
{meta, data}
envelope - Support for partial updates
- Request Changes
- Response Changes
- cURL Examples
Update Key Request Diff
POST /v1/keys.updateRemaining → POST /v2/keys.updateCredits
Purpose: Update the credit/usage count for an API key. Key Changes:- Endpoint name change:
updateRemaining
→updateCredits
- Request changes:
op
→operation
- Response format: Direct response →
{meta, data}
envelope
- Request Changes
- Response Changes
- cURL Examples
Update Credits Request Diff
POST /v1/keys.whoami → POST /v2/keys.whoami
Purpose: Get information about the current API key being used. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Enhanced response with additional metadata, mirroring the
v2/keys.getKey
response. - Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Whoami Request
New: /v2/keys.rerollKey
Purpose: Generate a new API key while preserving the configuration from an existing key.With this endpoint, you no longer have to delete an old key and recreate it if a key is unrecoverable and you don’t know the plaintext key anymore. Important: Analytics and usage metrics are tracked at both the key level AND identity level.
If the original key has an identity, the new key will inherit it, allowing you to track usage across both individual keys and the overall identity.
The new key will NOT inherit the usage from the old keyId.
- Request
- Response
Permission Management Endpoints
POST /v1/keys.addPermissions → POST /v2/keys.addPermissions
Purpose: Add permissions to an existing API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Will auto create permissions if they don’t exist, and the root key has the
rbac.*.create_permission
permission. Otherwise it will return a permission error. - Only identify permission by their slugs.
- Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Add Permissions Request
POST /v1/keys.removePermissions → POST /v2/keys.removePermissions
Purpose: Remove permissions from an existing API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Only accepts permission slugs now
- Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Remove Permissions Request
POST /v1/keys.setPermissions → POST /v2/keys.setPermissions
Purpose: Atomically replace all permissions on an API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Atomic replacement of all permissions
- Will auto create permissions if they don’t exist, and the root key has the
rbac.*.create_permission
permission. Otherwise it will return a permission error. - Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Set Permissions Request
Role Management Endpoints
POST /v1/keys.addRoles → POST /v2/keys.addRoles
Purpose: Add roles to an existing API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - No option to auto-create roles if they don’t exist
- Responds with all the roles that are attached to the key
- Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Add Roles Request
POST /v1/keys.removeRoles → POST /v2/keys.removeRoles
Purpose: Remove roles from an existing API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Responds with the remaining roles on the key
- Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Remove Roles Request
POST /v1/keys.setRoles → POST /v2/keys.setRoles
Purpose: Atomically replace all roles on an API key. Key Changes:- Response format: Direct response →
{meta, data}
envelope - Atomic replacement of all roles
- Responds with all the roles that are now set
- No auto creation of roles anymore
- Added
meta.requestId
for debugging
- Request Changes
- Response Changes
- cURL Examples
Set Roles Request
Migration Patterns
Response Format Migration
- Response Parsing Migration
v1 vs v2: Response Handling
Key Structure Migration
- Key Structure Migration
v1 vs v2: Key Structure
Migration Checklist
Key Creation & Updates
- Replace
ownerId
withexternalId
- Update
remaining
+refill
→credits
structure - Convert
ratelimit
→ratelimits
array - Add
name
field to rate limits - Change
async
parameter toautoApply
- Add
refillDay
for monthly intervals
Key Verification
- CRITICAL: Create root key with
api.*.verify_key
permission for your verification service - Add root key authentication header to all key verification calls
- Remove
apiId
parameter from verification requests (controlled by root key permissions now) - Convert permission query objects to strings:
"perm1 AND perm2"
- Update
remaining
→credits
for cost parameters - Handle new rate limits array structure in responses
- Test verification with both wildcard (
api.*.verify_key
) and specific API permissions
Response Handling
- Change
response
(direct) toresponse.data
in all key operations - Extract and log
meta.requestId
from responses for debugging - Remove references to
ownerId
in response parsing - Update error handling for new response structure
Endpoint Updates
- Update
keys.updateRemaining
→keys.updateCredits
- Add
operation
parameter for credit updates (set/increment/decrement) - Add
permanent
parameter for key deletion if needed
Testing
- Test key creation with new structure
- Test key verification with string-based permission queries
- Test permission and role management operations
- Verify key updates work with new credit structure
- Confirm all responses follow new envelope format