Skip to main content
Why this matters: Subscriptions renew, payments fail, customers upgrade and credits run out, often while the customer isn’t in your app. One webhook and one object tell you everything that changed. See Customer state.
In this step you’ll listen for state changes, then gate your AI feature on the customer’s subscription and credit balance. The examples use the sandbox client from the quickstart overview, on API version 2026-10.

Forward webhooks to your machine

Use the Polar CLI to receive your sandbox organization’s webhooks on your local server:
Terminal
The CLI prints a webhook secret. Put it in your environment as POLAR_WEBHOOK_SECRET.

Handle customer.state_changed

Polar sends customer.state_changed whenever a customer’s subscriptions, benefits or details change. The payload is the full customer state, so you don’t need to fetch anything else.
validateEvent (validate_event in Python) throws if the signature is invalid, and the handler answers 403. Event types your SDK version doesn’t know yet get a 202, so Polar doesn’t retry them. Without an SDK, verify the signature with a Standard Webhooks library. See Webhook delivery for error handling and retries. If you use Next.js, Nuxt or Better Auth, an adapter gives you this handler in a few lines.

Guard usage with the customer state

Before running an expensive action, read the customer state by your user ID and check the balance of the AI tokens meter in active_meters:
Polar never blocks events, so a guardrail like this lives in your application. Then send the usage event for the action, and the loop is complete.

Verify it works

With polar webhook listen running, cancel Jane’s subscription immediately. From the CLI or API, its ID is in active_subscriptions of her customer state. The CLI logs a customer.state_changed delivery, and your handler records that Jane no longer has Pro.
  • Open Sales → Subscriptions
  • Open Jane’s Pro subscription
  • In the actions menu, click Cancel Subscription
  • Set Cancellation Date to Immediately
  • Click Cancel Subscription
Canceling immediately (a revoke in the API) ends the subscription now. A subscription canceled at End of current period stays active until then, so access correctly continues.

You’re done

You’ve built the full loop: usage flows into a meter, a plan bills and credits that usage, customers buy through checkout, and your application stays in sync.

Next: Go live

Set up production, add a webhook endpoint and pass the account review.

Customer portal

Let customers change plans, update payment methods and download invoices.

Webhook events

Every event Polar sends, and when.

How Polar works

The concepts behind everything you just built.