Better Auth Plugin
Add RevKeen billing, checkout, and customer portal to your Better Auth app
@revkeen/better-auth is a Better Auth plugin that adds RevKeen billing capabilities to your app. It auto-creates RevKeen customers on signup, generates checkout sessions, exposes a customer billing portal, and verifies webhook signatures.
Installation
npm install @revkeen/better-auth @revkeen/sdkServer setup
import { betterAuth } from "better-auth";
import { revkeen } from "@revkeen/better-auth";
import { RevKeenClient } from "@revkeen/sdk";
const rk = new RevKeenClient({ apiKey: process.env.REVKEEN_API_KEY! });
export const auth = betterAuth({
// ... your existing config
plugins: [
revkeen({
client: rk,
createCustomerOnSignUp: true,
checkout: {
successUrl: "/thank-you",
cancelUrl: "/pricing",
},
portal: true,
webhooks: {
secret: process.env.REVKEEN_WEBHOOK_SECRET!,
onEvent: {
"payment.succeeded": async (event) => {
console.log("Payment succeeded:", event.data.object);
},
"subscription.cancelled": async (event) => {
console.log("Subscription cancelled:", event.data.object);
},
},
},
}),
],
});Client setup
import { createAuthClient } from "better-auth/client";
import { revkeenClient } from "@revkeen/better-auth/client";
export const authClient = createAuthClient({
plugins: [revkeenClient()],
});Usage
Create a checkout session
const { url } = await authClient.revkeen.checkout({
priceId: "price_xxx",
});
// Redirect to checkout
window.location.href = url;Get customer billing portal data
const portal = await authClient.revkeen.portal();
console.log(portal.subscriptions); // Active subscriptions
console.log(portal.invoices); // Recent invoices
console.log(portal.paymentMethods); // Saved payment methodsHow it works
- On signup — the plugin creates a RevKeen customer via the SDK and stores the customer ID in user metadata
- Checkout —
POST /api/auth/revkeen/checkoutcreates a checkout session tied to the RevKeen customer - Portal —
GET /api/auth/revkeen/portalreturns the customer's subscriptions, invoices, and payment methods - Webhooks —
POST /api/auth/revkeen/webhooksverifies the signature and dispatches to your handlers
Configuration reference
| Option | Type | Default | Description |
|---|---|---|---|
client | RevKeenClient | required | Pre-configured SDK instance |
createCustomerOnSignUp | boolean | true | Auto-create RevKeen customer on signup |
checkout.successUrl | string | — | Default redirect after payment |
checkout.cancelUrl | string | — | Default redirect on cancel |
portal | boolean | false | Enable customer portal endpoint |
webhooks.secret | string | — | Webhook signing secret |
webhooks.onEvent | Record<string, handler> | — | Event handlers by type |