Skip to main content
Integrate Kelviq subscription billing into your Next.js application using the App Router and React SDK.

Prerequisites

  • Next.js 14+ with App Router
  • A Kelviq account with plans configured
  • API keys from Settings → API keys in the dashboard

Step 1: Get Your API Keys

  1. Sign up at app.kelviq.com
  2. Navigate to Settings → API Keys
  3. Copy two keys:
    • Server API Key — For API routes (keep secret)
    • Client API Key — For React SDK (safe to expose)

Add to Environment Variables

Add these to your .env.local file:
Never expose your server API key to the frontend or commit it to version control.

Step 2: Define Your Plans

In Kelviq Dashboard

  1. Go to Plans section
  2. Create each subscription tier with:
    • Plan Identifier: base, pro, enterprise (code-friendly names)
    • Price: Monthly/yearly pricing
    • Features: List of included features

Define Entitlements

For each plan, configure entitlements (feature access): Example Plan Structure:

Step 3: Install the React SDK


Step 4: Create Customers (Optional)

You have two options for creating customers in Kelviq:

Option 1: Auto-create during checkout

Skip this step entirely. When creating a checkout session, pass the customer’s email or customerId — Kelviq will automatically create the customer record if it doesn’t exist.

Option 2: Create customer explicitly

Create a customer record when a user signs up. This gives you more control and lets you track customers before they subscribe. Create API Routeapp/api/customer/route.ts:
Call from Client Component:
Pre-creating customers is useful when you want to track users in the Kelviq dashboard before they subscribe, or when you need to store additional customer metadata.

Step 5: Implement Checkout

Allow users to subscribe to a plan.

Create Checkout API Route

Create app/api/checkout/route.ts:

Pricing Page

Create app/pricing/page.tsx:

Step 6: Manage Subscriptions

Fetch Current Subscription

Create app/api/subscriptions/route.ts:

Update Subscription (Upgrade/Downgrade)

Create app/api/subscriptions/update/route.ts:

Handle Upgrades/Downgrades in Client

With activate_on_payment, the existing subscription remains active while payment is pending. If the payment method requires UPI approval or 3DS authentication, the updated subscription starts as incomplete. After payment succeeds, the updated subscription becomes active and the previous subscription becomes superseded. If an immediate charge fails, or the customer does not complete payment before expiry, the updated subscription becomes incomplete_expired and the existing subscription stays active.
Do not unlock the new plan immediately after this route returns. Check the updated subscription status or fetch the customer’s current entitlements first.

Step 7: Gate Features with Entitlements

Dynamically enable/disable features based on subscription.

Setup KelviqProvider

Wrap your app in app/layout.tsx:

Access Entitlements in Components

Entitlement Response Structure (React SDK)


Common Patterns


Need Help?

Have questions or need implementation support?