Table of Contents
- Installation
- Basic Setup
- Protecting API Routes
- Server Components
- Client Components
- Middleware Setup
- Payment Flow Integration
- Complete Example
Installation
Install the required packages:Basic Setup
1. Environment Variables
Create a.env.local file:
2. Initialize SolvaPay Client
Create a shared SolvaPay instance:Protecting API Routes
Basic API Route Protection
Use thepayable.next() adapter to protect Next.js App Router API routes:
Using Next.js Helpers
The@solvapay/next package provides route-wrapper helpers that handle auth, body parsing, error formatting, and cache invalidation. Every wrapper returns Promise<NextResponse> — return it directly from your route handler.
Available Helper Functions
Check Purchase
Create Payment Intent
Process Payment
Create Checkout Session
Create Customer Portal Session
Sync Customer
Cancel Renewal
Reactivate Renewal
Activate Plan
{ status, purchaseRef?, checkoutUrl?, creditBalance?, ... }. Usage-based plans now activate eagerly at the plan step (no balance gate). See Purchase lifecycle management for all status values.
List Plans
{ plans, productRef }. Pass ?productRef=prd_... as a query parameter.
Merchant, Product, and Payment Method
getMerchantreturns{ name, iconUrl, logoUrl, termsUrl, privacyUrl, ... }— use it to brand checkout and mandate copy.getProductreturns the product with public plans for the current authenticated customer.getPaymentMethodreturns{ kind: 'card', brand, last4, expMonth, expYear } | { kind: 'none' }, mirrored from the last successfulpayment_intent.succeededwebhook. Safe to poll alongsidecheck-purchase.
Customer Balance
Track Usage
Server Components
In Server Components, reach for the*Core primitives from @solvapay/server instead of the route-wrapper helpers — the wrappers always return NextResponse, which isn’t useful when you want to read data.
Client Components
Use React hooks and components for client-side payment flows:Middleware Setup
Authentication Middleware
Set up authentication middleware (implemented inproxy.ts) to extract user
information and make it available to API routes:
Supabase Auth Middleware
If using Supabase, use the provided middleware helper (exported from@solvapay/next/middleware and used from proxy.ts):
Payment Flow Integration
1. Set Up Provider
Wrap your app withSolvaPayProvider:
2. Create API Routes
Set up the required API routes using Next.js helpers — each is a one-liner because every wrapper returnsPromise<NextResponse>:
3. Use Payment Components
Use React components and hooks in your pages:Complete Example
Here’s a complete Next.js application with SolvaPay integration:Project Structure
Root Layout
Protected API Route
Checkout Page
Dashboard Page
Cache Management
The@solvapay/next package includes purchase caching to reduce API calls:
Best Practices
-
Use Environment Variables: Store API keys and configuration in
.env.local. - Separate API Routes: Keep API routes separate from page routes for better organization.
-
Error Handling: Helpers always return
NextResponse(including error responses). For Server Component / RSC code paths where you need raw data, use the*Coreprimitives from@solvapay/serverand check withisErrorResult. - Type Safety: Use TypeScript for better type safety.
- Cache Management: Use purchase caching to reduce API calls and improve performance.
- Middleware: Set up authentication middleware to extract user information early.
Next Steps
- React Integration Guide - Learn about React components and hooks
- Usage Events - Track and bill usage-based features
- Webhooks - Handle payment and subscription events
- API Reference - Full API documentation