Skip to main content
The API Key plugin lets you create and manage API keys for your application. It supports rate limiting, custom expiration, permissions, metadata, organization-owned keys, and optional Redis-backed storage.

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

The raw key value is only returned at creation time. Store it securely — it cannot be retrieved again.
Creating a key server-side (for a specific user):

Verify an API key

Verification is always server-side. Pass the key from the incoming request:
By default, Better Auth looks for the API key in the 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
With this enabled, if a valid API key is found in the 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 to apiKey(). Each configuration must have a unique configId:
auth.ts
Specify configId when creating or verifying keys to use a specific configuration.

Schema

The API Key plugin creates an apikey table:
API keys are hashed before storage by default. Disabling hashing (disableKeyHashing: true) is strongly discouraged as it exposes raw keys if your database is breached.