# Coding practices

These conventions apply to the APIs in the [catalog](/apis/). The API reference of each operation gives its exact parameters and response.

## JSON

Request and response bodies are JSON:

```http
Content-Type: application/json
Accept: application/json
```

Error bodies may use `application/problem+json`. See [Errors](/getting-started/errors).

## Pagination

List operations that page their results take two query parameters:

| Parameter | Default | Maximum | Description |
|  --- | --- | --- | --- |
| `pageNumber` | `1` | — | Page to return, starting at 1 |
| `pageSize` | set per operation (often `50`) | `100` | Items per page |


A paged response looks like this:

```json
{
  "items": [],
  "totalCount": 1250,
  "pageNumber": 1,
  "pageSize": 100,
  "totalPages": 13,
  "hasNextPage": true,
  "hasPreviousPage": false
}
```

Read pages until `hasNextPage` is `false`:

```python
page = 1
items = []

while True:
    response = session.get(
        "https://apis.traceone.com/reference-data/v2/allergens",
        params={"pageNumber": page, "pageSize": 100},
    )
    response.raise_for_status()
    data = response.json()
    items.extend(data["items"])

    if not data["hasNextPage"]:
        break
    page += 1
```

A list with no results answers `200 OK` with an empty `items` array, never `404`. `404` means "this resource does not exist".

## Batch reads

To read many resources whose IDs you know, use the batch operation instead of one `GET` per ID:

- **Specification**: `POST .../collection` with up to **50 IDs**, for example
`POST https://apis.traceone.com/spec/v2/packaging/specifications/collection`.
The response is an array of the resources found.
- **Reference Data**: some resources take a `POST` with a list of IDs on the resource itself, for example
`POST https://apis.traceone.com/reference-data/v2/allergens`.


```http
POST https://apis.traceone.com/spec/v2/packaging/specifications/collection
Content-Type: application/json

{
  "specificationIds": [
    "3fa85f64-5717-4562-b3fc-2c963f66afa6",
    "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
  ]
}
```

For more IDs, split them into several calls.

## Search

Filtered searches are a `POST` to a `/search` resource, with the filters in the body and a paged response. The Specification search is in [preview](/getting-started/api-versioning#stages):

```http
POST https://apis.traceone.com/spec/v2/specifications/search
Content-Type: application/json

{
  "templateTypes": ["Food"],
  "statuses": ["Validated"],
  "pageNumber": 1,
  "pageSize": 50
}
```

## IDs

- Resource IDs are UUIDs. They never change.
- A resource owned by another API is referenced by its ID only. Read it from the API that owns it.


## Enums

Enum values are **PascalCase**, exactly as listed in the API reference:

```json
"status": "InProgress",
"templateType": "Packaging"
```

New values can be added to an enum without a new version. Handle a value you do not know (log it, use a default) instead of failing.

## New fields

Fields can be added to a response without a new version. Ignore the fields you do not use. Do not make your parser fail on an unknown field.

## Languages

Operations that return translated text take a `languageCode` query parameter, for example `languageCode=fr-FR`. Without it, the operation uses its default language (English). Each operation in the API reference lists the parameter when it supports it.

## Retries

| Call | Safe to retry? |
|  --- | --- |
| `GET`, search, batch read | Yes |
| `PUT` (full replace) | Yes: it sends the same full content again |
| Action `POST` (`share`, `link`) | Yes: running it again changes nothing |
| `POST` that creates a resource | **No**, not automatically: check whether the resource exists before you create it again |


Retry `429` and `5xx` answers with exponential backoff. Do not retry other `4xx` answers without changing the request. See [Rate limits](/getting-started/rate-limits).

## Best practices

| Do | Don't |
|  --- | --- |
| Reuse a token until it expires | Request a new token for each call |
| Use `pageSize=100` for bulk reads | Read page by page with a small page size |
| Use the batch operation for known IDs | Loop over `GET` calls one ID at a time |
| Stop when `hasNextPage` is `false` | Read pages until you get an empty one |
| Retry `429` with exponential backoff | Retry at once in a loop |


## Next steps

- [Writing to the Specification API](/getting-started/writing-to-specification) — updates, concurrency, refusals
- [Errors](/getting-started/errors) — status codes and the error body
- [Rate limits](/getting-started/rate-limits) — the limit and how to retry
- [Using Reference Data](/getting-started/using-reference-data) — lists, allergens, nutrients