RevKeen Docs

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/sdk

Server 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 methods

How it works

  1. On signup — the plugin creates a RevKeen customer via the SDK and stores the customer ID in user metadata
  2. CheckoutPOST /api/auth/revkeen/checkout creates a checkout session tied to the RevKeen customer
  3. PortalGET /api/auth/revkeen/portal returns the customer's subscriptions, invoices, and payment methods
  4. WebhooksPOST /api/auth/revkeen/webhooks verifies the signature and dispatches to your handlers

Configuration reference

OptionTypeDefaultDescription
clientRevKeenClientrequiredPre-configured SDK instance
createCustomerOnSignUpbooleantrueAuto-create RevKeen customer on signup
checkout.successUrlstringDefault redirect after payment
checkout.cancelUrlstringDefault redirect on cancel
portalbooleanfalseEnable customer portal endpoint
webhooks.secretstringWebhook signing secret
webhooks.onEventRecord<string, handler>Event handlers by type

Next Steps

On this page