Installation
1
Install the package
2
Add the plugin to your auth config
auth.ts
3
Migrate the database
4
Add the client plugin
auth-client.ts
Usage
Create an API key
Verify an API key
Verification is always server-side. Pass the key from the incoming request:x-api-key request header.
Get an API key
List API keys
Update an API key
Delete an API key
Permissions
API keys support fine-grained, resource-based permissions.Set default permissions
auth.ts
Create a key with permissions
Verify permissions
Using API keys for authentication
Enable session creation from API keys to allow API key holders to authenticate as the associated user:auth.ts
x-api-key header, Better Auth will mock a user session for the request.
Configuration
auth.ts
Secondary storage (Redis)
For high-performance API key lookups, store keys in Redis instead of your primary database:auth.ts
Multiple configurations
You can run multiple API key configurations side-by-side (e.g., public and private keys) by passing an array directly toapiKey(). Each configuration must have a unique configId:
auth.ts
configId when creating or verifying keys to use a specific configuration.
Schema
The API Key plugin creates anapikey table: