When verifying a key, you can check if it has specific permissions. If the key lacks the required permissions, verification fails with code: INSUFFICIENT_PERMISSIONS.
Basic permission check#
Pass a permission string to verify:
Permission query syntax#
Unkey supports logical operators for complex permission checks:
Single permission#
Key must have documents.read.
AND (all required)#
Key must have both permissions.
OR (any required)#
Key must have at least one of the permissions.
Complex queries with parentheses#
Key must have admin OR have both documents.read and documents.write.
Real-world example#
Response structure#
Successful verification with permissions:
Failed permission check:
Manual permission checking#
Sometimes you need to check permissions after loading data from your database (e.g., checking if the user owns a resource). In these cases:
Verify the key
Verify without permission requirements to get the key's permissions list.
Load your data
Query your database for the resource.
Check permissions manually
Use the permissions array and your data to make the decision.
Wildcard permissions#
Permissions support wildcards for broader access:
Common wildcard patterns:
*, All permissions (use carefully!)documents.*, All document permissionsapi.v1.*, All v1 API permissions
Best practices#
Use specific permissions, not broad ones
Instead of admin, define specific permissions like users.delete,
billing.manage. This gives you more control and better audit trails.
Check permissions at the API layer
Don't just check in the UI, always verify permissions server-side during API requests.
Use roles for common access patterns
Instead of attaching 10 permissions to every key, create a role and attach that. Easier to manage and update.