Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.stellartools.dev/llms.txt

Use this file to discover all available pages before exploring further.

The @stellartools/betterauth-adapter adds a billing plugin to Better Auth. It links users to StellarTools customers and adds session-protected endpoints for subscriptions, refunds, and credits.

Install

npm install @stellartools/betterauth-adapter

Configure

import { stellarTools } from "@stellartools/betterauth-adapter";
import { betterAuth } from "better-auth";

export const auth = betterAuth({
  plugins: [
    stellarTools({
      api_key: process.env.STELLAR_TOOLS_API_KEY!,
      create_customer_on_signup: true,
      credit_low_threshold: 100,
      on_customer_created: async (customer) => {
        console.log("Customer created", customer);
      },
      on_subscription_created: async (subscription) => {
        console.log("Subscription created", subscription);
      },
      on_subscription_canceled: async (subscription) => {
        console.log("Subscription canceled", subscription);
      },
      on_credits_low: async (creditBalance) => {
        console.log("Credits low", creditBalance);
      },
    }),
  ],
});
The plugin adds stellartools_customer_id to the user schema. Run migrations after adding it so your database is up to date.

Plugin options

api_key
string
required
Your StellarTools API key.
create_customer_on_signup
boolean
Create a StellarTools customer when a user signs up. Defaults to false.
credit_low_threshold
number
Trigger on_credits_low when balance is at or below this value. Defaults to 10.
on_customer_created
function
Called when a customer is created or linked.
on_subscription_created
function
Called when a subscription is created.
on_subscription_canceled
function
Called when a subscription is canceled.
on_credits_low
function
Called when credits fall below the threshold after a consume.

Endpoints

All endpoints require a valid Better Auth session.

Customers

MethodPathDescription
POST/api/auth/stellar/customer/createCreate or link a StellarTools customer.
GET/api/auth/stellar/customer/retrieveGet the current user’s customer.
POST/api/auth/stellar/customer/updateUpdate name, email, phone, or metadata.

Subscriptions

MethodPathDescription
POST/api/auth/stellar/subscription/createCreate a subscription.
GET/api/auth/stellar/subscription/listList subscriptions for the current user.

Credits

MethodPathDescription
POST/api/auth/stellar/credits/consumeConsume credits for a product.
GET/api/auth/stellar/credits/transactionsGet credit transaction history.

Refunds

MethodPathDescription
POST/api/auth/stellar/refund/createCreate a refund for a payment.