Skip to content
Max age and partial success
API · Fundamentals

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.

Cache or live 1 of 3 live
Max age
  • 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:

UnitMeaningExample
yYears1y
wWeeks4w
dDays14d
hHours8h
mMinutes, not months45m
  • 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.

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.

ParameterResourceOperations
company.taxRegime.maxAgeTax regimeLookup CNPJ
stateRegistry.maxAgeState registrationsLookup CNPJ, Lookup CPF
suframa.maxAgeSUFRAMA registrationsLookup 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_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:

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:

OperationRefreshed liveSeparate ages
Lookup CNPJCNPJ registration, company and partners, Simples Nacional, SIMEI and IBS and CBS regular regime options, state and SUFRAMA registrationscompany.taxRegime.maxAge, stateRegistry.maxAge, suframa.maxAge
Issue CNPJ dossierEach requested certificateNone
Lookup CPFState and SUFRAMA registrationsstateRegistry.maxAge, suframa.maxAge
Issue CPF dossierEach requested certificateNone

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 parameterRule in the API
address.coordinatesDoes 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 viewAccept no maxAge: every request generates a new image from the cached address. See Location and maps
company.taxRegime.view=DETAILEDThe 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
stateRegistry.view=SUMMARY, on a CNPJFollows the age of stateRegistry.updatedAt, and only the establishment’s state is queried live
stateRegistry.view=DETAILEDEach state in stateRegistry.states has its age checked and, when needed, is queried live separately
stateRegistry, on a CPFWith 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_REGISTRATION and RFB_PARTNERSCome 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.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, 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) 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. 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, 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:

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.000Z:

FieldOperationsLast update of
updatedAtLookup CNPJ, Lookup company, Lookup person, and the CNPJ and people searchesReceita Federal data
company.taxRegime.updatedAt, or taxRegime.updatedAt on Lookup companyLookup CNPJ, Lookup company, Search CNPJTax regime, from the Simples Nacional
stateRegistry.updatedAtLookup CNPJ, Lookup CPFLast live query in any state
stateRegistry.registrations[].updatedAtLookup CNPJ, Lookup CPFEach state registration, which can be earlier than stateRegistry.updatedAt
suframa.updatedAtLookup CNPJ, Lookup CPFSUFRAMA registrations
suframa.registrations[].updatedAtLookup CNPJ, Lookup CPFEach SUFRAMA registration
updatedAtLookup CEP, Search CEPCorreios data (DNE)

Lookup CNPJ response with expand=company.taxRegime,stateRegistry, 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 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.
  • 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, because live queries can take a few seconds.

Frequent errors

StatusCodeCauseAction
400REQUEST_VALIDATION_FAILEDmaxAge or a separate age outside the format or the range, such as 45D, 3m or 2ySend 1 to 3 digits and a lowercase unit, between 5m and 1y
400REQUEST_VALIDATION_FAILEDSeparate age without its resource in expand, with the DEPENDENCIES code in errorsAdd the resource to expand or remove the separate age
401API_KEY_MISSINGmaxAge or another parameter in a request without a keySend a key, or remove the parameters to use the 45d default
402CACHED_QUERY_QUOTA_EXCEEDED, LIVE_QUERY_QUOTA_EXCEEDED or OVERAGE_BUDGET_EXCEEDEDThe organization’s quota or overage budget is used up. What each code means is in Error codesFollow one of the ways out in Quota used up
404ESTABLISHMENT_NOT_FOUNDRequest without a key for a CNPJ not yet in the cache, which is not queried liveSend a key, as explained in Public access
504REQUEST_TIMED_OUTThe request, including its live queries, went past the maximum durationRetry 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

  • 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.
  • 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.

Informational content that may change without notice. It is not tax, accounting or legal advice, and CNPJá's terms and policies prevail over this page.