Skip to main content
This page covers installing the package, satisfying peer dependencies, setting environment variables, and wiring the plugin into your server and database.

Install the package

Use your preferred package manager:

Peer dependencies

The server plugin requires these peers. They should already be present in a Better Auth project. If you use the React hooks, you also need:

Package entry points

The plugin exposes three entry points:

Environment variables

Add these to your environment. Use test mode keys while developing.
.env
  • RAZORPAY_KEY_ID: Your Razorpay API key id. Prefix with rzp_test_ for test mode and rzp_live_ for live mode.
  • RAZORPAY_KEY_SECRET: Your Razorpay API key secret. Required for payment verification and server-side operations.
  • RAZORPAY_WEBHOOK_SECRET: A secret you choose and configure in the Razorpay Dashboard. Used to verify webhook signatures.
You can find your keys in the Razorpay Dashboard under Account & Settings > API Keys. Toggle between test and live mode in the dashboard header. Test mode transactions use test cards and do not move real money.

Alternative: pass a Razorpay client instance

Instead of passing keyId and keySecret, you can provide your own Razorpay instance. This is useful if you already manage a Razorpay client elsewhere in your application.
auth.ts
If you provide neither razorpayClient nor both keyId and keySecret, the plugin throws an error at initialization.

Mount the handler

All plugin endpoints live under your Better Auth base path. Mount the handler for your framework.

Next.js App Router

app/api/auth/[...all]/route.ts

Express

server.ts
Use the equivalent adapter for other frameworks (for example, toSolidStartHandler, toSvelteKitHandler, or toHonoHandler from Better Auth).

Run schema migration

The plugin adds fields and tables to your database, such as user.razorpayCustomerId and a local subscription table when subscriptions are enabled. Run Better Auth’s migration command after adding the plugin.
If you prefer to inspect the generated SQL first, run:
Then apply the generated migration to your database before starting the application.