Skip to main content
Plugins are a core part of Better Auth. They let you extend base functionality with new authentication methods, features, and custom behaviors. Better Auth ships many built-in plugins. You can also create your own.

Using plugins

Plugins can be server-side, client-side, or both. Server plugin — add to the plugins array in your auth config:
server.ts
Client plugin — add when creating the auth client:
auth-client.ts
Keep your auth client and your server auth instance in separate files to avoid importing server-only code on the client.

What plugins can do

  • Create custom endpoints to handle any action.
  • Define database schemas (tables and columns).
  • Use middleware to intercept specific routes (API requests only).
  • Use hooks to intercept any endpoint call, including direct server calls.
  • Use onRequest and onResponse to affect all requests or responses.
  • Define custom rate limit rules.
  • Register trusted origins.

Creating a server plugin

A server plugin is any object satisfying the BetterAuthPlugin interface. The only required field is id.
plugin.ts
Wrap the plugin in a function so that options can be passed. This is consistent with all built-in plugins.

Endpoints

Add endpoints using createAuthEndpoint from better-auth/api. Better Auth wraps the Better Call framework to create endpoints.
plugin.ts
The ctx object provides access to Better Auth-specific context via ctx.context: Endpoint rules:
  • Use kebab-case for paths.
  • Use GET for data retrieval, POST for mutations.
  • Prefix paths with the plugin name (e.g., /my-plugin/hello-world).
  • Always use createAuthEndpoint — do not create raw endpoints.

Schema

Define a database schema for your plugin:
plugin.ts
Better Auth automatically adds an id field to each table. Field options: Schema options: Fields added to the user or session table are automatically inferred by TypeScript in endpoints that return those objects:
plugin.ts
Do not store sensitive information in the user or session tables. Create a separate table for sensitive data.

Hooks

Hooks run before or after an endpoint is executed, whether called via HTTP or directly on the server.
plugin.ts

Middleware

Middleware only runs on API requests from a client (not on direct server calls). Use middlewares to target specific paths:
plugin.ts
Throw an APIError or return a Response to stop the request and send an error to the client.

onRequest and onResponse

Use these to intercept all requests or responses:
plugin.ts

Rate limiting

Define custom rate limit rules for your plugin’s endpoints:
plugin.ts

Trusted origins

Register additional trusted origins and validate URLs in your endpoints:
plugin.ts

Helper functions

getSessionFromCtx

Retrieve the current client session inside a middleware or hook:
plugin.ts

sessionMiddleware

A built-in middleware that validates the session and attaches it to the context:
plugin.ts

Creating a client plugin

If your server plugin has endpoints that need to be called from the client, create a matching client plugin.
client-plugin.ts

Inferring server endpoints

The client can automatically infer endpoints from your server plugin. Kebab-case paths are converted to camelCase object keys — /my-plugin/hello-world becomes myPlugin.helloWorld.
client-plugin.ts

Custom client actions

Use getActions to add additional methods to the client. The client uses Better Fetch for HTTP requests.
client-plugin.ts

Reactive atoms

Use getAtoms to expose reactive state (like useSession) via nanostores. Atoms are resolved by each framework’s useStore hook.
client-plugin.ts

Path method overrides

By default, endpoints without a body use GET and endpoints with a body use POST. Override this with pathMethods:
client-plugin.ts