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
Configuring a Polar API Client
To interact with the Polar API, create a new instance of thePolar class using Encore’s secret management.
payments/polar.ts
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
/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
Terminal
Add Webhook Endpoint
- 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. - Select which events you want to be notified about. You can read more about the available events in the Events section.
- Generate a secret key to sign the requests. This will allow you to verify that the requests are truly coming from Polar.
- Add the secret key to Encore:
Terminal
Setting up the Webhook handler
Encore webhook handlers useapi.raw() to access the raw request body, which is required for signature verification.
payments/payments.ts
Local Development
Start your Encore app:Terminal
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
Deploying to Production
When you’re ready to go live, deploy to Encore Cloud or your own cloud account:Terminal
- Set production secrets in the Encore Cloud dashboard
- Update the
serverparameter from"sandbox"to"production"in your Polar client - Update your webhook URL in Polar to point to your production domain
- Update
successUrlto your production URL

