# Good practice

> Build integrations that read little, read rarely and stop when the API says so, without slowing Fledge down for its users.

The API reads and writes your company's own Fledge database: the one the people in the company work in all day. An integration that asks too much slows Fledge down for everyone, or reaches the [rate limits](https://docs.fledge.nl/en/api/errors-and-limits#rate-limits).

Writing code with an AI assistant? The Fledge MCP server gives it these rules as a skill, see [Build with an AI assistant](https://docs.fledge.nl/en/api/ai-assistant).

## Read

- **Ask for totals, not rows.** Counts, sums and charts come from `$apply` with `groupby` and `aggregate`: the API computes them, and the answer is a few rows. See [Reading data](https://docs.fledge.nl/en/api/reading-data#totals-instead-of-rows).
- **Only the fields you use.** Always `$select`. `$expand` only with its own `$select`, and a list only with a `$top`.
- **Filter in the API.** Filter on the period, the status or the key you need, and always on `administration_id`. Never read a table to filter it in your code.
- **Small pages.** Follow `@odata.nextLink`, or use `$top` up to 100 with `$skip` and an `$orderby` on the key.
- **One request at a time.** Wait for an answer before the next request. Don't loop over customers or articles with a request each: filter or group on them in one request.

## Don't copy Fledge

There is no reliable way to read only the rows that changed since the last time. So a "sync" that copies tables and polls for changes reads everything again and again.

- Read the data a screen or a job needs, for the period it shows, when it needs it.
- Keep an answer for as long as it can't change: closed periods for a day or longer, articles and relations for hours, open work for minutes.
- A job that refreshes runs every 15 minutes at most, or at night. Never poll in a loop.
- A copy for reporting or BI is not a job for the API. Ask Fledge for an export.

## Tokens and errors

- **Reuse the access token** until it expires or the API answers `401`. See [Authentication](https://docs.fledge.nl/en/api/authentication).
- **On `429` or `503`**, wait as long as `Retry-After` says, else back off: one second, then double.
- **On a `5xx`**, retry at most a few times, then stop and report it.
- **Never retry a `4xx`** (except `401` once with a new token, and `412` after reading the row again): the request is wrong.

## Before you go live

- Every request has `$select`, and a filter on `administration_id` where the set has it.
- No loop with a request per row, and no requests in parallel.
- Answers are cached with a lifetime; no polling more often than every 15 minutes.
- The token is reused; `429`, `503` and `5xx` are handled as above.
- The API key is in a secret store, and its user has only the licenses the integration needs.
- The integration uses the stable version URL, and someone plans the move to each new stable version every few weeks (see [Versions](https://docs.fledge.nl/en/api/getting-started#versions)).

---

Source: https://docs.fledge.nl/en/api/good-practice
