Customers & Customer Portal
Relevant source files
The following files were used as context for generating this wiki page:
Polar provides a comprehensive system for managing customers and a dedicated "Customer Portal" for end-users. The Customer Portal acts as a user-facing interface where customers can manage their subscriptions, view order history, and access digital products. This system is designed to handle the boilerplate of billing, receipts, and customer accounts, allowing developers to focus on their core product.
Sources: README.md:14-20, DEVELOPMENT.md:15-30
Architecture Overview
The system is built on a monorepo architecture with a Python/FastAPI backend and a Next.js frontend. Customer data is stored in PostgreSQL and managed through dedicated service layers. The Customer Portal utilizes specialized API endpoints under the /v1/customer-portal/ prefix to provide authenticated access to customer-specific resources without requiring a full Polar user account.
Sources: AGENTS.md:15-30, clients/apps/web/src/app/(main)/[organization]/portal/overview/page.tsx:80-110
System Flow
The following diagram illustrates how the frontend interacts with the backend service layers to populate the Customer Portal.
The diagram shows the request path from the web client through the FastAPI layer to specific customer and portal services, which interact with the primary PostgreSQL database and external payment rails.
Sources: DEVELOPMENT.md:15-30, AGENTS.md:15-35
Customer Service & Management
Backend customer management is primarily handled in server/polar/customer/service.py. This service encapsulates the business logic for creating, updating, and querying customer records. It follows a modular structure where database logic is separated into repositories and business logic into service classes.
Backend Components
| Component | Description |
|---|---|
CustomerService | Manages core customer lifecycles and business logic. |
CustomerRepository | Handles direct SQLAlchemy queries for customer models. |
AuthSubject[Customer] | Represents an authenticated customer entity in the API. |
Sources: server/polar/customer/service.py, AGENTS.md:110-125, AGENTS.md:175-185
The Customer Portal
The Customer Portal is a self-service area for customers of an organization. It is implemented as a set of Next.js pages that fetch data using the Polar SDK. Access is controlled via session tokens, specifically customer_session_token or member_session_token.
Sources: clients/apps/web/src/app/(main)/[organization]/portal/overview/page.tsx:55-65
Authentication and Access
The portal uses specialized tokens to provide secure access. If a valid token is not found or has expired (returning a 401 status), the user is redirected to a request page to obtain a new session.
Portal Data Fetching
The portal overview page aggregates data from several endpoints to provide a complete view of the customer's status:
- Subscriptions: Active and past subscriptions for the customer.
- Claimed Seats: Subscriptions managed via seat-based pricing.
- Orders: Recent purchase history and receipts.
Sources: clients/apps/web/src/app/(main)/[organization]/portal/overview/page.tsx:80-110
Technical Implementation Details
API Endpoints (Customer Portal)
The frontend communicates with several key backend endpoints to populate the portal interface.
| Endpoint | Method | Purpose |
|---|---|---|
/v1/customer-portal/subscriptions/ | GET | Fetches active subscriptions. |
/v1/customer-portal/seats/subscriptions | GET | Fetches subscriptions involving seat-based pricing. |
/v1/customer-portal/orders/ | GET | Fetches order history. |
Sources: clients/apps/web/src/app/(main)/[organization]/portal/overview/page.tsx:85-108
Database Integration
The system uses SQLAlchemy for ORM mapping. Repositories inherit from RepositoryBase to provide standard CRUD operations while maintaining transaction boundaries via AsyncSession. Developers are encouraged to use session.flush() rather than session.commit() in business logic, as the API layer handles the final commit automatically.
Sources: AGENTS.md:140-160, server/polar/backoffice/DEVELOPMENT_GUIDE.md:30-50
Conclusion
The Customers and Customer Portal module provides a robust foundation for Merchant of Record operations. By centralizing customer management in the backend and offering a rich, token-authenticated frontend portal, Polar enables organizations to provide professional-grade billing management to their customers with minimal integration effort.