This guide assumes you have already set up Better Auth
in your project.
How plugins work
A Better Auth plugin is a pair of objects:- Server plugin — registered in
betterAuth({ plugins: [] }). Defines the schema, endpoints, and lifecycle hooks. - Client plugin — registered in
createAuthClient({ plugins: [] }). Mirrors the server types and can add client-side helper methods.
BetterAuthPlugin /
BetterAuthClientPlugin) that Better Auth merges at startup.
Building the server plugin
Create the plugin file
Create a This is already a valid plugin — it just doesn’t do anything yet.
birthday-plugin/index.ts file:birthday-plugin/index.ts
Define the schema
Extend the built-in
user model with a birthday field. Better Auth’s CLI
reads this schema when running generate or migrate.birthday-plugin/index.ts
Add a before hook
Use a
before hook to validate that the signing-up user is at least 5 years
old. Hooks receive the request context and can throw APIError to reject
the request.birthday-plugin/index.ts
Building the client plugin
Create the client file
Create
birthday-plugin/client.ts. The client plugin’s main job is to
import the server plugin’s return type so that client types stay in sync.birthday-plugin/client.ts
Update the database schema
After writing your plugin, generate or apply the schema changes:Plugin structure reference
A server plugin can implement any combination of these properties:| Property | Type | Purpose |
|---|---|---|
id | string | Unique plugin identifier |
schema | Schema | Database table extensions |
hooks.before | Hook[] | Middleware run before handlers |
hooks.after | Hook[] | Middleware run after handlers |
endpoints | Record<string, Endpoint> | New API endpoints |
middlewares | Middleware[] | Express-style middleware |
init | (ctx) => void | Called once at startup |
onRequest | (request, ctx) => void | Called on every request |
onResponse | (response, ctx) => void | Called on every response |
Next steps
- Read the plugins concepts page for advanced patterns like adding custom endpoints and custom client methods.
- Share your plugin on Discord or open a PR to share it with the community.