Skip to main content
A billing config is the file behind Pricing as Code: it declares your organization’s meters, benefits and products in your repository. This page covers the polar config commands that create, check, draft and publish it. Read the overview for what Pricing as Code changes for your organization, and Versioning for how drafts, versions and existing customers work.

Create the config

Install the Polar CLI, sign in to your sandbox organization and run init:
Terminal
init turns Pricing as Code on, which makes the dashboard and the API read-only for products, benefits, meters and custom fields, so read Before you adopt first. You can undo it with polar config disable until a publish replaces your live catalog. That’s your first polar config publish if your organization already had a catalog when you ran init, or your second if it started from scratch.
init writes the file from your current catalog. A new organization starts from scratch.
  • Format: TypeScript in a project with a package.json, for which init also installs @polar-sh/config, and JSON otherwise. Pass --format ts or --format json to choose.
  • External IDs: init suggests an external ID for each existing resource that doesn’t have one, and asks you to confirm it.
  • Skipped resources: init lists anything it left out of the file, and those stay on Polar as they are.

Write the config

A config holds at most 100 meters, 100 benefits, 100 products and 100 custom fields. For what can’t be in a config yet, see Before you adopt. The examples below are small. For every option, read the types of @polar-sh/config, or with a JSON config let polar config validate tell you which field is wrong and which values it accepts.
Each section is a function that receives its helpers and returns the entries by external ID.
polar.config.ts
The package is fully typed. A wrong helper, or a meter or benefit key that does not exist, is a type error.
Three rules hold in both formats:
  • Never rename an external ID. Changing it removes the old resource and creates a new one, so change the name when you want customers to see something different.
  • A section you leave out is left alone, so a file without meters doesn’t touch your meters.
  • A section you include is complete. A product missing from products is left out of the next catalog version, and an empty list takes every product off sale. validate lists each one as removed, but nothing is deleted and customers keep what they have.

Organization settings

An optional settings section holds organization-wide settings: payments, subscriptions, the Customer Portal and customer notifications. Keys are camelCase in TypeScript and snake_case in JSON. One trade-off: a file with settings describes every setting, so any setting it leaves out goes back to its default, and validate shows that reset as a change. Leave the section out entirely to keep your settings as they are.

Check, draft and publish

1

Check what would change

Terminal
validate lists every entry as created, updated, unchanged or removed, with the old and new value of each changed field. It also reports errors and warnings with their location in the file.
2

Save the draft

Terminal
draft shows the same plan, asks you to confirm, then saves the file to the draft catalog. Nothing a customer sees changes.
3

Publish

Terminal
publish makes the draft the live catalog and needs the owner or admin role.
If you edit the file after drafting, run draft again before you publish. publish warns you when the file has changes the draft doesn’t. You can name the version with --name on either command. Existing customers keep the version they bought, see What existing customers keep.

When the draft and the file disagree

draft stops with a conflict when the draft on Polar has changes your file lacks, such as a teammate’s changes that were never committed to the config file. Pick one:

Sandbox and production

Sandbox and production are separate organizations with separate catalogs, but the file doesn’t record which one it belongs to, so the same file works for both. Check which organization is active with polar auth whoami, or pass --org to target one for a single command.

Generate types

Supported languages: Python
generate only writes Python. Skip this section if you have a TypeScript config, which is already type-checked as you write it.
Your app refers to products, benefits and meters by external ID, and a typo in one of those strings only fails at run time. With a JSON config, generate them as constants and types your app imports:
Terminal
It writes one file next to the config, with every external ID and event name in it. For a Python project that is polar_types.py:
  • Run it again after every change to the config and commit the file. Don’t edit it by hand.
  • polar config generate --check writes nothing and fails when the file is missing or out of date.

Deprecate and prune

You can’t remove a product from the file while customers still use it: validate reports it as an error. Mark it deprecated instead, then draft and publish:
A deprecated entry is left out of the next version, so nothing new can use it while the customers who have it keep it. See Stop selling something. Once nobody uses a deprecated entry, remove it from the file:
Terminal
prune removes every deprecated entry nothing uses any more. It only edits your file and changes nothing on Polar.

Change what a meter counts

Polar rejects a change to the filter or aggregation of a meter with billed usage. Add a new meter under a new external ID, point your prices and benefits at it, and deprecate the old one.

Run it in CI

CI can show every pricing change on its pull request and release it when the pull request merges.
1

Create two access tokens

CI signs in with two organization access tokens stored as repository secrets: a read-only one for pull requests and a write one for the main branch. Only an owner or admin can create them.
Terminal
Piping into gh secret set keeps the token off your screen.
2

Add the workflow

This one is for GitHub Actions and a TypeScript config at the root of the repository.
.github/workflows/polar-config.yml
What it does:
  • On a pull request that changes the config, validate --pr-comment posts what would change as one comment and updates it on every push. The job fails when the config has errors, so a broken config can’t merge. Pull requests from forks are skipped, because they get no secrets.
  • On a merge to main, it saves the draft and publishes it. Remove the publish step if a person should publish after the merge.
To adapt it:
  • JSON config: drop npm ci and point paths at polar.config.json. Add polar config generate --check to the validate job if you generate types.
  • Sandbox organization: set POLAR_ENVIRONMENT: sandbox on the steps that run polar.
  • Approval before release: environment: production is a GitHub environment, so giving it a required reviewer makes every pricing change wait for a person’s approval after the merge.
  • Other CI systems: --pr-comment also works in Buildkite for a repository on GitHub. Elsewhere, run the same commands without it.

Learn more