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

# Contracts

> Create a contract for a customer, review and approve it, change it later, and preview the invoices it will produce.

A contract is an agreement with one customer for a fixed term. It lists what you sell the customer, at what price, and how often you bill it. When you approve a contract, Paygentic writes the whole term as scheduled invoice lines. Until then, nothing is billed.

Use the contracts API when the terms come from outside Paygentic, for example from a CRM deal, and a person must check them before billing starts.

You need an API key with the `contracts:read` permission to read contracts, `contracts:write` to create and change them, and `contracts:approve` to approve or reject them. See [API keys](https://docs.paygentic.io/api-keys.md) for how to send the key.

## How a contract moves

A contract starts as a **draft**. You can change a draft as often as you like. When someone approves it, it becomes the contract in force. After that, a change does not touch the contract in force. It waits beside it as a **pending change** until someone decides on it.

| Status | Meaning |
| - | - |
| `draft` | Never approved. It waits for a decision. |
| `rejected` | Never approved, and a person rejected it. It cannot change again. |
| `scheduled` | Approved. `contractStart` has not come yet. |
| `active` | Approved. `contractStart` has passed and the contract has not ended. |
| `ended` | `contractEnd` or `terminatedAt` has passed. |

After approval, the status follows the dates on the customer's clock. If the customer has a test clock, the test-clock time decides.

## Create a contract

Call `POST /v0/contracts`. Both values of `autoApprove` are required, and there is no default. Set `autoApprove.contract` to `false` to review the contract first.

```bash theme={null}
curl -X POST "https://api.paygentic.io/v0/contracts" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customerId": "cus_2hNYSCIrw6LUITza",
    "currency": "USD",
    "name": "Acme annual agreement",
    "contractStart": "2027-01-01T00:00:00Z",
    "contractEnd": "2028-01-01T00:00:00Z",
    "autoCharge": false,
    "bankTransferOnly": false,
    "autoApprove": { "contract": false, "invoices": false },
    "externalReference": { "provider": "hubspot", "externalId": "deal-4821" },
    "lines": [
      {
        "itemId": "itm_gr3drv86tUvjr85W",
        "price": { "model": "standard", "properties": { "unitPrice": "12000" } },
        "billingCadence": "P1Y",
        "paymentTerm": "in_advance"
      },
      {
        "itemId": "itm_Jsy8kLr3tH89o1Op",
        "price": { "model": "standard", "properties": { "unitPrice": "0.002" } },
        "billingCadence": "P1M",
        "paymentTerm": "in_arrears"
      }
    ]
  }'
```

The response has status `201`. The contract is a `draft`, and each line has an id that starts with `ctrl_`.

```json theme={null}
{
  "id": "ctr_HYA5sDacOMeE7ZxR",
  "object": "contract",
  "merchantId": "org_vtH5Z5hkE9y5XwZA",
  "customerId": "cus_2hNYSCIrw6LUITza",
  "currency": "USD",
  "status": "draft",
  "createdAt": "2026-11-27T02:38:08.353Z",
  "externalReference": { "provider": "hubspot", "externalId": "deal-4821" },
  "name": "Acme annual agreement",
  "contractStart": "2027-01-01T00:00:00.000Z",
  "contractEnd": "2028-01-01T00:00:00.000Z",
  "autoCharge": false,
  "bankTransferOnly": false,
  "autoApprove": { "contract": false, "invoices": false },
  "lines": [
    {
      "id": "ctrl_infmZFycrWTZV6cZ",
      "itemId": "itm_gr3drv86tUvjr85W",
      "billableMetricId": null,
      "price": { "model": "standard", "properties": { "unitPrice": "12000" } },
      "quantity": "1",
      "billingCadence": "P1Y",
      "paymentTerm": "in_advance",
      "invoiceDisplayName": "Platform licence"
    },
    {
      "id": "ctrl_01AWv4TGvgHR0uXh",
      "itemId": "itm_Jsy8kLr3tH89o1Op",
      "billableMetricId": "bm_RuFh4TqtMMqN1PtJ",
      "price": { "model": "standard", "properties": { "unitPrice": "0.002" } },
      "quantity": "1",
      "billingCadence": "P1M",
      "paymentTerm": "in_arrears",
      "invoiceDisplayName": "API calls"
    }
  ],
  "updatedAt": "2026-11-27T02:38:08.353Z",
  "pendingChange": null,
  "validation": [],
  "conflicts": []
}
```

The example shows the main fields. The response has more, such as `startDate`, `endDate` and `invoiceOffsetDays` on each line. See the [Create](https://docs.paygentic.io/api-reference/contracts/create.md) API reference for all fields.

### Send the same deal twice

When you send an `externalReference`, a create first looks for the contract that record already has. If there is one, the response is that contract with status `200`, and nothing is created. You can send a CRM deal again after a timeout without making a second contract. If the `externalReference` already has a contract for another customer, the create fails with a `409` and `CONTRACT_REFERENCE_CUSTOMER_MISMATCH`, and nothing is written.

### Line entries

Every entry in `lines` is a new line. You can write an entry in two ways when you create a contract.

| Entry | What it makes |
| - | - |
| `{ itemId, price, billingCadence, paymentTerm }` | One line for an item, priced in the request. |
| `{ planVersionId }` | One line for each price of the plan version. |

A line for an item is a fee line when the item has no billable metric. It is a metered line when the item has one. When the item has several billable metrics, name the one to bill in `billableMetricId`. If you do not, the request fails with a `400` and `CONTRACT_LINE_METRIC_AMBIGUOUS`. The error lists the metrics.

A line from a plan version copies the price, cadence, payment term and display fields. It does not follow later changes to the plan.

```bash theme={null}
curl -X POST "https://api.paygentic.io/v0/contracts" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "customerId": "cus_2hNYSCIrw6LUITza",
    "currency": "USD",
    "name": "Acme from a plan",
    "contractStart": "2027-01-01T00:00:00Z",
    "contractEnd": "2028-01-01T00:00:00Z",
    "autoCharge": false,
    "bankTransferOnly": false,
    "autoApprove": { "contract": false, "invoices": false },
    "lines": [{ "planVersionId": "pver_hwf6pZQ78StqFvYG" }]
  }'
```

A plan version with a monthly fee and a metered price gives a contract with two lines. Each line has `copiedFromPriceId` set to the price it copied.

## Change a draft

Call `PATCH /v0/contracts/{id}`. Send the `updatedAt` that you read as `basedOnUpdatedAt`. If the draft changed after you read it, the request fails with a `409` and `CONTRACT_CHANGED`, so two people cannot overwrite each other.

When you send `lines`, they are the complete set of lines. An entry with an `id` keeps that line and changes the fields you send. An entry without an `id` adds a line. A line that you leave out is removed. If you do not send `lines`, the lines stay as they are.

```bash theme={null}
curl -X PATCH "https://api.paygentic.io/v0/contracts/ctr_HYA5sDacOMeE7ZxR" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "basedOnUpdatedAt": "2026-11-27T02:38:08.353Z",
    "note": "Signed 2026-11-20",
    "lines": [
      { "id": "ctrl_infmZFycrWTZV6cZ", "quantity": "2" },
      { "id": "ctrl_01AWv4TGvgHR0uXh" }
    ]
  }'
```

The response is the draft, with a new `updatedAt`. The licence line now has `"quantity": "2"`, and the second line is as it was.

The customer and the currency are fixed when you create the contract. Send the same value or leave them out. A different value fails with a `400` and `CONTRACT_FIELD_FIXED`.

## Check a draft before you approve it

Every response that holds a draft or a pending change has two lists, `validation` and `conflicts`.

`validation` lists the reasons you cannot approve the contract yet. This example is a line that starts before the contract term:

```json theme={null}
[
  {
    "scope": "line",
    "code": "LINE_DATES_OUTSIDE_TERM",
    "lineId": "ctrl_ks8PXsiYCMX6rhbY",
    "field": "startDate",
    "message": "A line must start inside the contract term"
  }
]
```

`conflicts` lists lines that share an item or a billable metric with a line of another live contract of the same customer. A conflict is a warning. It never stops an approval.

```json theme={null}
[
  {
    "code": "ITEM_ON_OTHER_LIVE_CONTRACT",
    "lineId": "ctrl_ks8PXsiYCMX6rhbY",
    "otherContractId": "ctr_HYA5sDacOMeE7ZxR",
    "otherLineId": "ctrl_01AWv4TGvgHR0uXh",
    "itemId": "itm_Jsy8kLr3tH89o1Op",
    "message": "Line ctrl_ks8PXsiYCMX6rhbY and line ctrl_01AWv4TGvgHR0uXh of contract ctr_HYA5sDacOMeE7ZxR name the same item over overlapping dates"
  }
]
```

## Preview the invoices

Call `GET /v0/contracts/{id}/projected` to see the invoices the contract would produce. It writes nothing, so you can call it on a draft as often as you like. Use `limit` and `offset` to page through the invoices.

```bash theme={null}
curl "https://api.paygentic.io/v0/contracts/ctr_HYA5sDacOMeE7ZxR/projected?limit=2" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY"
```

The invoices are in date order, earliest first. They are grouped as the invoice collector groups them: one invoice is one customer, currency, invoice date and charge date. A fee that you invoice early is on a separate invoice from usage that you charge on the same day.

```json theme={null}
{
  "data": [
    {
      "invoiceAt": "2027-01-01T00:00:00.000Z",
      "chargeDate": "2027-01-01T00:00:00.000Z",
      "dueAt": "2027-01-04T00:00:00.000Z",
      "paymentTermDays": 3,
      "currency": "USD",
      "autoCharge": false,
      "bankTransferOnly": false,
      "autoApproveInvoices": false,
      "lineItems": [
        {
          "contractLineId": "ctrl_infmZFycrWTZV6cZ",
          "contractVersion": 1,
          "itemId": "itm_gr3drv86tUvjr85W",
          "type": "fee",
          "displayName": "Platform licence",
          "description": null,
          "periodStart": "2027-01-01T00:00:00.000Z",
          "periodEnd": "2028-01-01T00:00:00.000Z",
          "paymentTerm": "in_advance",
          "quantity": "2",
          "unitPrice": "12000",
          "subtotal": "24000",
          "upliftFactor": "1"
        }
      ]
    },
    {
      "invoiceAt": "2027-02-01T00:00:00.000Z",
      "chargeDate": "2027-02-01T00:00:00.000Z",
      "dueAt": "2027-02-04T00:00:00.000Z",
      "paymentTermDays": 3,
      "currency": "USD",
      "autoCharge": false,
      "bankTransferOnly": false,
      "autoApproveInvoices": false,
      "lineItems": [
        {
          "contractLineId": "ctrl_01AWv4TGvgHR0uXh",
          "contractVersion": 1,
          "itemId": "itm_Jsy8kLr3tH89o1Op",
          "type": "metered",
          "displayName": "API calls",
          "description": null,
          "periodStart": "2027-01-01T00:00:00.000Z",
          "periodEnd": "2027-02-01T00:00:00.000Z",
          "paymentTerm": "in_arrears",
          "quantity": null,
          "unitPrice": "0.002",
          "subtotal": null,
          "upliftFactor": "1"
        }
      ]
    }
  ],
  "pagination": { "limit": 2, "offset": 0, "total": 13 }
}
```

A metered line shows its rate in `unitPrice`. It has no `quantity` or `subtotal` until Paygentic measures the usage. When a pending change waits, each line shows the amounts of the change from its next billing moment. Before that moment, the line shows the amounts in force. Paygentic reads the dates on the customer's clock, which is its test clock when it has one. A rejected contract has no invoices.

For all fields, see the [Projected invoices](https://docs.paygentic.io/api-reference/contracts/projected-invoices.md) API reference.

## Approve or reject

Call `POST /v0/contracts/{id}/decision` with `approve` or `reject`. This needs the `contracts:approve` permission. Send the `updatedAt` of the version you read as `basedOnUpdatedAt`.

```bash theme={null}
curl -X POST "https://api.paygentic.io/v0/contracts/ctr_HYA5sDacOMeE7ZxR/decision" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "decision": "approve", "basedOnUpdatedAt": "2026-11-27T02:38:29.792Z" }'
```

The response is the contract. Its `status` is now `scheduled`, because `contractStart` is in the future. `decidedAt` and `decidedByPrincipalId` show when and by whom. Paygentic has written every invoice line of the whole term in one transaction.

To reject, send `reason` (up to 200 characters) and `text` (up to 2000 characters). An approval does not take them.

```bash theme={null}
curl -X POST "https://api.paygentic.io/v0/contracts/ctr_P01F6cIT1rW337vv/decision" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "decision": "reject",
    "basedOnUpdatedAt": "2026-11-27T02:38:54.111Z",
    "reason": "Wrong start date",
    "text": "Resend from HubSpot"
  }'
```

The status is now `rejected`, and `rejectReason` and `rejectText` hold your words. A rejected contract cannot change again.

If the contract has validation errors, an approval fails with a `409` and `CONTRACT_INVALID`. The body lists the errors in `details.errors`, and the contract stays as it was.

### Approve on create

Set `autoApprove.contract` to `true` to approve a contract in the request that writes it. This is useful for a contract you import from a system that already approved it. The request needs the `contracts:approve` permission, and it fails with a `403` without it.

If the contract is valid, Paygentic approves it, and its status follows its dates: `scheduled` before `contractStart`, `active` after it. If it is not valid, the contract stays a `draft`, and `validation` lists the errors. Someone can fix it and approve it later.

A `PATCH` that leaves `autoApprove.contract` set to `true` needs `contracts:approve` too.

`autoApprove.invoices` is a separate choice. It sets whether Paygentic approves the contract's invoices when it collects them.

## Change an approved contract

The first `PATCH` of an approved contract opens a pending change. It is a copy of the contract in force with your fields applied. Later `PATCH` requests edit that change. Send the change's `updatedAt` as `basedOnUpdatedAt`.

```bash theme={null}
curl -X PATCH "https://api.paygentic.io/v0/contracts/ctr_HYA5sDacOMeE7ZxR" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "lines": [
      { "id": "ctrl_infmZFycrWTZV6cZ", "quantity": "3" },
      { "id": "ctrl_01AWv4TGvgHR0uXh" }
    ]
  }'
```

The top level of the response still shows the contract in force. The licence line has `"quantity": "2"`. The new quantity is in `pendingChange`, which has the same fields as the contract and its own `updatedAt`.

```json theme={null}
{
  "id": "ctr_HYA5sDacOMeE7ZxR",
  "status": "scheduled",
  "updatedAt": "2026-11-27T02:38:38.614Z",
  "pendingChange": {
    "name": "Acme annual agreement",
    "lines": [
      { "id": "ctrl_infmZFycrWTZV6cZ", "quantity": "3" },
      { "id": "ctrl_01AWv4TGvgHR0uXh", "quantity": "1" }
    ],
    "updatedAt": "2026-11-27T02:38:43.723Z"
  }
}
```

A contract has at most one pending change. If you send a `PATCH` without `basedOnUpdatedAt` while a change waits, the request fails with a `409` and `CONTRACT_PENDING_CHANGE_EXISTS`. Read the contract and send the `updatedAt` of the change.

To discard a pending change, reject it with the change's `updatedAt`. The contract in force stays as it was, and `pendingChange` is `null` again.

Approving a pending change on a contract that is already approved is not available yet. It fails with a `409` and `CONTRACT_RETILE_NOT_AVAILABLE`. While a change waits, `validation` lists this code.

## List contracts

Call `GET /v0/contracts`. The newest contract is first. Use these filters, and `limit` and `offset` to page.

| Parameter | Description |
| - | - |
| `merchantId` | Only the contracts of this merchant. |
| `customerId` | Only the contracts of this customer. |
| `status` | Only contracts in one of these statuses. Repeat the parameter to pass several. |
| `hasPendingChange` | `true` returns the drafts never approved and the approved contracts with a change that waits for a decision. `false` returns the others. |

```bash theme={null}
curl "https://api.paygentic.io/v0/contracts?customerId=cus_2hNYSCIrw6LUITza&status=scheduled&hasPendingChange=true" \
  -H "Authorization: Bearer sk_live_YOUR_API_KEY"
```

The response has `data` and `pagination`. Read one contract with `GET /v0/contracts/{id}`.

## Errors

A failed request has the [error body](https://docs.paygentic.io/errors.md) that every API call has. Check `code` in your code. A schema failure is the one exception: see the note under the table.

| Status | Code | When |
| - | - | - |
| `409` | `CONTRACT_CHANGED` | The draft or pending change changed after you read it. Read it again and retry. |
| `409` | `CONTRACT_PENDING_CHANGE_EXISTS` | The contract waits for a decision and you did not send `basedOnUpdatedAt`. |
| `409` | `CONTRACT_NOT_PENDING` | Nothing waits for a decision. The contract was already decided, or it is rejected. |
| `409` | `CONTRACT_INVALID` | You approved a version with validation errors. `details.errors` lists them. |
| `409` | `CONTRACT_RETILE_NOT_AVAILABLE` | You approved a change to an approved contract. |
| `400` | `CONTRACT_FIELD_FIXED` | A PATCH tried to change the customer or the currency. |
| `400` | `CONTRACT_LINE_METRIC_AMBIGUOUS` | The item has several billable metrics and the line names none. |
| `400` | `CONTRACT_LINE_UNKNOWN` | A line `id` is not a line of this contract. |
| `400` | `CONTRACT_LINE_REPEATED` | A line `id` appears twice in `lines`. |
| `409` | `CONTRACT_REFERENCE_CUSTOMER_MISMATCH` | A create sent an `externalReference` that already has a contract for another customer. Nothing is written. |
| `409` | `TRANSIENT_CONTENTION` | Another create of the same record was in progress. Send the request again. |
| `400` | `CONTRACT_PLAN_CURRENCY_MISMATCH` | A `{ planVersionId }` entry names a plan in another currency than the contract. |
| `400` | `CONTRACT_PLAN_PRICE_FEATURES`, `CONTRACT_PLAN_PRICE_CREDITS`, `CONTRACT_PLAN_PRICE_GRANT_DISCOUNT`, `CONTRACT_PLAN_PRICE_OBLIGATION` | A price of the plan version cannot be copied to a line: it has several features, it is priced in credits, it has a grant discount, or it is an obligation. `details.priceId` names the price. |
| `403` | none | Your key lacks the permission. `requiredPermissions` names it. |
| `404` | none | The contract, customer, item, billable metric or plan version does not exist, or it belongs to another merchant. |
| `400` | none | The body does not match the schema, for example a missing `autoApprove` or an `id` on a line you create. See the note below. |

A schema failure has `"error": "validation_error"`, a `message` that names the field, and a `requestId`. It has no `code` and no `errors` array, so test `error` and read `message` for it.

This is the body of a stale write:

```json theme={null}
{
  "message": "The contract changed after it was read; read it again and retry",
  "error": "conflict",
  "code": "CONTRACT_CHANGED",
  "details": {
    "updatedAt": "2026-11-27T02:38:29.792Z",
    "basedOnUpdatedAt": "2026-11-27T02:38:08.353Z",
    "code": "CONTRACT_CHANGED"
  },
  "requestId": "req_0JMfW0xrOkpAhtG1fyYf"
}
```

## Related pages

* [Projected invoices](https://docs.paygentic.io/platform/billing/projected-invoices.md): preview the invoices of a subscription
* [Invoices](https://docs.paygentic.io/platform/billing/invoices.md): how Paygentic issues and collects invoices
* [Errors](https://docs.paygentic.io/errors.md): the shape of an error response


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