Skip to main content
This guide adds Polar to a Next.js App Router project: a client for the API, a route that sends customers to checkout, and a route that receives webhooks. It uses the @polar-sh/nextjs adapter and API version 2026-10. Build against the sandbox first. Nothing there moves real money. The complete code is in the polar-next example on GitHub. To scaffold the routes in an existing Next.js project instead, run:
Terminal

Install the packages

Terminal

Set the access token

Create an organization access token in your sandbox organization’s Settings → Access Tokens, or run polar auth token, and add it to .env.local:
.env.local

Create an API client

Use the client for any API call your app makes, such as listing products:
src/polar.ts
When you go live, remove environment or set it to "production", and use a production token.

Add a checkout route

The Checkout handler creates a Checkout Session and redirects the customer to it:
src/app/checkout/route.ts
Link to /checkout?products=<product_id>. Add &external_customer_id=<your user ID> so Polar links the customer to your user. The handler appends checkout_id={CHECKOUT_ID} to successUrl. See the adapter reference for every query parameter.

Receive webhooks

Webhooks keep your database in sync with checkouts, orders and subscriptions.

Forward webhooks locally

The Polar CLI forwards your sandbox organization’s webhooks to your dev server:
Terminal
Copy the secret it prints into .env.local:
.env.local
When you deploy, create a webhook endpoint in Settings → Webhooks for https://<your-domain>/api/webhook/polar and use that endpoint’s secret instead.

Add the webhook route

The Webhooks handler verifies the signature, then calls your handlers with a typed payload:
src/app/api/webhook/polar/route.ts
onCustomerStateChanged is often all you need for access control. See Customer state and Webhook events.

Verify it works

  • Start your dev server and polar webhook listen
  • Open /checkout?products=<product_id> and pay with the test card 4242 4242 4242 4242
  • You land on your success URL, and the CLI logs order.paid and customer.state_changed deliveries to your route
The checkout’s status reaches succeeded shortly after payment. Polar sends checkout.updated when it does, so a confirmation page that waits for it can update its UI then.

Learn more