Public API & SDK Generation
Relevant source files
The following files were used as context for generating this wiki page:
Polar provides a comprehensive Public API and a set of officially maintained SDKs to enable developers to integrate payment infrastructure into their applications. The system relies on a central OpenAPI specification generated from the FastAPI backend, which serves as the "single source of truth" for both internal and external client generation.
The architecture ensures that changes made to backend schemas and endpoints are automatically reflected in the client libraries, maintaining type safety and consistency across the monorepo.
Sources: README.md, AGENTS.md:32-34
API Architecture & Schema Generation
The backend is built using FastAPI, which inherently generates an OpenAPI schema based on Pydantic models (schemas) and route definitions. This schema includes endpoint paths, request/response structures, and authentication requirements.
Schema Pipeline
The generation process follows a structured flow from the Python server to various client consumers:
The flow shows how the FastAPI server generates the OpenAPI specification, which is then used to produce both internal TypeScript clients and public SDKs for multiple languages.
Sources: AGENTS.md:12-25, DEVELOPMENT.md:1-20
Key Components
| Component | Responsibility | File Reference |
|---|---|---|
| Pydantic Schemas | Define request/response validation and serialization. | server/polar/{module}/schemas.py |
| FastAPI Routes | Define API paths, methods, and status codes. | server/polar/{module}/endpoints.py |
| OpenAPI Spec | The intermediate JSON/YAML format describing the API. | Generated at runtime or via CLI |
| Internal Client | TypeScript-based client for the web dashboard. | clients/packages/client/ |
Sources: AGENTS.md:83-88, server/CLAUDE.md:15-25
SDK Generation with Speakeasy
Polar uses Speakeasy to generate its official public SDKs from the OpenAPI specification. This ensures that the SDKs remain up-to-date with the latest API features and provide a idiomatic experience for different programming languages.
OpenAPI Overlays
To ensure the generated SDKs are ergonomic and follow best practices, Polar utilizes OpenAPI Overlays. These overlays allow developers to "tweak" the schema without modifying the core backend logic, specifically for SDK generation purposes (e.g., marking fields as read-only).
# Example snippet from sdk/overlays/read_only.yml
overlay: 1.0.0
info:
title: Read Only Overlay
version: 0.0.1
actions:
- target: $..[?(@.name == 'created_at')]
update:
readOnly: true
Sources: sdk/overlays/read_only.yml:1-10, AGENTS.md:32-34
Supported SDKs
Polar maintains official SDKs for the following languages:
- JavaScript/TypeScript: polarsource/polar-js (Node.js and Browsers)
- Python: polarsource/polar-python
Sources: README.md:58-62
Internal Client Generation
For the Next.js web dashboard (clients/apps/web), Polar uses an internal TypeScript client generated via openapi-typescript-codegen. This package provides data-fetching hooks and typed models for use with TanStack Query.
Generation Workflow
When changes are made to the backend API, developers must regenerate the internal client to ensure frontend type-safety:
# From the clients directory
pnpm generate
This command triggers the generation logic located in clients/packages/client, updating the types and hooks used by the web application.
Sources: AGENTS.md:104-106, clients/README.md:23-25
Integration Patterns
Developers integrating with Polar can choose between direct API usage or official SDKs. The API supports core infrastructure such as:
- Public REST API: For managing products, subscriptions, and customers.
- Webhook API: For receiving real-time event notifications.
- OAuth2: For authenticated access to organization resources.
Example Internal Client Usage
The internal client is centralized and exported for use throughout the monorepo's frontend packages.
// Example location: clients/packages/client/src/index.ts
export * from './generated'
Sources: clients/packages/client/src/index.ts:1, README.md:52-56, AGENTS.md:20-22
Summary
The Public API and SDK generation system at Polar is a highly automated pipeline. By leveraging FastAPI's auto-generation capabilities, Speakeasy for multi-language SDKs, and custom OpenAPI Overlays for refinement, Polar maintains a synchronized developer experience across its backend, frontend, and external integrations. This architecture minimizes manual effort and maximizes reliability by treating the backend code as the source of truth for all client-side communication.