# API overview

SQL Flow serves two API formats side by side. The **v2 API** under `/v2/api/` uses the official SQL Account API's resource names and request shapes, so a developer who knows the hosted API knows this one. The **v1 API** under `/api/` is the earlier format kept for existing integrations. This page describes v2.

## Base URL and authentication

The API runs on the machine with SQL Accounting, port 5080 unless you chose another at setup:

```
http://<machine>:5080/v2/api/<resource>
```

Every request carries a token from the console's **Tokens** page:

```
Authorization: Bearer <token>
X-Company: <company code>
```

A token is **Read** or **Read and write**, and can be limited to companies, paths and IP addresses. `X-Company` names the company the request is for, by the code you gave it in the console. A read may send `X-Company: *` to query every company the token may see in one call; each row then carries its `company`.

## Resources

There are 69 resources, named as the official API names them: `customer`, `supplier`, `agent`, `stockitem`, `stockgroup`, `salesquotation`, `salesorder`, `deliveryorder`, `salesinvoice`, `salescreditnote`, `salesdebitnote`, `customerpayment`, `customerrefund`, `purchaseorder`, `goodsreceived`, `purchaseinvoice`, `supplierpayment`, `journalentry`, `paymentvoucher`, `receiptvoucher`, `stockadjustment`, `stocktransfer`, `account`, `tax`, `terms`, `currency`, `location`, `project`, `profile` and the rest. The console's API explorer lists them all with their fields.

| Call | What it does |
|---|---|
| `GET /v2/api/customer?AREA=KL` | a page of rows; `limit` (default 100, max 500), `offset`, and any of the resource's fields as an exact filter |
| `GET /v2/api/customer/300-A001` | one row by its key (code, or document number) |
| `GET /v2/api/salesinvoice/recent?since=2026-11-01` | documents changed since a time |
| `POST /v2/api/salesinvoice` | create a document |
| `PUT /v2/api/salesinvoice/IV-1041` | update it |
| `DELETE /v2/api/salesinvoice/IV-1041` | delete it |
| `GET /v2/api/salesinvoice/IV-1041?format=pdf` | the document as a PDF |
| `GET /v2/api/customer/300-A001/outstanding` | what the customer owes, with `aging` and `statement` alongside |
| `GET /v2/api/customer/aging` | every customer's aging in one call |
| `GET /v2/api/stockaging?dateto=2026-11-30` | FIFO stock aging; `stockbalanceinquiry`, `stockcard` and eight more reports |
| `GET /v2/api/einvoice?status=pending` | documents by MyInvois status |
| `GET /v2/api/changes?since=...` | everything that changed, across resources |

Answers are JSON. A list answer has `data` and `pagination` (`offset`, `limit`, `count`); one document answers `data` with its lines in `sdsdocdetail`. Field names are SQL Accounting's own, in lower case in answers (`code`, `companyname`, `docno`, `docdate`, `docamt`, and on each line `itemcode`, `qty`, `unitprice`). Dates are sent as `YYYY-MM-DD` and answered as `YYYY-MM-DDT00:00:00`.

## Writes

`POST` creates a record and `PUT /<key>` changes it; a document's key may be its number, as in `PUT /v2/api/salesinvoice/IV-1041`. The body is JSON with SQL Accounting's field names (in any case), a document's lines in `sdsdocdetail`, true and false as JSON booleans and dates as `YYYY-MM-DD`. A `PUT` with lines replaces all the old ones. Send your own `DOCNO`, or leave it out for SQL Accounting's next number.

```json
{
  "CODE": "300-A001",
  "DOCNO": "IV-1041",
  "DOCDATE": "2026-11-03",
  "sdsdocdetail": [
    { "ITEMCODE": "ANT", "QTY": 2, "UNITPRICE": 120, "TAX": "SV-8" }
  ]
}
```

Setting `CODE` fills in the customer's details, and setting `ITEMCODE` the item's price, unit and tax; anything else you send replaces them. Before anything reaches SQL Accounting, the request is checked: the customer or supplier, the items and the tax codes must exist (for a journal, the accounts), and a journal must balance. A refused write answers 422 with the reason, and nothing is saved. There is no single create-or-update call: read the record by its key, and `POST` when that answers 404.

**Idempotency.** Send an `Idempotency-Key` header (any string unique to the operation, such as your order number) and a retried request answers the stored result instead of posting twice; the replay carries `Idempotent-Replayed: true`. Keys are kept for 24 hours per token.

**Knock-offs.** Create the payment, then apply it with its own call: `POST /v2/api/customerpayment/OR-0001/knockoff` with `{ "knockoff": [{ "doctype": "IV", "docno": "IV-1041", "amount": 240 }] }`. A smaller amount leaves the rest of the invoice open, and one call may knock off several documents. Every entry is checked first (the document exists, belongs to the same customer and has that much open, and the total is no more than the payment has unapplied); a failed check answers 422 and nothing is written. `GET` on the same path lists what the payment has knocked off, and `DELETE .../knockoff/IV-1041` releases one. Customer refunds and credit notes, and supplier payments, refunds and credit notes, work the same way.

**Batches.** `POST /v2/api/<resource>/batch` with a JSON list of up to 200 records creates each one and answers a result per record; one that fails stops nothing. Add `?dryRun=1` to run every check and save nothing. Batches are not in the free trial.

## Errors

| Status | Meaning |
|---|---|
| 400 | the request is malformed: not JSON, a bad date or number, or an unknown filter |
| 402 | a free-trial limit: the 100 calls are used up, or a PDF or batch, which the trial leaves out |
| 401 | no token, or an unknown or expired one |
| 403 | the token may not do this: wrong scope, company, path or address |
| 404 | no such resource or row |
| 409 | the change feed is off for this company; turn it on in the console |
| 422 | a write was refused: a check failed (an unknown field, customer, item or tax code, an unbalanced journal, a knock-off larger than what is open) or SQL Accounting refused the save; nothing was saved |
| 503 | SQL Accounting is not connected right now; check the console's Status page |

Every error body carries `error` with the reason, for example `{ "error": "There is no stock item 'X1'." }`. A free-trial limit puts a code in `error` and the reason in `message`.

## Logs and audit

The console's **Logs** page shows every request with its token, path, status and time; **Writes only** narrows it to what changed data. The same log is available at `GET /v2/api/audit` for a system that wants to keep its own copy.
