Skip to main content
Encore is an open source backend framework for TypeScript with built-in infrastructure automation and observability. When you define services, databases, and APIs in code, Encore automatically provisions and manages the underlying infrastructure, both locally and in the cloud. Because Encore defines infrastructure in code and traces every request, a coding agent can read your Polar integration and inspect the trace of a checkout or webhook to debug it. This guide adds a checkout endpoint and a webhook handler to an Encore app, using the TypeScript SDK and API version 2026-10. Build against the sandbox first. Nothing there moves real money.

Install the SDK

Terminal

Set the secrets

Encore manages secrets the same way locally and in the cloud.

Access token and environment

Create an organization access token in your sandbox organization’s Settings → Access Tokens, or run polar auth token. Add it to Encore, along with POLAR_MODE set to sandbox:
Terminal
You’ll add POLAR_WEBHOOK_SECRET in Receive webhooks. When you deploy to Encore Cloud, set production secrets in its dashboard or your CI pipeline.

Create an API client

Create the client with createPolar, reading the token and environment from Encore’s secrets:
payments/polar.ts
Set POLAR_MODE to production, with a production token, when you go live.

Add a checkout endpoint

This endpoint creates a Checkout Session and redirects the customer to it:
payments/payments.ts
Visit /checkout?product=<product_id> to start a checkout. To link the customer to your user, also pass external_customer_id to checkouts.create.

Receive webhooks

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

Forward webhooks locally

The Polar CLI forwards your sandbox organization’s webhooks to your Encore app:
Terminal
Set the secret it prints as POLAR_WEBHOOK_SECRET:
Terminal
When you deploy, create a webhook endpoint in Settings → Webhooks for https://<your-domain>/webhooks/polar and set that endpoint’s secret in Encore Cloud.

Add the webhook handler

Use api.raw() so the handler gets the raw request body, which signature verification needs. webhooks.validateEvent verifies the signature and returns a typed event:
payments/webhooks.ts
See Handle deliveries for retries and timeouts, and Webhook events for every event type.

Run it locally

Start your Encore app:
Terminal
Encore’s local development dashboard at localhost:9400 lets you:
  • View traces for every request, including checkout redirects and webhook deliveries
  • Inspect API calls between services
  • Query local databases
  • View logs and errors as they happen

Deploy to production

Deploy to Encore Cloud or your own cloud account:
Terminal
Encore provisions the infrastructure your app needs. Then:
  1. Set production secrets in the Encore Cloud dashboard, with POLAR_MODE set to production
  2. Create a webhook endpoint in your production organization for your production domain
  3. Update success_url to your production URL