How RBAC works in Unkey
Unkey’s authorization system has three components:1
Permissions
Individual capabilities like
documents.read or users.delete. These represent atomic actions in your system.2
Roles
Collections of permissions. A role like
editor might include documents.read, documents.write, and documents.delete.3
Keys
API keys are assigned one or more roles. When a key is verified, you can check if it has the required permissions.
Keys inherit permissions from all assigned roles. A key with both
editor and viewer roles will have permissions from both.Setting up authorization
Define your permission model
Start by mapping out what actions your API supports. Use a hierarchical naming convention:Create permissions
Create permissions via the dashboard or API:- Dashboard
- API
- Navigate to Authorization → Permissions in your Unkey dashboard
- Click Create New Permission
- Enter a name (human-readable) and slug (your permission identifier)
- Add an optional description
- Click Create new permission
Create roles
Group permissions into logical roles:- Dashboard
- API
- Navigate to Authorization → Roles
- Click Create New Role
- Enter a unique role name (e.g.,
admin,editor,viewer) - Optionally add a description
- Select permissions to attach
- Click Create new role
Attach roles to keys
When creating or updating keys, assign one or more roles:Verifying permissions
During key verification, check if the key has required permissions. If not, verification fails withcode: INSUFFICIENT_PERMISSIONS.
Single permission check
Complex permission queries
Unkey supports logical operators for sophisticated checks:AND - Require multiple permissions
AND - Require multiple permissions
OR - Require any permission
OR - Require any permission
Complex expressions with parentheses
Complex expressions with parentheses
Wildcard permissions
Use wildcards to grant broad access without listing individual permissions:*— All permissions (admin-level access)documents.*— All document operationsapi.v1.*— All v1 API endpointstenant.*.read— Read access to all tenant resources
Advanced patterns
Context-aware authorization
Sometimes you need to check permissions after loading data from your database:Permission hierarchies
Design permissions to reflect resource hierarchies:API endpoint protection
Map HTTP routes to permissions:Real-world example: Domain management API
Let’s build a complete RBAC system for a domain management platform:1
Define permissions
2
Create roles
admin — Full access to everything
- All permissions
domain.dns.create_recorddomain.dns.read_recorddomain.dns.update_recorddomain.dns.delete_record
domain.read_domaindomain.dns.read_record
3
Assign roles to keys
4
Verify in your API
Best practices
Use specific permissions
Define
users.delete instead of admin. Granular permissions give you better audit trails and easier debugging.Check permissions server-side
Never rely on client-side checks. Always verify permissions in your API, even if the UI hides certain actions.
Group with roles
Instead of attaching 15 permissions to every key, create roles and attach those. Easier to manage and update.
Start broad, then narrow
Begin with a few roles (
admin, user), then split them as your needs grow. Premature optimization leads to complexity.Response structure
Successful verification with permissions:Next steps
Multi-tenant applications
Use identities to scope permissions per organization
Rate limit overrides
Adjust rate limits based on user roles