Polar

Open source payments infrastructure that turns software into a business — subscriptions, usage-based billing, digital products, and merchant-of-record compliance in one platform.

9.5Kstars
675forks
Apache License 2.0
Python

Polar is an open source monetization platform built for developers who want to sell SaaS, AI tools, and digital products without spending months building billing infrastructure. It acts as the merchant of record, handling subscriptions, usage-based billing, tax compliance across 100+ countries, and customer lifecycle management so you can focus on building the product itself.

The platform supports selling access to GitHub repositories, Discord communities, license keys, file downloads, and seat-based products through a single unified checkout and customer portal. It integrates with Stripe for payments and provides official SDKs for JavaScript and Python, plus framework adapters for Next.js, BetterAuth, Astro, and more.

At its core, Polar includes an event ingestion engine and metering system that enables usage-based pricing — developers can emit custom events (API calls, token usage, storage bytes) and Polar automatically meters them, generates invoices, and bills customers based on actual consumption. This makes it particularly well-suited for AI SaaS products and developer tooling where pricing tied to resource usage is increasingly the norm.

Polar is open source under the Apache 2.0 license, with a hosted version at polar.sh that acts as the MOR and handles the complexity of tax filings and payment processor relationships.

What You Get

  • Usage-Based Billing Engine - Ingest custom usage events (API calls, token consumption, compute time) via the Ingestion SDK; Polar aggregates them with configurable meters and bills customers at the end of each billing cycle based on actual consumption.
  • Global Merchant of Record - Polar collects and remits VAT, GST, and US sales tax on your behalf across 100+ countries, handling the full compliance burden so you can sell globally without dedicated finance infrastructure.
  • Digital Product Delivery - Sell access to private GitHub repositories, Discord channels, file downloads, and license keys; delivery is automated on purchase and revoked on cancellation via webhook-driven benefit grants.
  • Customer Portal and Lifecycle Management - Customers get a self-serve portal for managing subscriptions, viewing invoices, updating payment methods, and tracking usage — no custom code required.
  • Framework Adapters and SDKs - Official Python and JavaScript SDKs plus adapters for Next.js, Astro, BetterAuth, and 10+ other frameworks enable checkout, customer portal, and webhook handling with minimal boilerplate.
  • Webhook and Public API - Every payment event, subscription change, and usage threshold triggers configurable webhooks; the versioned REST API gives full programmatic control over products, customers, meters, and billing entries.
  • Discount and Trial Management - Create percentage or fixed-amount discount codes, configure free trials with automatic conversion, and set up seat-based or metered pricing with overages — all without writing billing logic.

Common Use Cases

  • AI SaaS with token-based pricing - An LLM wrapper startup emits an event per API call with the token count as metadata; Polar’s meter aggregates daily consumption per customer, generates monthly invoices, and charges overages automatically without any custom billing code.
  • Selling access to a private GitHub repository - A developer releases a premium tool as a private repo; customers pay via Polar checkout, get immediate repo access granted by Polar’s GitHub benefit, and lose access if they cancel — no manual provisioning needed.
  • Discord community with tiered membership - A content creator sells Discord role access through Polar subscriptions; Polar’s Discord benefit automatically assigns roles on subscribe and removes them on cancellation, with different tiers unlocking different channels.
  • Indie developer monetizing a CLI tool - A solo developer sells a license-key-gated CLI with Polar; the checkout generates a license key on purchase, the tool validates it against Polar’s API, and the developer handles no payment processor setup or tax filings themselves.
  • Seat-based SaaS with member management - A B2B tool uses Polar’s seat pricing to charge per active team member; the organization admin can add or remove seats through the customer portal and billing automatically adjusts on the next cycle.

Under The Hood

