This endpoint wraps the official
@polar-sh/nextjs Checkout helper. It validates the query string, redirects users to Polar’s hosted checkout, and returns them to /success when payment completes.- Endpoint:
GET /api/checkout - File Location:
src/app/api/checkout/route.ts - Authentication: Pricing UI enforces Supabase auth before building the checkout URL
Query Parameters
Comma-separated Polar product IDs. Usually a single ID such as
prod_monthly_... or prod_yearly_....Source: Polar Dashboard → Products → Copy product ID (or use NEXT_PUBLIC_POLAR_PRODUCT_ID_*).Prefills the email field in Polar Checkout. The pricing component passes the Supabase user’s email.
JSON string forwarded to Polar and later surfaced in webhook payloads. Sabo stores
{ "user_id": "uuid" } so the webhook can link the Polar subscription back to the Supabase user.Example Request
Because the endpoint performs a redirect, you typically build a URL and assign it towindow.location.href:
Example Response
Polar returns
307/308 redirects rather than JSON payloads. The browser navigates to the hosted checkout UI automatically.How It Works
1
Proxy to Polar
@polar-sh/nextjs generates a handler that validates query parameters, injects your POLAR_ACCESS_TOKEN, and negotiates the redirect with Polar.2
Success redirect
successUrl is set to ${NEXT_PUBLIC_SITE_URL}/success. After payment, Polar sends the buyer there. The success page is purely informational—the webhook activates the subscription.3
Webhook metadata
The metadata string travels with the subscription so
/api/webhooks/polar can read user_id and associate the subscription + orders with the right Supabase user.Implementation
The default boilerplate uses"sandbox" for safety during development:
src/app/api/checkout/route.ts
Frontend Integration
The pricing component handles authentication, reads the plan IDs fromsrc/lib/payments/plans.ts, and then calls the endpoint:
src/components/marketing/pricing.tsx (excerpt)
Testing
- Set
POLAR_SANDBOX=trueand ensure ngrok/HTTPS tunneling is running for webhooks. - Sign in, visit
/pricing, and click Upgrade to Pro. - Complete checkout using
4242 4242 4242 4242. - Confirm
/successloads and webhook logs showsubscription.createdfollowed byorder.paid.