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.
- CNPJ registration 2 days ago Cache
- Tax regime 20 days ago Cache
- State registrations 90 days ago Live
Tip: Concepts on this page: Data freshness, Max age, Resources with their own rules and Partial result.
The maxAge parameter sets the max 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 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. The full technical specification is in 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 from5mto1y. - Values outside the format or the range, such as
45D,1.,5d 3mor2y, are rejected with400REQUEST_.VALIDATION_ FAILED - To express months, use days or weeks:
90dis about three months, while3mmeans three minutes. - Public requests, without a key, accept no parameters and always use the
45ddefault, as explained in 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.
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. | Tax regime | Lookup CNPJ |
stateRegistry. | State registrations | Lookup CNPJ, Lookup CPF |
suframa. | 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. Without it, the request is rejected with 400 REQUEST_, 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:
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 | CNPJ registration, company and partners, Simples Nacional, SIMEI and IBS and CBS regular regime options, state and SUFRAMA registrations | company., stateRegistry., suframa. |
| Issue CNPJ dossier | Each requested certificate | None |
| Lookup CPF | State and SUFRAMA registrations | stateRegistry., suframa. |
| Issue CPF 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.
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.
- 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.
- Lookup CEP and Search CEP: they follow the DNE editions, as explained in CEPs and addresses.
Parameters with their own rules
| Resource or parameter | Rule in the API |
|---|---|
address. | 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 |
company. | The age counts only from the latest live query at the Simples Nacional. Use it with a short company. when the SIMPLES_ classification is decisive. See Tax regime |
stateRegistry., on a CNPJ | Follows the age of stateRegistry., and only the establishment’s state is queried live |
stateRegistry. | Each state in stateRegistry. 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 |
RFB_ and RFB_ | 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.
Important: with
stateRegistry., each requested state with no registration is queried live on every request, whatever the max age, and withview=DETAILED stateRegistry.this applies to all 27 states. Request only the states you need. The consumption is in CNPJ lookup, in the Metering Rules.states=ALL
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) 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
200response, 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. The others are charged as usual.
Important: treat
206as success. A client that only accepts200discards 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, 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.:
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, a failure at the source makes the request fail instead of returning 206. In that case, retry later, as explained in 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.:
| Field | Operations | Last update of |
|---|---|---|
updatedAt | Lookup CNPJ, Lookup company, Lookup person, and the CNPJ and people searches | Receita Federal data |
company., or taxRegime. on Lookup company | Lookup CNPJ, Lookup company, Search CNPJ | Tax regime, from the Simples Nacional |
stateRegistry. | Lookup CNPJ, Lookup CPF | Last live query in any state |
stateRegistry. | Lookup CNPJ, Lookup CPF | Each state registration, which can be earlier than stateRegistry. |
suframa. | Lookup CNPJ, Lookup CPF | SUFRAMA registrations |
suframa. | Lookup CNPJ, Lookup CPF | Each SUFRAMA registration |
updatedAt | Lookup CEP, Search CEP | Correios data (DNE) |
Lookup CNPJ response with expand=company., trimmed to the update dates:
{
"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
updatedAtwith the limit that applies to it: its separate age,maxAgeor the45ddefault. - A resource without
updatedAtwas never queried. address.has nocoordinates updatedAt.- Certificates have no
updatedAt: each certificate reflects the live query that produced it, not the date of the request.
Best practices
- Use larger
maxAgevalues for high volumes. For record enrichment and recurring analysis, the45ddefault or larger values, up to1y, reduce consumption and response time. - Shorten only the critical resource. Use a separate age, such as
stateRegistry., instead of reducingmaxAge=1d maxAgefor the whole response. - Avoid short ages on repeated requests. With a
maxAgeof minutes or a few hours, most requests become live queries, which are slower and cost more. - Treat
206as success. Check theupdatedAtof 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.
- Request only the states you need with
stateRegistry., because each state with no registration is queried live on every request.view=DETAILED - Set the client timeout above the maximum duration of a request, described in Limits and errors, because live queries can take a few seconds.
Frequent errors
| Status | Code | Cause | Action |
|---|---|---|---|
400 | REQUEST_ | 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_ | 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_ | maxAge or another parameter in a request without a key | Send a key, or remove the parameters to use the 45d default |
402 | CACHED_, LIVE_ or OVERAGE_ | The organization’s quota or overage budget is used up. What each code means is in Error codes | Follow one of the ways out in Quota used up |
404 | ESTABLISHMENT_ | 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 |
504 | REQUEST_ | 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.
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 and Export to a spreadsheet.
Notes
maxAgesets 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.- The cache is shared by all customers:
updatedAtcan 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
maxAgedoes 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. updatedAtuses the ISO 8601 format in UTC, indicated by the trailingZ. Convert to the local time zone only for display, as explained in Dates and times.