> ## Documentation Index
> Fetch the complete documentation index at: https://better-auth-razorpay.amankushwaha.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Client setup

> Configure the Razorpay client plugin, call endpoints from your frontend, and handle errors.

This page shows how to set up the Razorpay client plugin, how endpoint paths map to client methods, and how to call endpoints from both the browser and the server.

## Add the client plugin

Import `razorpayClient` from the client entry point and pass it to `createAuthClient`. Set `subscription: true` when your server has subscriptions enabled so the client registers subscription methods.

```ts client.ts theme={null}
import { createAuthClient } from "better-auth/react"; // or "better-auth/client"
import { razorpayClient } from "@amank-root/better-auth-razorpay/client";

export const authClient = createAuthClient({
  plugins: [razorpayClient({ subscription: true })],
});
```

`razorpayClient` is also exported as `razorpayClientPlugin`. The plugin id is `"razorpay-client"`.

## How endpoints map to client methods

Better Auth converts endpoint paths to camelCase methods on `authClient`. Here are common examples:

| Endpoint | Client method |
| - | - |
| `POST /razorpay/create-order` | `authClient.razorpay.createOrder(...)` |
| `POST /razorpay/verify-payment` | `authClient.razorpay.verifyPayment(...)` |
| `POST /subscription/upgrade` | `authClient.subscription.upgrade(...)` |
| `POST /razorpay/payment/refund` | `authClient.razorpay.payment.refund(...)` |
| `POST /razorpay/customer/bank-account/delete` | `authClient.razorpay.customer.bankAccount.delete(...)` |
| `GET /razorpay/order` | `authClient.razorpay.getOrder(...)` |
| `GET /subscription/list` | `authClient.subscription.list(...)` |

## POST vs GET calls

POST calls pass the body object directly:

```ts theme={null}
const { data, error } = await authClient.razorpay.createOrder({
  amount: 50000,
  currency: "INR",
  receipt: "rcpt_001",
});
```

GET calls pass a `query` object:

```ts theme={null}
const { data, error } = await authClient.razorpay.getOrder({
  query: { id: "order_XXXX" },
});
```

## Response shape and error handling

Every client call returns `{ data, error }` following the Better Auth convention. On success, `data` contains the endpoint response. On failure, `error` includes `error.code` and `error.message`.

```ts theme={null}
const { data, error } = await authClient.razorpay.verifyPayment({
  razorpayOrderId: "order_XXXX",
  razorpayPaymentId: "pay_YYYY",
  razorpaySignature: "sig_ZZZZ",
});

if (error) {
  console.error(error.code); // e.g. INVALID_PAYMENT_SIGNATURE
  console.error(error.message);
  return;
}

console.log(data.valid); // boolean
```

The client entry point re-exports `RAZORPAY_ERROR_CODES` so you can compare against known codes:

```ts theme={null}
import { RAZORPAY_ERROR_CODES } from "@amank-root/better-auth-razorpay/client";

if (error?.code === RAZORPAY_ERROR_CODES.INVALID_PAYMENT_SIGNATURE) {
  // handle invalid signature
}
```

## Calling endpoints from the server

On the server, use `auth.api.<name>` with the request headers so Better Auth can resolve the session. Pass `body` for POST endpoints or `query` for GET endpoints.

```ts theme={null}
import { auth } from "@/auth";
import { headers } from "next/headers";

// POST endpoint
const result = await auth.api.createRazorpayOrder({
  body: {
    amount: 50000,
    currency: "INR",
  },
  headers: await headers(),
});

// GET endpoint
const order = await auth.api.getRazorpayOrder({
  query: { id: "order_XXXX" },
  headers: await headers(),
});
```

The server API names match the endpoint definitions. For example, `POST /razorpay/create-order` is `auth.api.createRazorpayOrder`, and `GET /razorpay/order` is `auth.api.getRazorpayOrder`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.