Skip to main content
Role-Based Access Control (RBAC) lets you define what each API key is allowed to do. Instead of treating all keys equally, you can grant specific permissions based on roles, enabling fine-grained authorization for your API.

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:
For complex systems, use deeper hierarchies:

Create permissions

Create permissions via the dashboard or API:
  1. Navigate to Authorization → Permissions in your Unkey dashboard
  2. Click Create New Permission
  3. Enter a name (human-readable) and slug (your permission identifier)
  4. Add an optional description
  5. Click Create new permission

Create roles

Group permissions into logical roles:
  1. Navigate to Authorization → Roles
  2. Click Create New Role
  3. Enter a unique role name (e.g., admin, editor, viewer)
  4. Optionally add a description
  5. Select permissions to attach
  6. 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 with code: INSUFFICIENT_PERMISSIONS.

Single permission check

Complex permission queries

Unkey supports logical operators for sophisticated checks:
Key must have both permissions.
Key needs at least one of the specified permissions.
Key must be an admin OR have both delete and write permissions.

Wildcard permissions

Use wildcards to grant broad access without listing individual permissions:
Common patterns:
  • * — All permissions (admin-level access)
  • documents.* — All document operations
  • api.v1.* — All v1 API endpoints
  • tenant.*.read — Read access to all tenant resources
Use * sparingly. It’s better to create an admin role with explicit permissions than to use wildcard matching.

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
dns.manager — Can manage DNS records but not domains
  • domain.dns.create_record
  • domain.dns.read_record
  • domain.dns.update_record
  • domain.dns.delete_record
read-only — View-only access
  • domain.read_domain
  • domain.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.
Don’t commit files with sensitive permissions. Avoid hardcoding permission checks in your codebase. Instead, fetch the permission requirements dynamically or store them in configuration.

Response structure

Successful verification with permissions:
Failed permission check:

Next steps

Multi-tenant applications

Use identities to scope permissions per organization

Rate limit overrides

Adjust rate limits based on user roles