Skip to main content

Table of Contents

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 the payable.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.
Breaking change (SDK 1.1): wrappers such as checkPurchase, createPaymentIntent, processPaymentIntent, activatePlan, cancelRenewal, reactivateRenewal, createCheckoutSession, createCustomerSession, syncCustomer, listPlans, getMerchant, getProduct, getPaymentMethod, getCustomerBalance, and trackUsage now always return Promise<NextResponse>. Earlier versions returned NextResponse | data, so if you still have result instanceof NextResponse ? result : NextResponse.json(result) in your handlers, delete that branch.Unchanged: getAuthenticatedUser, getCustomerReference, and syncCustomer helpers that return raw user/customer data stay as-is.

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

Response body: { status, purchaseRef?, checkoutUrl?, creditBalance?, ... }. Usage-based plans are topup-first: a zero-balance customer receives topup_required and the plan activates only after a successful top-up. See Purchase lifecycle management for all status values.

List Plans

Response body: { plans, productRef }. Pass ?productRef=prd_... as a query parameter.

Merchant, Product, and Payment Method

  • getMerchant returns { name, iconUrl, logoUrl, termsUrl, privacyUrl, ... } — use it to brand checkout and mandate copy.
  • getProduct returns the product with public plans for the current authenticated customer.
  • getPaymentMethod returns { kind: 'card', brand, last4, expMonth, expYear } | { kind: 'none' }, mirrored from the last successful payment_intent.succeeded webhook. Safe to poll alongside check-purchase.

Customer Balance

Track Usage

Credit Top-Ups

createTopupPaymentIntent and processTopupPaymentIntent back the TopupForm components for pay-as-you-go credit purchases:
createTopupPaymentIntent also accepts an autoRecharge payload to arm auto-recharge on the same charge. See the auto-recharge guide.

Auto-Recharge

getAutoRecharge, saveAutoRecharge, and disableAutoRecharge share one route and back the <AutoRecharge> component:
See the auto-recharge guide for configuration inputs and the React surface.

Business Details

attachBusinessDetails attaches a business buyer’s company name, country, and tax ID to a payment intent and returns the computed tax breakdown:
See the business checkout guide for the form parts and tax treatment details.

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 in proxy.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 with SolvaPayProvider:

2. Create API Routes

Set up the required API routes using Next.js helpers — each is a one-liner because every wrapper returns Promise<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

  1. Use Environment Variables: Store API keys and configuration in .env.local.
  2. Separate API Routes: Keep API routes separate from page routes for better organization.
  3. Error Handling: Helpers always return NextResponse (including error responses). For Server Component / RSC code paths where you need raw data, use the *Core primitives from @solvapay/server and check with isErrorResult.
  4. Type Safety: Use TypeScript for better type safety.
  5. Cache Management: Use purchase caching to reduce API calls and improve performance.
  6. Middleware: Set up authentication middleware to extract user information early.

Next Steps