1
Install the package
Install the plugin with your preferred package manager. You also need
better-auth and its peer dependencies.2
Add environment variables
Add your Razorpay keys to your environment. Use test mode keys while developing.You can find these in the Razorpay Dashboard under Account & Settings > API Keys.
.env
3
Configure the server plugin
Import Mount the Better Auth handler in your framework. For example, in Next.js App Router:Or in Express:
razorpay from the server package and add it to your Better Auth config. The plugin registers all endpoints under your auth base path, such as /api/auth/*.auth.ts
app/api/auth/[...all]/route.ts
server.ts
4
Run schema migration
The plugin adds fields and tables to your database. Run Better Auth’s migration command to apply them.If you prefer to generate a migration file first, run:
5
Configure the client plugin
Create your auth client with the Razorpay client plugin. Set
subscription: true when your server has subscriptions enabled.client.ts
6
Create an order and open Checkout
In your frontend, create a Razorpay order through the auth client, then open Razorpay Checkout with the returned order id.
checkout.tsx
7
Verify the payment on the server
The
verifyPayment client call already hits your server endpoint, which checks the signature and payment status. For extra safety, also listen to the order.paid webhook before fulfilling the order. See the next step.8
Set up a webhook
Configure a webhook in the Razorpay Dashboard pointing to your plugin’s webhook endpoint, for example
https://your-domain.com/api/auth/razorpay/webhook. Provide the same secret you set in RAZORPAY_WEBHOOK_SECRET and select the events you need, such as order.paid.On the server, handle the event with the plugin’s built-in webhook support. Use the onOrderPaid callback to fulfill orders reliably.auth.ts
The exact webhook path depends on your auth base path and the plugin’s route registration. If requests return 404, confirm the path in your server setup (for example, by listing the plugin endpoints).
Next steps
One-time payments
Learn about manual capture, partial payments, and refunds.
Subscriptions
Set up recurring billing, trials, and seat-based plans.
Webhooks
See all supported events and how to handle them.
React hooks
Use TanStack Query hooks for subscriptions and payments.