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

# Posting Sales

How to send sales to Sequifi in bulk with `POST /v1/sales` so every record lands, and how to confirm that it did. Written for integrators and for AI agents running an integration: follow the rules in order. A reference script that applies every rule is published at https://apidocs.sequifi.com/examples/post_sales_bulk.py (Python 3.9+, no extra packages).

**What a success response means:** A `201` or `200` from `POST /v1/sales` means the records passed validation and were queued. Sequifi then works through the queue in the background: each sale is created or updated and its commissions are recalculated. Until that finishes, `GET /v1/sales` still returns the old values, or `404` for a brand-new pid.
Processing usually finishes within minutes. A large run, or one sent while other imports are running, can take an hour or more. A sale whose payroll is being finalized waits until that payroll completes and then processes on its own.

**Rule 1, send the complete sale every time:** Every record carries `pid`, `customer_name`, `customer_signoff` (the sale date, `YYYY-MM-DD`) and the fields your vertical requires, marked `*` on the Vertical Labels page (for example `gross_account_value` for Pest, Fiber and Roofing, and `kw` for Turf and Roofing). Send them on updates too, not only on new sales.
Why: An update to a sale Sequifi already has is merged into that sale, so a field you leave out keeps its current value. Sending the complete sale still keeps your system and Sequifi in step, and milestone dates always need the full list (below). `pid` is the key, so an update never creates a duplicate.
Send one `milestone_dates` entry for every milestone in the product's schedule, in order, with `milestone_date: null` for those not reached yet. Dates are saved by position, so sending only the milestone that changed moves its date onto the first milestone and clears the rest. The list replaces the sale's list: a milestone you leave out is treated as removed.

**Rule 2, turn on partial_success:** Set `"partial_success": true` at the top level of every request. Without it, one bad record rejects the whole request with `400` and nothing in the batch is queued. With it, the good records are queued and each bad one comes back in `data.rejected[]` with its reason. A rejected record is not saved, whether it is a new sale or an update; fix it and resend it. A duplicate `pid` inside one request still rejects the whole request, so remove duplicates first.

**Rule 3, batch and pace:** Put up to 200 records in each request. Send one request at a time and wait for its response before sending the next. Stay under 30 requests a minute to this endpoint: above that, the processing of a batch can wait until your next request. Do not pause between batches to wait for processing; queued batches are processed in the order they arrive. One request per record is the wrong shape for a bulk run.

**Rule 4, read every response:**
- `201` or `200`: compare `recordsProcessed` with the number you sent (a request where every record was rejected still returns `201`, with `recordsProcessed` of 0). Fix and resend each record in `data.rejected[]`. Check `data.warnings[]` for fields that were ignored, and fix your mapping. `warnings[]` lists a product that could not be matched, but not a closer or setter: check `closer1_id` in `data.sales[]`.
- `400`: nothing in the request was queued. Fix the records named in `data.errors` and resend the whole request.
- `429`, `500`, `502`, `503`, `504` or a timeout: resend the same request after a pause: wait 2, 4, 8 and 16 seconds between attempts. Resending is safe because `pid` is the key.

**Rule 5, confirm, and do not resend what is still queued:** After the last batch, wait (allow an hour for a large run), then call `GET /v1/sales/{pid}` for each pid and compare it with what you sent: `customer_signoff`, the product, and `data.sale.milestone_dates[]`. Do not resend a record only because it has not shown up yet. If you send the same pid again before the first copy is processed, the newer copy replaces it and goes to the back of the queue. If a sale still does not match a few hours later, send its pid to Sequifi support.

**Records rejected during processing:** A queued record can still be rejected when it is processed. The API does not report this today, so the check in Rule 5 is how you catch it. The causes:
- a milestone date earlier than `customer_signoff`;
- changing or removing a milestone date that has already been paid;
- removing or changing the product, or changing a closer or setter, after commission on the sale has been paid.
Make those corrections in Sequifi, or ask support to make them.

**Do not send:** `data_source_type` (leave it out); commission or paid amounts, which Sequifi calculates; flat fields such as `milestone_date` or `m1_payable_date`, which are ignored. Milestone dates go only in `milestone_dates[]`.
