<!-- Generated by scripts/build-docs.js. Edit docs/swagger.yaml or docs/guides/, then run `npm run docs:build`. -->

# Recalculate a sale

`POST https://marketplace-api.sequifi.com/v1/sales/{pid}/recalculate`

Module: Sales · operationId: `recalculateSale` · Auth: `Authorization: Bearer <token>`
Web page: https://apidocs.sequifi.com/#/api/recalculate-sale

Recalculates one sale's commissions, milestones and overrides from its current data and the current pay plans. It is the same as pressing Recalculate on the sale in the web app. Use it after you change something a sale's pay depends on, such as a worker's position or a pay plan, when the sale itself did not change.

**Before you call:**

- You do not need this after `POST /v1/sales`. A sale you send is recalculated as part of processing.
- URL-encode the pid. A pid that contains `/` cannot be used here.
- The sale needs a closer. Not available to SequiPay companies (`403`).
- Send no body. Anything you send is ignored.

**What happens:**

- Sequifi recalculates the sale and answers when it is done, usually within a few seconds.
- Amounts already paid are never edited in place. If a recalculated amount differs from what was paid, the difference is recorded the same way as for a recalculation in the web app.
- If the sale's final milestone was paid more than three months ago, that milestone is not re-evaluated. Change it in the web app if you need to.
- Some tiered sales are priced only when their tier period closes. For those the call returns `200` with `skipped: true` and changes nothing.
- Recalculation is refused while a payroll that includes the sale is being finalized or paid (`409`). Retry after the payroll completes.

**Limits (per company):**

- One recalculation at a time. A call made while another is running returns `429` with `data.code` `RECALCULATION_IN_PROGRESS`.
- The same sale can be recalculated once every 10 minutes. An earlier call returns `429` with `data.code` `COOLDOWN` and `data.last_run`, which says when the last run started and how it ended.
- At most 20 calls a minute and 300 an hour, including calls refused with `404`, `409` or `422`. Over the limit returns `429` with `data.code` `RATE_LIMITED`.
- Every `429` and `503` carries a `Retry-After` header and `data.retry_after_seconds`. Wait that many seconds before trying again. To recalculate many sales, send them one at a time and wait for each answer.

**Idempotency and retries:**

- Recalculating twice gives the same result, so a retry is safe.
- After a `504`, the recalculation may still finish. Retry the same call: it returns `429` `COOLDOWN`, and `data.last_run.outcome` says whether the first attempt landed (`recalculated`, `skipped`, `failed`, or `running` if it has not finished).

**Errors:**

- `400` `pid is required`, or the pid is longer than 255 characters.
- `403` with `data.code` `NOT_AVAILABLE`: SequiPay company.
- `404` with `data.code` `SALE_NOT_FOUND`: no sale with that pid. A sale sent with `POST /v1/sales` appears only after processing.
- `409` with `data.code` `PAYROLL_IN_PROGRESS` (retry later) or `RECON_FINALIZED` (the sale has finalized reconciliation amounts and can no longer be recalculated).
- `422` with `data.code` `CLOSER_MISSING`: assign a closer first.
- `429`: one of the limits above. Read `data.code` and `Retry-After`.
- `500` with `data.code` `RECALCULATION_FAILED`, or `Could not resolve the tenant host`. Contact Sequifi support if it repeats.
- `502` `The tenant returned an unreadable response` or `Could not reach the tenant`: retry later.
- `503` with `data.code` `LIMITER_UNAVAILABLE`: temporarily unavailable, retry after `Retry-After`. A `503` with no `data.code` means your company is not set up for this endpoint yet. Contact Sequifi support.
- `504`: no answer in time. See retries above.
- Errors passed back from Sequifi carry `data.code`, `data.pid` and `data.requestedBy`.
- `401` when the token is missing, malformed, expired or revoked. It is rejected before the request is processed, so the body is only `{"message":"Unauthorized"}`, with no `status` or `data`.

**Related:**

- `GET /v1/sales/{pid}` to read the sale and its commission lines after the recalculation.

## Path parameters

- `pid` (string, **required**): The sale's pid, as sent with `POST /v1/sales` and returned by `GET /v1/sales`. URL-encoded. Max length: 255. Example: `16653`.

## Example request

```bash
curl -X POST "https://marketplace-api.sequifi.com/v1/sales/16653/recalculate" \
  -H "Authorization: Bearer $SEQUIFI_TOKEN"
```

## Responses

### 200: Recalculated, or skipped because the sale is priced when its tier period closes

