> ## Documentation Index
> Fetch the complete documentation index at: https://polar.sh/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Pricing as Code

> Manage events, meters, products and prices from your repository, and ship every pricing change like code, as a new version.

**Pricing as Code** means you manage your catalog from your repository instead of the dashboard. Your events, meters, benefits, products and custom fields live in one file, which the Polar CLI compares with what is on Polar before saving the difference as a draft and publishing it as a new version of your catalog.

Pricing changes more often than it used to, because model prices and your own costs keep moving and your plans have to follow. Each change matters to finance and sales as much as to engineering, yet a dashboard edit has no review step.

With Pricing as Code, a pricing change becomes a code change. It can be reviewed in a pull request, checked in CI and shipped as a new version that doesn't touch existing customers.

## Getting started

A billing config is managed through the Polar CLI. Install it, let it set up your agent if you want, and sign in to your sandbox organization:

```bash Terminal theme={null}
curl -fsSL https://polar.sh/install.sh | bash
polar agent setup
polar auth login --sandbox
```

You can opt in to Pricing as Code with a single command:

```bash Terminal theme={null}
polar config init
```

or ask your agent to take it from here:

```text Prompt theme={null}
Move my pricing into a Polar billing config, using my sandbox organization.
```

If the user has no Polar account, sign them up by following
[https://polar.sh/auth.md](https://polar.sh/auth.md), then run `polar config init` to start from an empty
config. See [Agent signup](https://polar.sh/docs/agents/signup.md).

Work in sandbox until the user asks for production. Run `polar config`
commands with `--json` and read the fields, not the text. `polar config
    validate` changes nothing. `init`, `draft`, `publish` and `disable` change the
organization: run each only after the user agrees to what it will do. `init`
makes the dashboard and API read-only for the catalog, so confirm the user
wants that first. Never invent a product, price, currency or billing interval.
Refer to resources by external ID in code, never by Polar ID. For setup steps
read [Billing config](https://polar.sh/docs/integrate/cli/config.md). For versions, read
[Versioning](https://polar.sh/docs/features/pricing-as-code/versioning.md).

## What it looks like

At its core, a billing config is a JSON representation of your catalog: your meters, benefits, products and prices, and how they link to each other. That means it works from any language, as `polar.config.json`.

In a JavaScript or TypeScript project, the config is `polar.config.ts`, written with `@polar-sh/config`. Say you're building an AI assistant with a Pro plan at \$20 per seat per month, and you want to track the tokens each customer uses:

```ts polar.config.ts theme={null}
import { defineConfig, usd } from '@polar-sh/config'
import { z } from 'zod'

export default defineConfig({
  events: ({ event }) => ({
    completion: event(z.object({ feature: z.string() }))
      .withTokens()
      .withCosts(),
  }),
  meters: ({ meter, events }) => ({
    tokens: meter('Tokens').on(events.completion).sum('_llm.total_tokens'),
  }),
  products: ({ product, seats }) => ({
    pro: product('Pro')
      .prices(seats().flat().amount(usd(20)))
      .recurring('monthly'),
  }),
})
```

`events` describes the shape of the events your app sends, `meters` adds them up per customer, and `products` is what you sell. Each entry's key, such as `pro` or `tokens`, is its **external ID**. It stays the same across versions, which makes it what your code refers to.

Because the package is fully typed, a meter on an event you never declared, or a sum over a property the event doesn't have, fails as a type error instead of showing up on an invoice. `.withTokens()` and `.withCosts()` declare the `_llm` and `_cost` properties that the [LLM ingestion strategy](https://polar.sh/docs/features/usage-based-billing/ingestion-strategies/llm-strategy.md) sends with every model call, and that [Cost Insights](https://polar.sh/docs/features/cost-insights/introduction.md) reads to show what each customer costs you next to what they pay.

For every option and the JSON format, see [Billing config](https://polar.sh/docs/integrate/cli/config.md#write-the-config).

## How it works

`polar config init` writes the billing config from the meters, benefits and products your organization already has, and turns Pricing as Code on. From then on, every change follows the same three commands:

| Command | What it does | Changes Polar |
| - | - | - |
| `polar config validate` | Shows what the file would change: every entry created, updated, unchanged or removed, plus errors with their location in the file | No |
| `polar config draft` | Saves the file to the **draft catalog**. Customers see no difference | Yes, the draft only |
| `polar config publish` | Makes the draft the **live catalog**. New checkouts sell it | Yes |

Polar doesn't read your repository. The CLI sends the file when you run `polar config draft`, whether that's from a terminal, from your agent or from CI.

## Reviewable changes

Because the config is a file, a pricing change goes through the same review as any other change: a diff, a pull request and an approval.

The change doesn't have to start with an engineer, either. A founder, or someone in finance or sales, can ask an agent for it, and the agent edits the config and opens a pull request for an engineer to review and merge.

Your pricing also lives in the same repository as the features it charges for, so one history shows when a feature shipped and when its price changed.

`polar config validate --pr-comment` posts the plan on the pull request in GitHub Actions or Buildkite, so reviewers see which products, prices and benefits change, with their old and new values, before anything is drafted. Once it's merged, run `polar config draft --confirm && polar config publish --confirm` from your deploy pipeline.

See [Run it in CI](https://polar.sh/docs/integrate/cli/config.md#run-it-in-ci).

## Safe versioning

Every publish creates a new **catalog version**, named after what you pass to `polar config draft --name`, or `v1`, `v2` and so on when you don't name it.

* New checkouts sell the live version.
* Existing customers stay on the version they bought: same price, same benefits, same renewals.
* Polar IDs can change between versions, but external IDs don't, so your code should refer to external IDs.
* A product with active subscriptions can't be dropped from the file. Instead you mark it deprecated, so it stops being sold while its subscribers keep it.

Read [Versioning](https://polar.sh/docs/features/pricing-as-code/versioning.md) for how versions work, what existing customers keep and how to deprecate a product.

## Before you adopt

Pricing as Code changes how your whole organization manages its catalog, so know these trade-offs first.

**The dashboard and API become read-only for the catalog.** Once `polar config init` turns Pricing as Code on, products, benefits, meters and custom fields can only change through the billing config, including for teammates who used to edit pricing in the dashboard.

**Turning it off is possible only at first.** `polar config disable` returns the catalog to the dashboard, but only 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. After that, Pricing as Code stays on for the organization for good.

**Not everything is covered yet.** License key, file download, Discord and GitHub repository benefits can't be in a config, and neither can pay-what-you-want prices. If your organization has any of them, `polar config init` stops and lists them.

**Start in sandbox.** [Sandbox](https://polar.sh/docs/integrate/sandbox.md) is a separate organization with its own catalog and versions, so you can try the whole flow there first. The same file works for both organizations.

## Availability

Pricing as Code is in beta. `@polar-sh/config` follows semantic versioning from 1.0.0, so configs that work today will keep working, but expect more of the catalog to be covered and tighter SDK integrations as it develops.

## Learn more

| Resource | Description |
| - | - |
| [Billing config](https://polar.sh/docs/integrate/cli/config.md) | Set up, write, draft and publish a billing config step by step |
| [Versioning](https://polar.sh/docs/features/pricing-as-code/versioning.md) | Drafts, published versions, existing customers and deprecation |
| [`polar config` reference](https://polar.sh/docs/integrate/cli/reference/config) | Every subcommand and flag |
| [Products](https://polar.sh/docs/features/products.md) | What a product, price and billing interval are |
| [Sandbox](https://polar.sh/docs/integrate/sandbox.md) | Try changes without real money |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.