# API Integration Guide — read-only data collection

Everything an external platform needs to collect data from this ERP and integrate it with its own
system. This guide mirrors **Settings → API Docs** in the dashboard (same content, authenticated
only — the endpoint inventory and guideline are never published anonymously).

## 1. What you get

A **GET-only** integration surface under:

```
https://<erp-host>/api/v1/integration           (legacy alias: https://<erp-host>/api/integration)
```

There is **no create / update / delete** on this surface — not restricted for you, simply not
implemented. A leaked key can read data; it can never mutate anything.

| Endpoint | Returns | Owner permission required |
| --- | --- | --- |
| `GET ping` | Key validity, name, rate-limit info | (ability only) |
| `GET branches` | Branch list | `settings.view` |
| `GET customers` | Customer directory | `customers.view` |
| `GET products` | Inventory catalog | `inventory.view` |
| `GET sales-orders` | Sales orders | `sales.view` |
| `GET customer-dues` | Open dues (paid < amount) | `customers.view` |
| `GET customer-payments` | Payment history | `customers.view` |

Shared query parameters on list endpoints:

- `per_page` — 1–200 (default 50; hard-capped at 200).
- `updated_since` — ISO-8601 timestamp; only rows changed after it are returned (efficient polling).
- Resource filters: `customers`/`sales-orders`/`products` accept `search`/`id`/`status`/`sku`,
  `sales-orders` also `order_no` + `customer_id`, payments/dues accept `customer_id`.

## 2. Getting a key (least privilege)

1. An administrator opens **Settings → API Docs** (dashboard; the docs tab and this guide are
   authentication-gated and require the `settings.manage` permission).
2. Creates a key named after your platform (e.g. `partner-crm`), with a validity (1–365 days,
   default 30). The plain token is shown **once**.
3. The key carries exactly one Sanctum ability: `integration.read`. Nothing else.
   - Tokens issued by `POST /api/v1/login` (dashboard sessions) carry the user's dashboard
     permissions — they do **not** work here even though they authenticate fine elsewhere. That is
     deliberate least-privilege: a dashboard token pasted into an external system is rejected with
     403.
4. The key inherits its **owner's** view permissions and **branch** scope: a key minted from a
   non-admin account sees only that branch's records; admins may additionally filter by `branch_id`.
5. Before expiry: mint a replacement, swap it into your platform, revoke the old key in the same
   screen. Revocation is immediate.

## 3. Calling the API

```bash
curl "https://<erp-host>/api/v1/integration/customers?per_page=100&updated_since=2026-01-01" \
  -H "Authorization: Bearer <integration-key>"
```

Responses are Laravel paginator envelopes:

```json
{
  "data": [ { "id": 42, "name": "…", "email": "…", "phone": "…", "branch_id": 1, … } ],
  "meta": { "current_page": 1, "last_page": 7, "per_page": 100, "total": 642 },
  "links": { "…": "…" }
}
```

Field discipline: every endpoint returns an **explicit column whitelist** — no password hashes, no
token material, no internal configuration. New database columns never leak into the surface
automatically.

## 4. Transport, rate limits and errors

- **HTTPS only in production.** Plain HTTP is answered `426 Upgrade Required`, never redirected.
- **Rate limits:**
  - Global burst: 240 requests/minute per IP (then 429 + temporary IP block on repeats).
  - **Per key: 180 requests/minute** (`API_TOKEN_RPM`) — one busy key cannot consume a shared
    office IP's whole budget.
  - Respect `Retry-After` on 429.
- **Auth/session tokens expire after 60 minutes** (dashboard sessions; integration keys use their
  own issue-date-based TTL as issued).
- Auth failures, permission denials, rate-limit events and every unauthenticated hit are logged
  server-side (laravel.log + security_logs) and correlated by IP — never rendered to the caller.
- Error codes: `401` missing/invalid/expired token · `403` ability or permission denied ·
  `404` unknown record (also for other branches' records) · `422` bad parameters ·
  `426` non-HTTPS call · `429` rate limited.
- All `/api/*` responses are JSON in every environment, including errors.
- Input scanning: injection/XSS-shaped payloads are rejected `400` before controller code runs.

## 5. What the integration surface intentionally does not have

- No POST/PUT/PATCH/DELETE — data collection only. Submissions (orders, tickets, forms) stay inside
  the authenticated dashboard or the throttled public portals.
- No full endpoint inventory without login: the complete reference lives in
  **Settings → API Docs** (`GET /api/v1/integrations/api-docs`, requires `settings.manage`).
- No secret values in URLs and no static "secret URLs": public one-off links (payment proofs, file
  shares, surveys) are single-purpose random tokens, revocable from the dashboard.

## 6. Versioning

The canonical base is `/api/v1`. The unversioned `/api/*` path remains as an exact alias for
existing clients (legacy). Breaking changes ship only in a new `/api/vN` prefix; `v1` contracts are
additive-only.
