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.
import { FlowgladServer } from '@flowglad/server'export const flowglad = (customerExternalId: string) => { // customerExternalId is the ID from YOUR app's database, NOT Flowglad's customer ID return new FlowgladServer({ customerExternalId, getCustomerDetails: async (customerExternalId) => { // Fetch customer details from YOUR database using YOUR app's ID const user = await db.users.findOne({ id: customerExternalId }) if (!user) { throw new Error('Customer not found') } return { email: user.email, name: user.name, } }, })}// Usage:// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst billing = await flowglad(userId).getBilling()
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
type FlowgladServerOptions = ScopedFlowgladServerParamsinterface ScopedFlowgladServerParams { /** * The customer ID from YOUR app's database (e.g., user.id or organization.id). * This is NOT Flowglad's customer ID—Flowglad uses this external ID to identify * and create customers in its system. * * For B2C apps: Use your user.id * For B2B apps: Use your organization.id or team.id */ customerExternalId: string baseURL?: string apiKey?: string /** * Handler to retrieve customer details from your database. * * This function is called when attempting to create a customer record in Flowglad * for a customer that doesn't yet exist. Implement this to fetch the customer's * name and email from your database based on their customer ID. * * @param customerExternalId - The external customer ID from YOUR system (not Flowglad's) * @returns Promise resolving to an object containing the customer's name and email */ getCustomerDetails: (customerExternalId: string) => Promise<{ name: string email: string }>}
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
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.
Copy
Ask AI
// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst customer = await flowglad(userId).findOrCreateCustomer()
Get comprehensive billing information for the customer, including helper functions like checkFeatureAccess, checkUsageBalance, getProduct, and getPrice.
Copy
Ask AI
// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst billing = await flowglad(userId).getBilling()const hasPremium = billing.checkFeatureAccess('premium_feature')const invoiceCount = billing.invoices.length
Get the default pricing model with products, prices, and features.
Copy
Ask AI
// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst pricingModel = await flowglad(userId).getPricingModel()// Returns products, prices, and features available for purchase
// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst checkoutSession = await flowglad(userId).createCheckoutSession({ priceSlug: 'price_123', successUrl: 'https://example.com/success', cancelUrl: 'https://example.com/cancel', quantity: 1, outputMetadata: { campaign: 'summer_sale', },})// Redirect user to checkoutSession.url
Parameters:
Copy
Ask AI
interface CreateCheckoutSessionParams { /* * Must specify either priceId or priceSlug * Slug is encouraged over Id */ priceId?: string priceSlug?: string successUrl: string cancelUrl: string quantity?: number outputMetadata?: Record<string, string> outputName?: string}
export async function getCustomerPortalData(customerExternalId: string) { // customerExternalId is the ID from YOUR app's database, NOT Flowglad's customer ID const billing = await flowglad(customerExternalId).getBilling() return { customer: billing.customer, activeSubscriptions: billing.subscriptions.filter( (s) => s.status === 'active' ), paymentMethods: billing.paymentMethods, recentInvoices: billing.invoices.slice(0, 10), features: billing.features, }}
import { FlowgladServer } from '@flowglad/server'export const flowglad = (customerExternalId: string) => { // customerExternalId is the ID from YOUR app's database, NOT Flowglad's customer ID return new FlowgladServer({ customerExternalId, getCustomerDetails: async (externalId) => { // Fetch customer details from YOUR database using YOUR app's ID const user = await db.users.findOne({ id: externalId }) if (!user) { throw new Error('Customer not found') } return { email: user.email, name: user.name, } }, })}// Usage:// Pass YOUR app's user/organization ID, not Flowglad's customer IDconst billing = await flowglad(userId).getBilling()
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