- `status` (integer): The HTTP status code, repeated. Example: `200`.
- `message` (string): Short text. Wording varies by endpoint; branch on the HTTP status, not on this text. Example: `success`.
- `data` (object)
  - `requestedBy` (string): Name of the API credential your token belongs to.
  - `pid` (string): The sale.
  - `recalculated` (boolean): True when the sale was recalculated.
  - `skipped` (boolean): True when the sale is priced at tier period close and nothing changed.
  - `duration_ms` (integer): How long the recalculation took, in milliseconds.
  - `next_allowed_at` (string (date-time)): Earliest time this sale can be recalculated over the API again.

Example (recalculated): Recalculated

```json
{
  "status": 200,
  "message": "Sale recalculated.",
  "data": {
    "requestedBy": "acme",
    "pid": "16653",
    "recalculated": true,
    "skipped": false,
    "duration_ms": 4312,
    "next_allowed_at": "2026-10-09T15:10:00+00:00"
  }
}
```

Example (skipped): Priced at tier period close

```json
{
  "status": 200,
  "message": "Skipped: this sale is priced by the retroactive-tier period-close engine, so Recalculate does not apply to it.",
  "data": {
    "requestedBy": "acme",
    "pid": "16653",
    "recalculated": false,
    "skipped": true,
    "duration_ms": 38,
    "next_allowed_at": "2026-10-09T15:10:00+00:00"
  }
}
```

### 400: Missing or over-long pid

```json
{
  "status": 400,
  "message": "pid is required",
  "data": {}
}
```

### 401: Missing, malformed, expired or revoked token. Rejected before processing, so the body has only `message`.

```json
{
  "message": "Unauthorized"
}
```

### 403: Not available for SequiPay companies

```json
{
  "status": 403,
  "message": "Sale recalculation is not available for SequiPay companies.",
  "data": {
    "requestedBy": "acme",
    "code": "NOT_AVAILABLE",
    "pid": "16653"
  }
}
```

### 404: No sale with that pid

```json
{
  "status": 404,
  "message": "No sale with pid 16653.",
  "data": {
    "requestedBy": "acme",
    "code": "SALE_NOT_FOUND",
    "pid": "16653"
  }
}
```

### 409: A payroll with this sale is being finalized or paid, or its reconciliation is finalized

Example (payroll_in_progress): Payroll in progress (retry later)

```json
{
  "status": 409,
  "message": "A payroll that includes this sale is being finalized or executed. Try again after it completes.",
  "data": {
    "requestedBy": "acme",
    "code": "PAYROLL_IN_PROGRESS",
    "pid": "16653"
  }
}
```

### 422: The sale has no closer

```json
{
  "status": 422,
  "message": "This sale has no closer. Assign a closer before recalculating it.",
  "data": {
    "requestedBy": "acme",
    "code": "CLOSER_MISSING",
    "pid": "16653"
  }
}
```

### 429: A limit was reached. Wait for `Retry-After` seconds.

Example (cooldown): Same sale recalculated within the last 10 minutes

```json
{
  "status": 429,
  "message": "This sale was recalculated over the API recently. It can be recalculated again in 412 seconds.",
  "data": {
    "requestedBy": "acme",
    "code": "COOLDOWN",
    "pid": "16653",
    "retry_after_seconds": 412,
    "last_run": {
      "started_at": "2026-10-09T15:00:00+00:00",
      "finished_at": "2026-10-09T15:00:04+00:00",
      "outcome": "recalculated"
    }
  }
}
```

### 500: The sale could not be recalculated

```json
{
  "status": 500,
  "message": "The sale could not be recalculated.",
  "data": {
    "requestedBy": "acme",
    "code": "RECALCULATION_FAILED",
    "pid": "16653"
  }
}
```

### 502: Sequifi could not complete the request. Retry later.

```json
{
  "status": 502,
  "message": "Could not reach the tenant",
  "data": {
    "pid": "16653"
  }
}
```

### 503: Temporarily unavailable (retry after `Retry-After`), or your company is not set up for this endpoint

Example (unavailable): Temporarily unavailable

```json
{
  "status": 503,
  "message": "Sale recalculation over the API is temporarily unavailable. Try again shortly.",
  "data": {
    "requestedBy": "acme",
    "code": "LIMITER_UNAVAILABLE",
    "pid": "16653",
    "retry_after_seconds": 60
  }
}
```

### 504: No answer in time. The recalculation may still finish; retry and read `data.last_run.outcome`.

```json
{
  "status": 504,
  "message": "The tenant did not respond in time. The recalculation may still finish. Retry this call: while the sale is cooling down it returns 429 with data.last_run.outcome, which says whether the first attempt landed.",
  "data": {
    "pid": "16653"
  }
}
```
