Using plugins
Plugins can be server-side, client-side, or both. Server plugin — add to theplugins array in your auth config:
server.ts
auth-client.ts
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
onRequestandonResponseto affect all requests or responses. - Define custom rate limit rules.
- Register trusted origins.
Creating a server plugin
A server plugin is any object satisfying theBetterAuthPlugin 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 usingcreateAuthEndpoint from better-auth/api. Better Auth wraps the Better Call framework to create endpoints.
plugin.ts
ctx object provides access to Better Auth-specific context via ctx.context:
Endpoint rules:
- Use kebab-case for paths.
- Use
GETfor data retrieval,POSTfor 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
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
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). Usemiddlewares to target specific paths:
plugin.ts
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
UsegetActions to add additional methods to the client. The client uses Better Fetch for HTTP requests.
client-plugin.ts
Reactive atoms
UsegetAtoms 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 useGET and endpoints with a body use POST. Override this with pathMethods:
client-plugin.ts