Architecture Polar uses a domain-driven modular architecture within its Python backend, with each business domain (product, subscription, checkout, meter, event, tax, benefit, webhook) encapsulated in its own package containing explicit layers: endpoints, service, repository, schemas, and tasks. Service classes mediate between the API layer and data access, coordinate cross-domain logic via domain events, and enqueue background jobs through Dramatiq workers backed by Redis. The frontend is a Next.js application communicating with the backend via a typed OpenAPI-generated client, with TanStack Query managing server state. The system follows a clear read/write session split at the database layer, with separate async sessions for read replicas and write primaries, reflecting careful attention to scalability under concurrent workloads.

Tech Stack The backend runs on Python 3.14 with FastAPI and SQLAlchemy 2.0 in async mode against PostgreSQL, using Alembic for schema migrations and asyncpg as the low-level driver. Background jobs use Dramatiq with a Redis broker. The metering pipeline integrates Tinybird for high-throughput event analytics and time-series aggregation. The frontend is Next.js 15 with React 19, Radix UI primitives, Tailwind CSS, and a Vercel AI SDK integration for AI-powered features. The full codebase is organized as a Turborepo monorepo with pnpm workspaces covering the web app, checkout embed, SDK packages, and documentation. Stripe handles payment processing, with Polar wrapping it as the merchant of record.

Code Quality The backend has comprehensive test coverage — 419 test files spanning unit, integration, and endpoint tests organized by domain, using pytest with async fixtures and a real PostgreSQL test database. Python type hints are used pervasively throughout with Pydantic v2 models for schema validation, and Pyright enforces strict static typing. The frontend uses TypeScript with auto-generated types from the OpenAPI spec. Structlog provides structured logging and Logfire instruments FastAPI, SQLAlchemy, and httpx calls for observability. CI enforces lint, type-check, and test runs on every pull request.

What Makes It Unique Polar’s most distinctive technical choice is positioning itself as the merchant of record in an open source package — the compliance infrastructure (tax jurisdiction detection via IPinfo, Stripe Tax integration, VAT ID validation via python-stdnum) is part of the open codebase, not a black box. The event-to-meter pipeline is particularly well engineered: raw events flow through Tinybird for aggregation, meter filters support arbitrary property expressions, and the billing engine converts meter readings into billing entries that get attached to subscription cycles. The benefit grant architecture is extensible by design, with a registry pattern allowing new benefit types (GitHub repos, Discord roles, license keys, file access) to be added without modifying core billing code.

Self-Hosting

Polar is licensed under the Apache License 2.0, one of the most permissive open source licenses available. You can use it commercially, modify it, distribute it, and even offer it as a hosted service without sharing your changes. There are no copyleft requirements and no restrictions on how the software is used in a commercial context. The only obligations are preserving copyright notices and the license file. This means you can self-host Polar as part of a commercial product or internal infrastructure without any licensing fees or compliance concerns.

Running Polar yourself requires meaningful infrastructure: a PostgreSQL database, a Redis instance, S3-compatible object storage, and a Stripe account (you will be the merchant of record, not Polar). The backend runs as a containerized FastAPI application with a separate Dramatiq worker process for background jobs. You will also need a Tinybird account if you want the usage-based metering pipeline to work at scale, since the event aggregation layer depends on their API. The team provides Docker Compose configuration for local development, but production deployments will require your own infrastructure management including database migrations, worker scaling, and observability setup.

The primary trade-off when self-hosting is that you become the merchant of record yourself — meaning you are responsible for tax compliance, VAT registration, and remittance in every jurisdiction where you have customers. The hosted polar.sh service absorbs all of that complexity. Self-hosting also means no managed support, no SLA, and no automatic updates — you are on your own for security patches and migrations. The hosted tier starts at 5% plus 50 cents per transaction, with optional paid plans (Pro, Growth, Scale) offering lower rates; that fee can be thought of as the price of outsourcing the entire financial and compliance layer.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack

No spam. Unsubscribe anytime.

Join 750+ subscribers
No spam. Unsubscribe anytime.

Search