# API versioning and deprecation policy

Status: Accepted 2026-10-09 (issue #171). Applies to the HTTP API described by its OpenAPI document, served at `/developers/openapi.json`.

## Versions

The API has one version, **v1**: every path starts with `/v1/` and the OpenAPI document is served at
`/developers/openapi.json`. A v1 client keeps working for the whole life of v1. A change that would break a v1
client goes through the deprecation process below, or waits for a v2. A v2 would get its own path prefix (`/v2/`) and
its own OpenAPI document, and v1 would keep running beside it for at least the minimum notice below.

What changed and when is in [CHANGELOG.md](CHANGELOG.md).

## What v1 promises

A change is **additive** (allowed in v1 at any time, listed in the changelog) when a correct v1 client cannot notice it
except by gaining something:

- a new operation (path or method);
- a new optional request parameter or request body field;
- a new response field (except in an object the document closes, see below);
- a new value of a response enum (for example a new status or event type);
- a new error response code where the request was already refused, or a more specific error message;
- a looser request limit (a longer maximum length, a larger page size).

A change is **breaking** when it can make a correct v1 client fail:

- removing an operation, or asking for credentials (or other credentials) where none were needed;
- a new required parameter or body field, or making an optional one required;
- refusing a value that was accepted: a removed request enum value, a narrower type, a tighter limit, a changed
  pattern, a field a closed request object no longer accepts;
- a success status or a media type that is no longer returned;
- a response field that may now be missing, or may now hold another type (for example `null`);
- a new field in a response object the document closes (`additionalProperties: false`): a client generated from it
  may refuse a field it does not know;
- changing what a field or a status means.

CropPath checks these rules mechanically: each change to the OpenAPI document is compared with the previous version of
the document, and a breaking change fails the check. One is let through only after a reviewer has accepted it, as a
correction recorded in the changelog or as the removal of an operation whose sunset has passed. Meaning changes are not
visible to a check; reviewers check them.

## What clients must do

To benefit from additive changes without breaking, a v1 client:

- ignores response fields it does not know;
- treats an unknown enum value as "other" instead of failing (log it, do not crash);
- does not depend on the order of object fields or on the exact error message (match on HTTP status and `code`);
- follows `Retry-After` on 429 and 503;
- watches for the `Deprecation` and `Sunset` headers below.

## Deprecation

A breaking change to v1 is made in two steps:

1. **Deprecate.** The operation (or the old behaviour) is marked deprecated: `deprecated: true` in the OpenAPI
   document, an entry in the changelog naming the replacement, and the route listed in the server's deprecation
   table. From then on every response of that route, errors included, carries:
   - `Deprecation: @<unix seconds>` ([RFC 9745](https://www.rfc-editor.org/rfc/rfc9745)), the start of the deprecation
     day (UTC);
   - `Sunset: <HTTP-date>` ([RFC 8594](https://www.rfc-editor.org/rfc/rfc8594)), when it stops working;
   - `Link: <changelog>; rel="deprecation", <changelog>; rel="sunset"`: the changelog the developer portal serves, or
     the entry itself when one is linked.
2. **Remove** at the sunset, in a change of its own, recorded in the changelog.

A way of calling an operation that stays, such as request fields replaced by others, is deprecated in the same two
steps without deprecating the operation: the fields are marked `deprecated: true`, the changelog entry names the
replacement and the sunset, and every answer to a request that uses the deprecated way, errors included, carries the
same three headers, the link naming that entry; other requests to the operation carry none. The first such case is
the address in Viet Nam given as text only (sunset 1 May 2027).

**Minimum notice: 180 days** between the deprecation and the sunset. The backend refuses to start with a deprecation
that gives less notice, has no https link or names a route that does not exist.

A security fix may shorten the notice when keeping the old behaviour would expose data; the changelog says so and why.

## Authentication of integrations

Company integrations authenticate with **OAuth2 client credentials** (RFC 6749 §4.4): a tenant admin, signed in,
registers an API client (`POST /v1/oauth-clients`; the secret is shown once), the integration exchanges its
client id and secret at `POST /v1/oauth/token` for a bearer access token valid for 15 minutes, and sends it as
`Authorization: Bearer <access_token>`. A token carries scopes, the names of the API permissions (route policies) it may
use, and an operator client may be limited to some facilities. Revoking the client ends all its tokens at once;
`POST /v1/oauth/revoke` (RFC 7009) revokes one token. The older API tokens (`agt_...`) keep working and are covered by
this same policy.

Client credential rules (#205):

- **Expiry.** Every API client expires 365 days after it is registered (clients registered before the rule got 365
  days from the day the server was upgraded). An expired client is refused at the token endpoint with
  `invalid_client`, like a wrong secret, and its live tokens stop working at once (401). A company admin renews it
  with `POST /v1/oauth-clients/{id}/renew` (`expires_in_days`, 1 to 730, default 365, counted from the renewal; it
  works on an expired client, not on a revoked one).
- **Allowed sources.** A client may be limited to 1 to 20 IPv4 or IPv6 networks in CIDR form with
  `PUT /v1/oauth-clients/{id}/allowed-ips` (null: any source). The token endpoint, the revocation endpoint and every
  call made with the client's tokens check the address the API sees: `CF-Connecting-IP` from a proxy listed in
  `AGRITRACE_TRUSTED_PROXIES`, else the connection's address (the same address the rate limits use). From any other
  source the client gets `invalid_client` and its tokens 401 `missing_or_invalid_token`, exactly like a wrong
  credential, so a refusal never reveals the list.
- **Last use.** Each client records when it last got a token or called the API, and a keyed hash of the source
  address (never the address). Calls are recorded at most once a minute per client.
- **Write scopes.** A client holding a scope that opens a write (a route other than GET) is flagged `broad_scope`.
  Scopes are fixed when the client is registered; a wider client is a new one.
- **Recent password.** Registering, renewing and changing the allowed sources need a password confirmed in the last 10
  minutes (#183).
- **Revocation** marks the client's live tokens revoked in the same transaction.
- `GET /v1/oauth-clients/details` shows all of this per client. Each change writes exactly one audit row
  (`oauth_client.create`, `.renew`, `.allowed_ips`, `.revoke`), never with a secret or an address: the allowed
  sources are recorded as a count and a keyed hash of the list (an HMAC under the server's hash secret, so
  enumerating networks never reverses it).

These three operations are new in v1 and not in the partner SDK 0.2.0 (`x-not-in-sdk`); they are console operations,
for a signed-in session only.

Scopes. A scope is the name of one route permission and implies no other: a token opens an
operation only when it holds that operation's scope and the client's role allows the operation; anything else is 403
`role not permitted` (the same answer as a role refusal). The OpenAPI document lists every scope in the `oauth2`
security scheme (client credentials flow, token URL `/v1/oauth/token`) and names, on each operation an API client may
call, the one scope it needs (`security: [{"oauth2": ["<scope>"]}]` beside the session and bearer alternatives).
Two scopes are dedicated by the owner's least-privilege decision on #171 and are granted only by name, never through
`lot_write`, `lot_read` or any other scope:

| Scope | Operations | Roles that may hold it |
|---|---|---|
| `epcis_capture` | `POST /v1/epcis/capture` (#167) | tenant_admin |
| `records_export` | `GET /v1/lots/{lot}/records-export.csv`, `.json`; `GET /v1/records-export.csv`, `.json` (#152) | tenant_admin, auditor |

A signed-in session and an API token (`agt_...`) are not scoped: they keep the role rules of these routes unchanged.
