First, set up a Flowglad server client.
Do this in a file that can be imported wherever you need to access billing data, or make calls to Flowglad.
Flowglad’s premade backend modules use the Flowglad Server in sessionful contexts to determine who you’re loading billing data for:
Copy
Ask AI
import { FlowgladServer } from '@flowglad/nextjs/server'// or wherever you initialize your supabase clientimport { createClient } from '@/utils/supabase'export const flowgladServer = new FlowgladServer({ supabaseAuth: { client: createClient },})
Next, set up your Flowglad API route at /api/flowglad/[...path]. Your app will use this to send and receive data from Flowglad.
Copy
Ask AI
// /api/flowglad/[...path]/route.tsimport { createAppRouterRouteHandler } from '@flowglad/nextjs/server';import { flowgladServer } from '@/app/flowglad';const routeHandler = createAppRouterRouteHandler(flowgladServer);export { routeHandler as GET, routeHandler as POST }
You can mount Flowglad’s handler at a different route, but you’ll
need to specify it via the serverRoute prop in <FlowgladProvider /> in your React app.
Next, you need to set up the FlowgladProvider component. For the fastest possible demo, make your success route the home page you load once customers sign in.
Copy
Ask AI
// app/layout.tsximport { PropsWithChildren } from 'react'import { FlowgladProvider } from '@flowglad/react'// or wherever you initialize your supabase clientimport { createClient } from '@/utils/supabase'export default function RootLayout({ children,}: PropsWithChildren) { const supabase = createClient(); const { data: { user } } = await supabase.auth.getUser(); return ( <FlowgladProvider loadBilling={!!user}> { /* ... existing layout JSX ... */} {children} { /* ... existing layout JSX ... */} </FlowgladProvider> )}
If you are using custom route for your Flowglad handler instead of
/api/flowglad, set it on FlowgladProvider’s serverRoute prop,
using /path/to/your/route.