NPM Package: @flowglad/server
Overview
The Server SDK is the core package for server-side Flowglad integration. It provides theFlowgladServer 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:
B2B apps: Pass
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 customerExternalIdB2B apps: Pass
organization.id or team.id as customerExternalId3. 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 theuseBilling hook on your frontend. It should handle requests at “/api/flowglad/…“
- Next.js App Router
- Next.js Pages Router
- Express
- Other Frameworks
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:
B2B apps: Pass
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 customerExternalIdB2B apps: Pass
organization.id or team.id as customerExternalIdCustomer 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.
cancelSubscription
Cancel an existing subscription.
uncancelSubscription
Reverse a scheduled subscription cancellation.
- 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_scheduledstatus will have their cancellation reversed - For paid subscriptions: throws error if no payment method exists
- Status reverts to
active(ortrialingif still in trial period) - The
cancelScheduledAtfield is cleared - Billing periods marked as
scheduled_to_cancelare restored - Any aborted billing runs are rescheduled
Checkout Methods
createCheckoutSession
Create a checkout session for payments.
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).
Common Patterns
Feature Gating Middleware
Usage Metering
Customer Portal Generation
Integration Examples
Basic Setup
Important:
B2B apps: Pass
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 customerExternalIdB2B apps: Pass
organization.id or team.id as customerExternalIdNext Steps
Next.js SDK
Use with Next.js applications