Skip to main content
NPM Package: @flowglad/server

Overview

The Server SDK is the core package for server-side Flowglad integration. It provides the FlowgladServer class that handles all billing operations, customer management, and API communication with Flowglad.

Installation

Installation

Quick Start

1. Set Up Environment Variables

.env

2. Create a FlowgladServer Factory Function

Important: customerExternalId is the ID from your app’s database (e.g., user.id or organization.id), not Flowglad’s customer ID.B2C apps: Pass user.id as customerExternalId
B2B apps: Pass organization.id or team.id as customerExternalId

3. Mount Flowglad Route Handler

Create a route handler to handle Flowglad API requests from your frontend. The Server SDK provides route handler constructors that enable your client to communicate with your server, allowing you to load billing and feature access data via the useBilling hook on your frontend. It should handle requests at “/api/flowglad/…“
app/api/flowglad/[...path]/route.ts

4. Call Server Methods

Key Features

  • Customer Management: Find or create customers automatically
  • Subscriptions: Create, update, and cancel subscriptions
  • Checkout Sessions: Generate checkout URLs for payments
  • Feature Access: Check customer access to features
  • Usage Tracking: Record usage events and track usage credit grants in real time
  • Invoices: Retrieve and manage invoices
  • Payment Methods: Handle customer payment methods
  • Pricing Model: Access your pricing model with products, prices, and features

API Reference

Constructor

Options

See types.ts for more details.
Important: customerExternalId is the ID from your app’s database (e.g., user.id or organization.id), not Flowglad’s customer ID.B2C apps: Pass user.id as customerExternalId
B2B apps: Pass organization.id or team.id as customerExternalId

Customer Methods

findOrCreateCustomer()

Find an existing Flowglad customer associated with the current requesting customer (per your app’s authentication) or create a new one if none is found.

Billing Methods

getBilling()

Get comprehensive billing information for the customer, including helper functions like checkFeatureAccess, checkUsageBalance, getProduct, and getPrice.

getPricingModel()

Get the pricing model with products, prices, and features.

Subscription Methods

createSubscription

Create a new subscription for the customer.
Parameters:

cancelSubscription

Cancel an existing subscription.
Parameters:

uncancelSubscription

Reverse a scheduled subscription cancellation.
Parameters:
Behavior:
  • If the subscription is not scheduled to cancel, the method will silently succeed without uncanceling (idempotent)
  • If the subscription is in a terminal state (e.g., canceled), the method will silently succeed without uncanceling
  • Only subscriptions in cancellation_scheduled status will have their cancellation reversed
  • For paid subscriptions: throws error if no payment method exists
What happens:
  • Status reverts to active (or trialing if still in trial period)
  • The cancelScheduledAt field is cleared
  • Billing periods marked as scheduled_to_cancel are restored
  • Any aborted billing runs are rescheduled

Checkout Methods

createCheckoutSession

Create a checkout session for payments.
Parameters:

createAddPaymentMethodCheckoutSession

Create a checkout session that collects a payment method for the current customer.

createActivateSubscriptionCheckoutSession

Create a checkout session that activates a provisioning or imported subscription.

Customer Updates

updateCustomer

Update the stored customer record in Flowglad.

Feature Access Methods

checkFeatureAccess

Check if the customer has access to a specific feature.

Usage Tracking Methods

createUsageEvent

Record a usage event for metered billing. You can create usage events with either a price (for tracking usage with billing) or a usage meter (for tracking usage without billing).
Parameters:

Common Patterns

Feature Gating Middleware

Usage Metering

Customer Portal Generation

Integration Examples

Basic Setup

Important: customerExternalId is the ID from your app’s database (e.g., user.id or organization.id), not Flowglad’s customer ID.B2C apps: Pass user.id as customerExternalId
B2B apps: Pass organization.id or team.id as customerExternalId

Next Steps

Next.js SDK

Use with Next.js applications