# Billing endpoints

Every path starts at your [API URL](https://docs.qualithm.com/api.md#find-your-api-url). Every response is JSON, and an error has the [error body](https://docs.qualithm.com/api.md#errors).

## Get the team's plan, usage and next bill

`GET /v1/teams/{teamId}/billing/summary`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `billing.read` permission, which the owner role holds.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |

**Response:** `200` with [BillingSummary](#billingsummary) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `401`, `403`, `404`, `429`, `500`, `503`

```bash
curl "<your-api-url>/v1/teams/<team-id>/billing/summary" \
  -H "Authorization: Bearer <your-api-token>"
```

## Preview what moving to another paid tier would cost

`POST /v1/teams/{teamId}/billing/tier/preview`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `billing.manage` permission, which the owner role holds.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |

**Request body:** [PreviewTierChangeRequest](#previewtierchangerequest)

**Response:** `200` with [TierChangePreview](#tierchangepreview) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `400`, `401`, `403`, `404`, `409`, `413`, `415`, `429`, `500`, `503`

```bash
curl -X POST "<your-api-url>/v1/teams/<team-id>/billing/tier/preview" \
  -H "Authorization: Bearer <your-api-token>" \
  -H "Content-Type: application/json" \
  -d '{"tier":"plus"}'
```

## List the team's invoices

`GET /v1/teams/{teamId}/invoices`

Newest first, by `createdAt` then `id`.

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `billing.read` permission, which the owner role holds.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |
| `limit` | query | integer | No | Items per page, 1 to 100. Default 50. |
| `cursor` | query | string | No | A previous page's `nextCursor`, to read the page after it |

**Response:** `200` with [InvoicePage](#invoicepage) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `400`, `401`, `403`, `404`, `429`, `500`, `503`

```bash
curl "<your-api-url>/v1/teams/<team-id>/invoices" \
  -H "Authorization: Bearer <your-api-token>"
```

## Get the link to an invoice's document, generating it on first request

`POST /v1/teams/{teamId}/invoices/{invoiceId}/document`

Needs an [API token](https://docs.qualithm.com/api.md#authenticate) with the `billing.read` permission, which the owner role holds.

**Parameters**

| Name | In | Type | Required | Description |
| ---- | -- | ---- | -------- | ----------- |
| `teamId` | path | string | Yes | The team the request acts on |
| `invoiceId` | path | string | Yes | An invoice ID: `inv_…` |
| `Idempotency-Key` | header | string | No | A unique key for this request. A retry with the same key and body returns the first response instead of acting again; keys last 24 hours. |

**Response:** `200` with [InvoiceDocument](#invoicedocument) in `data`.

**[Errors](https://docs.qualithm.com/api.md#errors):** `400`, `401`, `403`, `404`, `409`, `429`, `500`, `503`

```bash
curl -X POST "<your-api-url>/v1/teams/<team-id>/invoices/<invoice-id>/document" \
  -H "Authorization: Bearer <your-api-token>"
```

## Types

### BillingSummary

The team's plan, its usage this month and its price.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `tier` | one of `"free"`, `"plus"`, `"pro"`, `"max"` | Yes |  |
| `month` | string | Yes | The month usage counts over: `YYYY-MM`, UTC |
| `limitGrace` | object or null | Yes | Set while a downgrade's grace period keeps more history than the current plan does. After `endsAt`, history the current plan doesn't keep is deleted |
| `limits` | object | Yes |  |
| `limits.events` | integer (int64) | Yes | Events a month before new events throttle |
| `limits.automations` | integer (int64) | Yes | Automation runs a month before runs are limited |
| `usage` | object | Yes |  |
| `usage.events` | [BillingUsageDimension](#billingusagedimension) | Yes |  |
| `usage.automations` | [BillingUsageDimension](#billingusagedimension) | Yes |  |
| `overage` | object | Yes | Usage past the limits, billed at the plan's rates |
| `overage.enabled` | boolean | Yes | True while usage past each limit keeps going at the plan's rates instead of throttling |
| `overage.reported` | object | Yes | Units past each limit reported for billing this month; they're charged with the next renewal at the plan's rates. Reported every few minutes, so it can trail `used` |
| `overage.reported.events` | integer (int64) | Yes |  |
| `overage.reported.automations` | integer (int64) | Yes |  |
| `overage.capCents` | integer (int64) or null | Yes | The most a month's overage may cost before tax, in cents; once reached, usage past each limit throttles again. Null for no cap |
| `monthlyTotal` | [Money](#money) | Yes | What the next renewal will charge before tax, after the discount that still applies; zero for a plan set to end. A plan change mid-period adds its proration to that bill, which this leaves out. The plan price when `estimated` is true |
| `upcomingInvoice` | object or null | Yes | The next renewal from Polar, before tax; null on the free plan, for a plan set to end, or when estimated |
| `estimated` | boolean | Yes | True when Polar couldn't be read, so `monthlyTotal` is the plan price |
| `cancellation` | object or null | Yes | Set when the paid plan is cancelled at the end of its period |
| `slaCredit` | object or null | Yes | The team's latest SLA credit, from the last two closed months |
| `cheaperPlan` | object or null | Yes | Set when a larger plan would have billed this month's usage for less than the team's plan with the overage reported for billing; null otherwise |

### BillingUsageDimension

One metered dimension's usage this month against its limit.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `used` | integer (int64) | Yes |  |
| `limit` | integer (int64) | Yes |  |
| `throttled` | boolean | Yes | True while this dimension is throttled: devices' new events are refused, or automation runs are recorded as `limited`, until the month resets. Read from the live count the platform enforces, so it can turn true before the hourly `used` reaches `limit` |
| `threshold` | integer (int64) | Yes | The highest share of `limit` used this month that notifies the team's owners: 50, 80 or 100, or 0 below half. Read from the hourly `used` |

### Money

An amount of money in a currency's minor units.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `amount` | integer (int64) | Yes | The amount in minor units, e.g. cents |
| `currency` | string | Yes | An ISO 4217 currency code |

### PreviewTierChangeRequest

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `tier` | one of `"plus"`, `"pro"`, `"max"` | Yes | The paid tier to move to |

### TierChangePreview

What a tier change would cost, before it is confirmed.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `from` | one of `"plus"`, `"pro"`, `"max"` | Yes |  |
| `to` | one of `"plus"`, `"pro"`, `"max"` | Yes |  |
| `amountDue` | [Money](#money) | Yes | What the change adds to the next bill, before tax; a negative amount is a credit |
| `nextPaymentAttempt` | integer (int64) or null | Yes | When the next bill is charged, in Unix seconds |

### InvoicePage

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `items` | array of [Invoice](#invoice) | Yes |  |
| `nextCursor` | string or null | Yes | Pass as `cursor` to read the next page; `null` on the last page |

### Invoice

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `id` | string | Yes | An invoice ID: `inv_…` |
| `teamId` | string | Yes | A team ID: `tem_…`; also accepts the legacy `tm_…` |
| `status` | string | Yes |  |
| `total` | [Money](#money) or null | Yes | The invoice total; null until the billing provider sets it |
| `createdAt` | string (date-time) | Yes | UTC time, RFC 3339 |
| `updatedAt` | string (date-time) | Yes | UTC time, RFC 3339 |

### InvoiceDocument

The link to an invoice's document.

| Field | Type | Required | Description |
| ----- | ---- | -------- | ----------- |
| `status` | one of `"ready"`, `"generating"` | Yes | `generating` while the invoice is built; ask again in a few seconds |
| `url` | string or null | Yes | The invoice to view or download; null while generating |
