Skip to main content
@getruba/better-auth lets Better Auth identity become the customer key used across Ruba. Enable only the surfaces your application needs:

Install the identity bridge

Add Better Auth, the Ruba plugin, and the core SDK:
Terminal

Establish the shared customer identity

1

Create a server credential

Generate an Organization Access Token in Ruba settings and keep it in the server environment.
.env
2

Compose the server plugins

Create one Ruba SDK client, pass it to ruba(), and select the capability modules under use:
  • checkout starts buying flows.
  • portal exposes customer self-service and records.
  • usage sends events and reads meters.
  • webhooks receives signed state changes.
auth.ts

Root configuration

3

Expose typed client methods

Add rubaClient() to the browser-facing Better Auth client. Billing authority still remains in the server configuration.
auth-client.ts

Keep user and customer lifecycle aligned

Enable createCustomerOnSignUp to create a customer with the Better Auth user ID stored as externalId. That stable key removes the need for a separate mapping table. For deletion, enable Better Auth’s deleteUser flow and remove the external Ruba customer from afterDelete:
Customer Deletion Example

Start checkout from an authenticated session

Source code Add checkout under use. Its configuration separates catalog mapping from browser behavior:
1

Register checkout on the server

Place the configured checkout() module in the root Ruba plugin’s use array.
Checkout Plugin Example
2

Invoke it from the client

Call authClient.checkout() with product IDs or one configured slug. referenceId is copied into checkout, order, and subscription metadata.
BetterAuth Checkout with Ruba Example
With Better Auth Organizations, use the organization ID as referenceId to associate the resulting commerce records with that team rather than only the individual session.
BetterAuth Checkout with Ruba Organization Example

Connect authenticated usage to meters

Source code The usage module adds event ingestion and customer-meter reads. Register it in use after the application’s usage model exists.
Usage Plugin Example

Record authoritative activity

Billable events belong on the server route that performs the work. A browser can forge or skip its own usage claims.
With signup sync enabled, session.user.id is already the customer’s externalId. Reuse the configured SDK client inside the protected work route:
app/api/ai/video/route.ts (Next.js App Router)
Each event needs a name, the Better Auth user ID as externalCustomerId, and optional string, number, or boolean metadata used by filters and aggregations.

Non-authoritative browser signals

authClient.usage.ingestion(...) sends through the Better Auth server and attaches the signed-in user automatically:
auth-client.ts
Treat this endpoint like analytics input. The user controls its payload, so it must not determine a bill or protected quota.

Read the user’s meter position

authClient.usage.meters() lists Customer Meters for the active session. page starts at 1 and limit caps each page.
Customer Meters with Usage Plugin Example
The response combines customer identity, meter definition, consumed units, credited units, and the remaining balance between them.

Bring asynchronous changes into Better Auth

Source code The webhook module owns /api/auth/ruba/webhooks, verifies delivery, and routes events to your callbacks.
1

Register the generated endpoint

In Ruba webhook settings, create an endpoint ending in /api/auth/ruba/webhooks.
2

Store its secret

Save the issued value as RUBA_WEBHOOK_SECRET in the server environment.
.env
3

Bind callbacks

Add webhooks() under use, pass the secret, and implement the events your application consumes.
Webhooks Plugin Example

Choose callback granularity

Use onPayload as a catch-all or register callbacks around the domain your application owns:

Query commerce through the signed-in customer

Source code
Portal module
The portal module adds customer-scoped methods under authClient.customer.

Open self-service

authClient.customer.portal() redirects the user to Ruba’s portal for orders, subscriptions, and benefits.
Open Customer Portal Example

Fetch one access snapshot

authClient.customer.state() returns identity, direct active subscriptions, granted benefits, and active meters with their balances.
Retrieve Customer State Example
Use that snapshot for access decisions. It excludes subscriptions purchased by a parent organization; query subscriptions with the organization reference for that case. Understand Customer State →

List individual record types

Granted benefits

The benefits method returns grants belonging to the active customer.
List User Benefits Example

Orders

The orders method returns that customer’s purchases and renewal records.
List User Orders Example
When a purchase belongs to a Better Auth Organization, pass its ID as referenceId and use that reference to find the organization’s subscriptions.
List Organization Subscriptions Example

Subscriptions

The subscriptions method returns relationships tied directly to the active customer.
List User Subscriptions Example
Direct-customer listing does not include subscriptions purchased through a parent organization.