Skip to main content
This guide walks through creating a complete Better Auth plugin from scratch. We’ll build a birthday plugin that stores user birth dates and enforces an age requirement during sign-up.
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.
Both implement a simple interface (BetterAuthPlugin / BetterAuthClientPlugin) that Better Auth merges at startup.

Building the server plugin

1

Create the plugin file

Create a birthday-plugin/index.ts file:
birthday-plugin/index.ts
This is already a valid plugin — it just doesn’t do anything yet.
2

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
3

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
Return { context: ctx } at the end of a before hook to pass the (potentially modified) context to the next handler. Return nothing to short-circuit with an implicit 200.
4

Register the server plugin

Add the plugin to your auth config:
auth.ts

Building the client plugin

1

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
2

Register the client plugin

auth-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: See the plugins documentation for the complete API.

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.