Skip to main content
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 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. 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.
The response has status 201. The contract is a draft, and each line has an id that starts with ctrl_.
The example shows the main fields. The response has more, such as startDate, endDate and invoiceOffsetDays on each line. See the Create 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. 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.
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.
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:
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.

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.
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.
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 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.
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.
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.
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.
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.
The response has data and pagination. Read one contract with GET /v0/contracts/{id}.

Errors

A failed request has the error body that every API call has. Check code in your code. A schema failure is the one exception: see the note under the table. 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: