System Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:
Polar is an open-source payment infrastructure platform designed as a Merchant of Record (MoR) for digital products. It handles the complexities of international sales tax, billing, and entitlements, allowing developers to monetize software through a unified API and dashboard. The project is organized as a monorepo consisting of a Python/FastAPI backend and a Next.js frontend.
Sources: AGENTS.md:14-16, DEVELOPMENT.md:3-5
High-Level Infrastructure
The architecture follows a distributed service model where a central API interacts with persistent storage layers and asynchronous workers.
Core Components
- Backend (FastAPI): Serves the REST API, handles authentication, and executes business logic.
- Workers (Dramatiq): Manages asynchronous background tasks and job queues.
- Database (PostgreSQL): Primary relational storage using SQLAlchemy ORM for data persistence.
- Cache & Queue (Redis): Acts as the message broker for workers and a caching layer.
- Storage (S3/MinIO): Handles file uploads and static asset storage.
- Frontend (Next.js): The primary web dashboard for users to manage organizations and products.
The diagram shows the relationship between the frontend dashboard, the backend logic components, and the various data storage and messaging systems.
Sources: AGENTS.md:18-24, DEVELOPMENT.md:8-30, CLAUDE.md:65-70
Backend Architecture
The backend is structured into domain-specific modules located within server/polar/. Each module is encapsulated and typically contains its own endpoints, business services, and repository logic.
Modular Structure
| Component | Description |
|---|---|
endpoints.py | Defines FastAPI routes and request handling. |
service.py | Contains core business logic and service classes. |
repository.py | Encapsulates SQLAlchemy database query logic. |
schemas.py | Pydantic models for request/response validation. |
models/ | SQLAlchemy ORM models (defined globally in server/polar/models). |
Database and ORM
The system uses SQLAlchemy for ORM patterns. A strict separation is maintained between the repository layer (query logic) and the service layer (business logic). Transactions are managed using AsyncSession, and the API typically commits the session automatically at the end of a request to ensure consistency.
Sources: AGENTS.md:60-70, AGENTS.md:112-118
Internal Admin System (Backoffice)
The backoffice is an internal administrative interface built with a specific "Low-JS" architecture using server-side rendering. It utilizes Tagflow for Python-based HTML generation, HTMX for dynamic partial updates, and DaisyUI for styling.
Backoffice Architecture Patterns
- Boosted Navigation: Internal links use HTMX to provide SPA-like transitions without full page reloads.
- Modal Action Pattern: Destructive or complex actions are loaded into modal containers dynamically.
- Form Validation: Uses Pydantic for server-side form validation, with errors rendered directly back to the UI via HTMX swaps.
The sequence diagram illustrates how the Backoffice serves server-rendered HTML and handles dynamic updates using HTMX.
Sources: server/polar/backoffice/DEVELOPMENT_GUIDE.md:13-35, server/polar/backoffice/CLAUDE.md:1-5
External Integrations
Polar acts as a layer on top of major payment and social platforms.
- Stripe: Acts as the underlying "rails" for payment processing and international payouts through Stripe Connect Express.
- GitHub: Integrated for user authentication and repository-specific features (e.g., funding, issue management).
- AI Compliance (LLM): Uses AI agents (via Pydantic AI) to perform risk assessments on new organizations and products, checking against the Acceptable Use Policy.
Sources: AGENTS.md:162-166, DEVELOPMENT.md:204-206, server/polar/organization_review/analyzer.py:685-700
Authentication and Security
Authentication is built on FastAPI's dependency injection using an AuthSubject model. This subject can represent a User, Organization, Customer, or Anonymous entity.
Authentication Scopes
The system uses granular scopes to control access. Authenticators define required_scopes (e.g., discounts_write), and access is granted only if the resolved AuthSubject possesses at least one of those scopes.
Sources: AGENTS.md:131-150
Summary
The Polar architecture leverages a robust Python backend with a modular design, complemented by a modern Next.js frontend. Its internal administration relies on server-side rendering with HTMX for efficiency, while its core value proposition is delivered through tight integrations with Stripe and automated AI compliance systems.
Sources: AGENTS.md:14-25, DEVELOPMENT.md:8-12