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 provides built-in distributed tracing, AI coding assistants can both build and debug your backend end-to-end, including your Polar integration. They can read the code to understand the full system, then inspect traces to diagnose exactly what happened when a checkout completes or a webhook arrives. Consider following this guide while using the Polar Sandbox Environment. This will allow you to test your integration without affecting your production data.

Examples

Install the Polar JavaScript SDK

To get started, install the Polar JavaScript SDK and the webhook verification library:
Terminal

Setting up Secrets

Encore has built-in secrets management that works consistently across local development and cloud environments.

Polar Access Token and Mode

To authenticate with Polar, you need to create an access token and specify if it’s production or sandbox mode. You can create an organization access token from your organization settings. Add it to Encore using the secrets manager:
Terminal

Polar Webhook Secret

You’ll need a webhook secret to verify incoming webhook events. We’ll set this up in the webhook section below.
Terminal
When deploying to Encore Cloud, you’ll set production secrets through the dashboard or CI/CD pipeline.

Configuring a Polar API Client

To interact with the Polar API, create a new instance of the Polar class using Encore’s secret management.
payments/polar.ts
Remember to set POLAR_MODE as production when you’re ready to switch to the production environment.

Generating Polar Checkout Sessions

Create a checkout endpoint that creates a Polar checkout session and redirects the user.
payments/payments.ts
You can now create a checkout session by visiting /checkout?product=YOUR_PRODUCT_ID.

Handling Polar Webhooks

Polar can send you events about various things happening in your organization. This is useful for keeping your database in sync with Polar checkouts, orders, subscriptions, etc. Configuring a webhook is simple. Head over to your organization’s settings page and click on the “Add Endpoint” button to create a new webhook.

Tunneling webhook events to your local development environment

Encore runs your app locally with full infrastructure support. To receive webhooks during local development, you can use the Polar CLI to tunnel webhook events to your local environment.
Terminal
Make sure to copy the webhook secret and set it using:
Terminal

Add Webhook Endpoint

  1. Point the Webhook to your-app.com/webhooks/polar. This must be an absolute URL which Polar can reach. If you use the Polar CLI tunnel, it will handle this for you.
  2. Select which events you want to be notified about. You can read more about the available events in the Events section.
  3. Generate a secret key to sign the requests. This will allow you to verify that the requests are truly coming from Polar.
  4. Add the secret key to Encore:
Terminal

Setting up the Webhook handler

Encore webhook handlers use api.raw() to access the raw request body, which is required for signature verification.
payments/payments.ts
The standardwebhooks library expects the secret to be base64-encoded. Polar provides a raw string (starting with polar_whs_), so you must base64-encode the entire secret before passing it to new Webhook().

Local Development

Start your Encore app:
Terminal
Encore provides a local development dashboard at localhost:9400 where you can:
  • View distributed traces for all requests, including checkout redirects and webhook deliveries
  • Inspect API calls between services
  • Query local databases
  • View logs and errors in real-time
This makes debugging payment flows straightforward since you can trace exactly what happens when a checkout completes or a webhook arrives.

Deploying to Production

When you’re ready to go live, deploy to Encore Cloud or your own cloud account:
Terminal
Encore automatically provisions all the infrastructure your app needs. Remember to:
  1. Set production secrets in the Encore Cloud dashboard
  2. Update the server parameter from "sandbox" to "production" in your Polar client
  3. Update your webhook URL in Polar to point to your production domain
  4. Update successUrl to your production URL