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 withrzp_test_for test mode andrzp_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.
Alternative: pass a Razorpay client instance
Instead of passingkeyId 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
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
toSolidStartHandler, toSvelteKitHandler, or toHonoHandler from Better Auth).
Run schema migration
The plugin adds fields and tables to your database, such asuser.razorpayCustomerId and a local subscription table when subscriptions are enabled. Run Better Auth’s migration command after adding the plugin.