checkSubscription
SolvaPay SDK / next/src / checkSubscription
Function: checkSubscription()
checkSubscription(
request,options):Promise<SubscriptionCheckResult|NextResponse<unknown>>
Defined in: packages/next/src/index.ts:96
Check user subscription status with automatic deduplication and caching.
This Next.js helper function provides optimized subscription checking with:
- Automatic request deduplication (concurrent requests share the same promise)
- Short-term caching (2 seconds) to prevent duplicate sequential requests
- Fast path optimization using cached customer references from client
- Automatic customer creation if needed
The function:
- Extracts user ID from request (via requireUserId from @solvapay/auth)
- Gets user email and name from Supabase JWT token (if available)
- Validates cached customer reference (if provided via header)
- Ensures customer exists in SolvaPay backend
- Returns customer subscription information
Parameters
request
Request
Next.js request object (NextRequest extends Request)
options
Configuration options
Returns
Promise<SubscriptionCheckResult | NextResponse<unknown>>
Subscription check result with customer data and subscriptions, or NextResponse error
Example
import { NextRequest, NextResponse } from 'next/server';
import { checkSubscription } from '@solvapay/next';
export async function GET(request: NextRequest) {
const result = await checkSubscription(request);
if (result instanceof NextResponse) {
return result; // Error response
}
return NextResponse.json(result);
}
See
- clearSubscriptionCache for cache management
- getSubscriptionCacheStats for cache monitoring
Since
1.0.0