Skip to main content

What is the Node SDK?

The @billingos/node package runs on your server. Use it to:
  • Create session tokens — authenticate your users with BillingOS
  • Check entitlements — verify feature access in API routes
  • Track usage — record metered usage from your backend
  • Manage customers — create and update customer records
  • Manage subscriptions — create, cancel, and modify subscriptions

Installation

npm install @billingos/node

Initialize

import { BillingOS } from "@billingos/node";

const billing = new BillingOS({
  secretKey: process.env.BILLINGOS_SECRET_KEY!,
});

Configuration

OptionTypeDefaultDescription
secretKeystringRequiredYour BillingOS secret key
apiUrlstringAuto-detectedAPI base URL override
timeoutnumber30000Request timeout in ms
maxRetriesnumber3Max retry attempts for failed requests

Auto-environment detection

The SDK automatically detects your environment from the key prefix:
Key prefixEnvironmentAPI URL
sk_test_*Sandboxhttps://sandbox-api.billingos.dev
sk_live_*Productionhttps://api.billingos.dev
You can override this with the apiUrl option or the BILLINGOS_API_URL environment variable.

Quick examples

Create a session token

const { sessionToken, expiresAt } = await billing.createSessionToken({
  externalUserId: "user_123",
  expiresIn: 3600,
});

Check a feature entitlement

const result = await billing.checkEntitlement("user_123", "api_calls");

if (result.has_access) {
  // Allow the operation
} else {
  // Return 403
}

Track usage

await billing.trackUsage({
  customerId: "user_123",
  featureKey: "api_calls",
  quantity: 1,
});

Error handling

import { BillingOS, ValidationError, NotFoundError } from "@billingos/node";

try {
  await billing.createSessionToken({ externalUserId: "" });
} catch (error) {
  if (error instanceof ValidationError) {
    console.error("Invalid input:", error.message);
  } else if (error instanceof NotFoundError) {
    console.error("Resource not found:", error.message);
  }
}
Error classStatusDescription
ValidationError400Input validation failed
AuthenticationError401Invalid API key
AuthorizationError403Insufficient permissions
NotFoundError404Resource not found
RateLimitError429Rate limit exceeded
ServerError500+Server-side error
NetworkErrorConnection/timeout error