# Max age and partial success

> How to use maxAge in the CNPJá API to choose between a live query and the cache, the separate ages per resource, partial success (206) and updatedAt.

> **Tip:** Concepts on this page: [Data freshness](https://cnpja.com/en-us/docs/data/freshness), [Max age](https://cnpja.com/en-us/docs/data/freshness#max-age), [Resources with their own rules](https://cnpja.com/en-us/docs/data/freshness#special-rules) and [Partial result](https://cnpja.com/en-us/docs/data/freshness#partial).

The `maxAge` parameter sets the [max age](https://cnpja.com/en-us/docs/glossary#idade-máxima--maximum-data-age) accepted for a request's data: a resource within it comes from the cache, and an older resource, or one never queried, goes through a [live query](https://cnpja.com/en-us/docs/glossary#consulta-em-tempo-real--live-query) at the source before the response. When a live query fails, the request can carry on and answer with `206`, as explained in [Partial success](#partial). The full technical specification is in [Data freshness](https://cnpja.com/en-us/docs/api/reference#description/data-freshness), in the Reference.

## Syntax

The `maxAge` value is 1 to 3 digits followed by a lowercase unit:

| Unit | Meaning | Example |
| --- | --- | --- |
| `y` | Years | `1y` |
| `w` | Weeks | `4w` |
| `d` | Days | `14d` |
| `h` | Hours | `8h` |
| `m` | Minutes, not months | `45m` |

- The default is `45d`, and the value ranges from `5m` to `1y`.
- Values outside the format or the range, such as `45D`, `1.5d`, `3m` or `2y`, are rejected with `400` `REQUEST_VALIDATION_FAILED`.
- To express months, use days or weeks: `90d` is about three months, while `3m` means three minutes.
- Public requests, without a key, accept no parameters and always use the `45d` default, as explained in [Public access](https://cnpja.com/en-us/docs/api/public-access).

Larger values, such as the default, suit record enrichment and bulk analysis; smaller values suit one-off checks, such as confirming a state registration before issuing an invoice. The trade-off between cost and freshness is in [Max age](https://cnpja.com/en-us/docs/data/freshness#max-age).

## Separate ages per resource

Some optional resources accept a separate age that replaces `maxAge` for that resource only. Without a separate age, `maxAge` applies.

| Parameter | Resource | Operations |
| --- | --- | --- |
| `company.taxRegime.maxAge` | Tax regime | Lookup CNPJ |
| `stateRegistry.maxAge` | State registrations | Lookup CNPJ, Lookup CPF |
| `suframa.maxAge` | SUFRAMA registrations | Lookup CNPJ, Lookup CPF |

Each separate age follows the same syntax as `maxAge` and is accepted only with its resource in `expand`, described in [CNPJ lookup](https://cnpja.com/en-us/docs/api/cnpj-lookup). Without it, the request is rejected with `400` `REQUEST_VALIDATION_FAILED`, and `errors` contains an item with the `DEPENDENCIES` code.

In this request, the CNPJ registration can be up to 30 days old, and state registrations up to 1 day old:

```http
GET /v2/establishments/37335118000180?maxAge=30d&expand=stateRegistry&stateRegistry.maxAge=1d HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY
```

## Operations with live queries

Only four operations accept `maxAge` and make live queries based on the age of the data:

| Operation | Refreshed live | Separate ages |
| --- | --- | --- |
| [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d) | CNPJ registration, company and partners, Simples Nacional, SIMEI and IBS and CBS regular regime options, state and SUFRAMA registrations | `company.taxRegime.maxAge`, `stateRegistry.maxAge`, `suframa.maxAge` |
| [Issue CNPJ dossier](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/dossier) | Each requested certificate | None |
| [Lookup CPF](https://cnpja.com/en-us/docs/api/reference#tag/individuals/GET/v2/individuals/%7btaxId%7d) | State and SUFRAMA registrations | `stateRegistry.maxAge`, `suframa.maxAge` |
| [Issue CPF dossier](https://cnpja.com/en-us/docs/api/reference#tag/individuals/GET/v2/individuals/%7btaxId%7d/dossier) | Each requested certificate | None |

For the CNPJ registration, the age counts from the most recent update, either an open data release or a live query. A CNPJ not yet in the cache is always queried live at the Receita Federal in requests with a key, whatever the `maxAge`. Stale resources are queried in parallel. In dossiers, `maxAge` applies to each requested certificate, as explained in [Certificates](https://cnpja.com/en-us/docs/api/certificates).

### Cached-only operations

These operations accept no `maxAge` and make no live queries:

- **Lookup company and Lookup person:** to update a company and its partners, look up one of the company's CNPJs with Lookup CNPJ. A person's participations follow the lag described in [Current holdings](https://cnpja.com/en-us/docs/data/partners-and-qsa#current-only).
- **Search CNPJ and Search people:** they use the cache, which combines CNPJ open data with the results of live queries, as explained in [What is in the cache](https://cnpja.com/en-us/docs/data/freshness#cached).
- **Lookup CEP and Search CEP:** they follow the DNE editions, as explained in [CEPs and addresses](https://cnpja.com/en-us/docs/api/postal-codes).

### Parameters with their own rules

| Resource or parameter | Rule in the API |
| --- | --- |
| `address.coordinates` | Does not follow `maxAge`. While the address cannot be located, it comes with `latitude` and `longitude` set to `null` and is recalculated on every request that asks for it |
| Render CNPJ map and Render CNPJ street view | Accept no `maxAge`: every request generates a new image from the cached address. See [Location and maps](https://cnpja.com/en-us/docs/api/location) |
| `company.taxRegime.view=DETAILED` | The age counts only from the latest live query at the Simples Nacional. Use it with a short `company.taxRegime.maxAge` when the `SIMPLES_NACIONAL_HYBRID` classification is decisive. See [Tax regime](https://cnpja.com/en-us/docs/api/tax-regime) |
| `stateRegistry.view=SUMMARY`, on a CNPJ | Follows the age of `stateRegistry.updatedAt`, and only the establishment's state is queried live |
| `stateRegistry.view=DETAILED` | Each state in `stateRegistry.states` has its age checked and, when needed, is queried live separately |
| `stateRegistry`, on a CPF | With `SUMMARY` and an already known registration, a single query refreshes every state; with `DETAILED`, or with no known registration, each state is queried separately. See [State registrations](https://cnpja.com/en-us/docs/api/state-registrations) |
| `RFB_REGISTRATION` and `RFB_PARTNERS` | Come from the same live query and are refreshed only by live queries, including those made by Lookup CNPJ, not by open data |

The reason for each rule is in [Resources with their own rules](https://cnpja.com/en-us/docs/data/freshness#special-rules).

> **Important:** with `stateRegistry.view=DETAILED`, each requested state with no registration is queried live on every request, whatever the max age, and with `stateRegistry.states=ALL` this applies to all 27 states. Request only the states you need. The consumption is in [CNPJ lookup](https://cnpja.com/en-us/legal/metering#api-cnpj), in the Metering Rules.

## Partial success (206)

Each live query has up to 30 seconds to complete. When it fails or does not complete in time, the request is not aborted: the other resources are delivered as usual, and the response comes with status `206` ([partial success](https://cnpja.com/en-us/docs/glossary#sucesso-parcial--partial-success)) instead of `200`. Only the four operations that accept `maxAge` can return `206`.

In a `206` response:

- The body has the same format as a `200` response, including when it is a PDF file.
- The affected resource keeps its cached data, which can be older than the requested limit. If it was never queried, it comes back empty.
- In dossiers, affected certificates appear in their cached version, or are left out of the PDF when none exists.
- The affected resource is not charged, even when the response includes its cached data, as stated in the [Metering Rules](https://cnpja.com/en-us/legal/metering#not-charged). The others are charged as usual.

> **Important:** treat `206` as success. A client that only accepts `200` discards valid responses.

The response does not indicate which resource failed. To identify possibly outdated resources, compare each one's `updatedAt` with the limit that applies to it, as described in [Last update date](#last-update-date), and retry later to get fresh data.

This example accepts `200` and `206` and checks whether the state registrations meet the 1-day limit requested in `stateRegistry.maxAge`:

```js
const response = await fetch(
  'https://api.cnpja.com/v2/establishments/37335118000180?expand=stateRegistry&stateRegistry.maxAge=1d',
  { headers: { Authorization: process.env.CNPJA_API_KEY } },
);

if (response.status !== 200 && response.status !== 206) throw new Error(`HTTP ${response.status}`);

const establishment = await response.json();
const updatedAt = establishment.stateRegistry.updatedAt;
const isStale = !updatedAt || Date.now() - new Date(updatedAt).getTime() > 24 * 60 * 60 * 1000;
```

Partial success needs cached data to fall back on. When none exists, as on [a CNPJ's first lookup](https://cnpja.com/en-us/docs/data/freshness#first-lookup), a failure at the source makes the request fail instead of returning `206`. In that case, retry later, as explained in [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors).

## Last update date

Each resource reports its last update in its own `updatedAt` field, a date and time in UTC such as `2026-01-27T03:00:00.000Z`:

| Field | Operations | Last update of |
| --- | --- | --- |
| `updatedAt` | Lookup CNPJ, Lookup company, Lookup person, and the CNPJ and people searches | Receita Federal data |
| `company.taxRegime.updatedAt`, or `taxRegime.updatedAt` on Lookup company | Lookup CNPJ, Lookup company, Search CNPJ | Tax regime, from the Simples Nacional |
| `stateRegistry.updatedAt` | Lookup CNPJ, Lookup CPF | Last live query in any state |
| `stateRegistry.registrations[].updatedAt` | Lookup CNPJ, Lookup CPF | Each state registration, which can be earlier than `stateRegistry.updatedAt` |
| `suframa.updatedAt` | Lookup CNPJ, Lookup CPF | SUFRAMA registrations |
| `suframa.registrations[].updatedAt` | Lookup CNPJ, Lookup CPF | Each SUFRAMA registration |
| `updatedAt` | Lookup CEP, Search CEP | Correios data (DNE) |

Lookup CNPJ response with `expand=company.taxRegime,stateRegistry`, trimmed to the update dates:

```json
{
  "updatedAt": "2026-01-27T03:00:00.000Z",
  "taxId": "37335118000180",
  "company": {
    "taxRegime": { "updatedAt": "2026-01-27T03:00:00.000Z" }
  },
  "stateRegistry": {
    "updatedAt": "2026-01-27T03:00:00.000Z",
    "registrations": [
      { "updatedAt": "2026-01-27T03:00:00.000Z", "number": "936389130", "state": "SP" }
    ]
  }
}
```

- To check whether a resource came within what was requested, compare its `updatedAt` with the limit that applies to it: its separate age, `maxAge` or the `45d` default.
- A resource without `updatedAt` was never queried.
- `address.coordinates` has no `updatedAt`.
- Certificates have no `updatedAt`: each certificate reflects the live query that produced it, not the date of the request.

## Best practices

- **Use larger `maxAge` values for high volumes.** For record enrichment and recurring analysis, the `45d` default or larger values, up to `1y`, reduce consumption and response time.
- **Shorten only the critical resource.** Use a separate age, such as `stateRegistry.maxAge=1d`, instead of reducing `maxAge` for the whole response.
- **Avoid short ages on repeated requests.** With a `maxAge` of minutes or a few hours, most requests become live queries, which are slower and cost more.
- **Treat `206` as success.** Check the `updatedAt` of critical resources and retry later when they are above the limit.
- **Prefer cached-only operations for bulk reads.** Lookup company and Search CNPJ never make live queries; each one's consumption is in the [Metering Rules](https://cnpja.com/en-us/legal/metering#api).
- **Request only the states you need** with `stateRegistry.view=DETAILED`, because each state with no registration is queried live on every request.
- **Set the client timeout** above the maximum duration of a request, described in [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors), because live queries can take a few seconds.

## Frequent errors

| Status | Code | Cause | Action |
| --- | --- | --- | --- |
| `400` | `REQUEST_VALIDATION_FAILED` | `maxAge` or a separate age outside the format or the range, such as `45D`, `3m` or `2y` | Send 1 to 3 digits and a lowercase unit, between `5m` and `1y` |
| `400` | `REQUEST_VALIDATION_FAILED` | Separate age without its resource in `expand`, with the `DEPENDENCIES` code in `errors` | Add the resource to `expand` or remove the separate age |
| `401` | `API_KEY_MISSING` | `maxAge` or another parameter in a request without a key | Send a key, or remove the parameters to use the `45d` default |
| `402` | `CACHED_QUERY_QUOTA_EXCEEDED`, `LIVE_QUERY_QUOTA_EXCEEDED` or `OVERAGE_BUDGET_EXCEEDED` | The organization's quota or overage budget is used up. What each code means is in [Error codes](https://cnpja.com/en-us/docs/api/limits-and-errors#error-codes) | Follow one of the ways out in [Quota used up](https://cnpja.com/en-us/docs/billing/quota-exhausted#ways-out) |
| `404` | `ESTABLISHMENT_NOT_FOUND` | Request without a key for a CNPJ not yet in the cache, which is not queried live | Send a key, as explained in [Public access](https://cnpja.com/en-us/docs/api/public-access) |
| `504` | `REQUEST_TIMED_OUT` | The request, including its live queries, went past the maximum duration | Retry with increasing intervals between attempts; the response is not charged |

The error format, the maximum durations and the other codes are in [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors).

## In the app

On the platform, the max age is the **Maximum age** option of exports, bulk lookups and certificates, and on a CNPJ's record the automatic updates, on by default, query live the sources older than each one's rule, or never queried. See [Refresh data live](https://cnpja.com/en-us/docs/companies/live-refresh) and [Export to a spreadsheet](https://cnpja.com/en-us/docs/lists/export#max-age).

## Notes

- `maxAge` sets the age accepted on each request, not an update schedule: a resource is only queried live when a request finds it above the limit, as explained in [Limitations](https://cnpja.com/en-us/docs/data/freshness#limitations).
- The cache is shared by all customers: `updatedAt` can be more recent than the organization's last request, because it reflects the latest reading of the source made by any live query or open data release.
- A smaller `maxAge` does not refresh what follows its own rules, such as the regime declared in the ECF, the coordinates of an address that did not change and CEPs.
- `updatedAt` uses the ISO 8601 format in UTC, indicated by the trailing `Z`. Convert to the local time zone only for display, as explained in [Dates and times](https://cnpja.com/en-us/docs/data/format#dates).
