Polar Setup

Setting up Polar.sh for billing and subscriptions

Polar Setup

Bklit uses Polar.sh for billing and subscription management. This guide covers setting up Polar for your Bklit instance.

Polar is Optional: You only need Polar.sh if you want to charge for subscriptions. Without Polar, all users have free tier access. Core analytics features work without billing.

Already Set Up? The npx @bklit/create wizard can configure Polar for you during initial setup.

Prerequisites

  • Polar.sh account
  • Polar organization created
  • API access token

Step 1: Create Polar Account

  1. Go to polar.sh
  2. Sign up for an account
  3. Create an organization

Step 2: Get API Credentials

Access Token

  1. Go to Polar dashboard
  2. Navigate to Settings → API
  3. Generate an access token
  4. Copy the token (save it securely)

Organization ID

  1. Go to Polar dashboard
  2. Navigate to Settings → Organization
  3. Copy your Organization ID

Webhook Secret

  1. Go to Polar dashboard
  2. Navigate to Settings → Webhooks
  3. Create a webhook endpoint
  4. Copy the webhook secret

Step 3: Create Products

Pro Plan Product

  1. Go to Products in Polar dashboard
  2. Click "Create Product"
  3. Set up subscription product:
    • Name: "Bklit Pro"
    • Type: Subscription
    • Price: Set your price
    • Billing period: Monthly/Yearly
  4. Copy the Product ID

Free Plan Product (Optional)

If you want a free plan in Polar:

  1. Create a product with $0 price
  2. Copy the Product ID
  3. Set in POLAR_FREE_PRODUCT_ID

Step 4: Configure Environment Variables

Add to your .env file:

# Polar Configuration
POLAR_ACCESS_TOKEN="your-polar-access-token"
POLAR_SERVER_MODE="sandbox"  # or "production"
POLAR_WEBHOOK_SECRET="your-webhook-secret"
POLAR_ORGANIZATION_ID="your-organization-id"
POLAR_PRO_PRODUCT_ID="your-pro-product-id"
NEXT_PUBLIC_POLAR_PRO_PLAN_PRODUCT_ID="your-pro-product-id"  # Same as above
POLAR_FREE_PRODUCT_ID="your-free-product-id"  # Optional

Step 5: Set Up Webhooks

Webhook Endpoint

Your webhook endpoint should be:

https://your-domain.com/api/webhooks/polar

Configure in Polar

  1. Go to Polar dashboard
  2. Navigate to Settings → Webhooks
  3. Add webhook endpoint:
    • URL: https://your-domain.com/api/webhooks/polar
    • Secret: Your webhook secret
    • Events: Subscribe to subscription events

Required Events

Subscribe to these events:

  • subscription.created
  • subscription.updated
  • subscription.canceled
  • subscription.revoked

Local Development with Webhooks

Why You Need a Tunnel

Polar webhooks can't reach localhost. To test webhooks locally, expose your server with a tunnel.

1. Install ngrok:

2. Start services:

# Terminal 1: Your app
pnpm dev

# Terminal 2: ngrok
ngrok http 3000

3. Configure webhook in Polar:

  • Copy ngrok HTTPS URL: https://abc123.ngrok.io
  • In Polar dashboard → Settings → Webhooks
  • Webhook URL: https://abc123.ngrok.io/api/webhooks/polar
  • Secret: Your POLAR_WEBHOOK_SECRET from .env

4. Test subscription:

  • Create test subscription in Polar sandbox
  • Check webhook delivery in Polar logs
  • Verify organization plan updates in your dashboard

Alternative: Cloudflare Tunnel

cloudflared tunnel --url http://localhost:3000

Use: https://abc123.trycloudflare.com/api/webhooks/polar

Important Notes

  • Free ngrok URLs change on restart - Update Polar webhook URL when restarting
  • Use sandbox mode: POLAR_SERVER_MODE="sandbox" for local dev
  • Optional for basic dev - Webhooks only needed for subscription testing
  • Production uses your domain - No tunnels needed in production

For complete local development setup: Local Development

Step 6: Test Webhooks

Sandbox Mode

  1. Set POLAR_SERVER_MODE="sandbox"
  2. Test subscription creation
  3. Verify webhook receives events
  4. Check dashboard updates

Production Mode

  1. Set POLAR_SERVER_MODE="production"
  2. Test with real subscriptions
  3. Monitor webhook logs
  4. Verify billing updates

Server Modes

Sandbox Mode

Use for:

  • Development
  • Testing
  • Staging environments

Features:

  • Test subscriptions
  • No real charges
  • Webhook testing

Production Mode

Use for:

  • Live applications
  • Real customers
  • Production billing

Features:

  • Real charges
  • Live subscriptions
  • Production webhooks

Troubleshooting

Webhooks Not Receiving Events

  • Verify webhook URL is correct
  • Check webhook secret matches
  • Ensure endpoint is publicly accessible
  • Check webhook logs in Polar dashboard

Subscription Not Updating

  • Verify webhook events are subscribed
  • Check webhook secret is correct
  • Review webhook logs
  • Verify database connection

API Errors

  • Check access token is valid
  • Verify organization ID is correct
  • Ensure product IDs match
  • Check server mode (sandbox vs production)

On this page