# Migrate from CNPJá v0 to v2

> How to replace the CNPJá API v0 CNPJ lookup with v2, using the same key: new path, per-resource maxAge, camelCase fields and English codes.

API v0 has a single operation, the lookup of one CNPJ with `snake_case` fields. In v2 it matches [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d), which returns the same data under other names and codes, plus resources v0 didn't have.

## Overview

- The request changes from `GET /companies/37335118000180` to `GET /v2/establishments/37335118000180`, on the same host and with the same API key.
- The `*_max_age` parameters, in days, become `maxAge` and each resource's own limit, with an explicit unit such as `45d`.
- `simples_max_age` and `sintegra_max_age` no longer switch resources on: in v2, each one is requested in `expand`.
- Fields move to `camelCase` and change place: company data sits under `company` and the registration status moves up to the response root.
- Classified values become objects with `code` and `label`, and the codes move to English: `MATRIZ` becomes `HEAD`, `ATIVA` becomes `ACTIVE`.
- The `files` and `maps` links leave the response: certificates, maps and street view are operations of their own that return the file.

Effort: low. It is a single operation, and most of the work is renaming fields and translating codes.

## Prerequisites

- **Account and key:** v0 and v2 use the same API keys, from the same organization. If you don't have one yet, see [First request](https://cnpja.com/en-us/docs/api/first-request).
- **Authentication:** send the key as the entire value of the `Authorization` header, with no prefix: `Authorization: YOUR_API_KEY`. With `Bearer`, the request is rejected with `401` `API_KEY_INVALID`. See [Authentication](https://cnpja.com/en-us/docs/api/reference#description/authentication).
- **Base URL:** `https://api.cnpja.com/v2`. v0 answers on the same host, without the version prefix.
- **What carries over:** keys, organization, plan quotas and request limits. Both versions draw from the same quotas, and an equivalent lookup consumes the same in v0 and v2.
- **What changes:** the path, the parameters, the response shape and the codes of classified values.

## Matching operations

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `GET /companies/37335118000180` | [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d) | Same 14-character CNPJ in the path. See [CNPJ lookup](https://cnpja.com/en-us/docs/api/cnpj-lookup). |
| `files.registration` link | [Issue CNPJ dossier](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/dossier) | `certificates=RFB_REGISTRATION`. See [Certificates](https://cnpja.com/en-us/docs/api/certificates). |
| `files.membership` link | [Issue CNPJ dossier](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/dossier) | `certificates=RFB_PARTNERS`. Both can come in the same PDF: `certificates=RFB_REGISTRATION,RFB_PARTNERS`. |
| `maps.roads` and `maps.satellite` links | [Render CNPJ map](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/map) | `type=ROADMAP` or `type=SATELLITE`. See [Location and maps](https://cnpja.com/en-us/docs/api/location). |
| `maps.street` link | [Render CNPJ street view](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d/street-view) | No parameters. |

> **Important:** don't just add the version prefix. In v2, `/v2/companies/` is [Lookup company](https://cnpja.com/en-us/docs/api/reference#tag/companies/GET/v2/companies/%7bid%7d), which takes only the 8-character CNPJ root. The match for the v0 lookup is `/v2/establishments/`, with the full CNPJ.

## Request example

CNPJá v0:

```http
GET /companies/37335118000180?simples_max_age=1&sintegra_max_age=1 HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY
```

CNPJá v2:

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

## Parameters

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `taxId` (path) | `taxId` (path) | 14-character CNPJ, no punctuation. |
| `company_max_age` | `maxAge` | An integer in days in v0, text with a unit in v2: `company_max_age=45` becomes `maxAge=45d`. The default is still 45 days. v2 accepts `5m` to `1y`, so `0` becomes `5m` and values above one year become `1y`. |
| `simples_max_age` | `expand=company.taxRegime` and `company.taxRegime.maxAge` | In v0, sending the parameter added the regime. In v2, request the resource in `expand` and, for a limit of its own, send `company.taxRegime.maxAge`, such as `1d`. Without it, `maxAge` applies. |
| `sintegra_max_age` | `expand=stateRegistry`, `stateRegistry.states=ALL` and `stateRegistry.maxAge` | v0 returned registrations in every UF. In v2 the default is only the establishment's UF (`ORIGIN`); send `ALL` to keep the old behavior, or only the UFs you use. |
| `enable_cache_fallback` | No equivalent | Already deprecated in v0. In v2, falling back to the cache when a live query fails is always on, with a `206` response. |

All three v0 parameters are in days, so `N` becomes `Nd`, within the same `5m` to `1y` range. `maxAge` units: `y` years, `w` weeks, `d` days, `h` hours and `m` minutes, not months. The rules are in [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

## Response fields

The v2 paths are relative to the [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d) response. Date (`YYYY-MM-DD`), date-time (ISO 8601 in UTC) and amount (number) formats don't change.

### Identification and status

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `last_update` | `updatedAt` | Last update from the [Receita Federal](https://solucoes.receita.fazenda.gov.br/Servicos/cnpjreva/). |
| `tax_id` | `taxId` | |
| `name` | `company.legalName` | Legal name. |
| `alias` | `tradeName` | Trade name, `null` when there is none. |
| `type` | `type.code` | `MATRIZ` becomes `HEAD` and `FILIAL` becomes `BRANCH`. |
| `founded` | `incorporationDate` | Opening date. |
| `registration.status` | `status.code` | See the code table below. |
| `registration.status_date` | `statusDate` | |
| `registration.status_reason` | `statusReason.label` | v2 also returns the official code in `statusReason.code`, such as `01`. |
| `registration.special_status` | `specialStatus.label` | v2 also returns the code in `specialStatus.code`, such as `418`. |
| `registration.special_status_date` | `specialStatusDate` | |

| `registration.status` (v0) | `status.code` (v2) |
|---|---|
| `ATIVA` | `ACTIVE` |
| `BAIXADA` | `CLOSED` |
| `INAPTA` | `NONCOMPLIANT` |
| `SUSPENSA` | `SUSPENDED` |
| `NULA` | `VOID` |

### Address and contacts

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `address.street` | `address.street` | |
| `address.number` | `address.number` | |
| `address.details` | `address.details` | |
| `address.neighborhood` | `address.district` | |
| `address.city` | `address.city` | |
| `address.city_ibge` | `address.municipality` | 7-digit [IBGE municipality code](https://www.ibge.gov.br/explica/codigos-dos-municipios.php). |
| `address.state` | `address.state` | UF abbreviation. |
| `address.state_ibge` | No equivalent | Use the first 2 digits of `address.municipality`. |
| `address.zip` | `address.postalCode` | 8-digit CEP. |
| `email` | `emails[0].address` | v2 returns every email, each with its type in `emails[].type`. |
| `phone` | `phones[0].number` | In v2 the number comes in international format: `11971564144` becomes `+5511971564144`. Each phone has its type in `phones[].type`. |
| `phone_alt` | `phones[1].number` | Same format as `phones[0].number`. |

### Activities

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `primary_activity.code` | `primaryActivity.code` | 7-digit [CNAE](https://concla.ibge.gov.br/busca-online-cnae.html?view=estrutura). |
| `primary_activity.description` | `primaryActivity.label` | |
| `secondary_activities[].code` | `secondaryActivities[].code` | |
| `secondary_activities[].description` | `secondaryActivities[].label` | |

### Company and partners

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `capital` | `company.shareCapital` | |
| `size` | `company.size.code` | `ME` becomes `MICRO`, `EPP` becomes `SMALL` and `DEMAIS` becomes `OTHER`. |
| `legal_nature.code` | `company.legalNature.code` | |
| `legal_nature.description` | `company.legalNature.label` | |
| `federal_entity` | `company.federativeEntity.label` | v2 also returns `company.federativeEntity.code`: `BR` for the Union, the UF abbreviation or the IBGE municipality code. |
| `membership[]` | `company.partners[]` | Board of partners and administrators. |
| `membership[].name` | `company.partners[].person.legalName` | |
| `membership[].tax_id` | `company.partners[].person.taxId` | Masked CPF, such as `***650245**`, or CNPJ. `null` for foreigners and unknown persons. |
| `membership[].home_country` | `company.partners[].person.domicileCountry` | v0 returned the country name in uppercase, such as `BRASIL`. v2 returns the alpha-2 code in `code` (`BR`) and the name in `label` (`Brasil`). |
| `membership[].role.code` | `company.partners[].role.code` | |
| `membership[].role.description` | `company.partners[].role.label` | |
| `membership[].legal_rep` | `company.partners[].legalRepresentative` | Legal representative. Its inner fields follow the same mapping: `name` becomes `person.legalName`, `tax_id` becomes `person.taxId`, `home_country` becomes `person.domicileCountry` and `role` stays `role`. |

In v2, each partner also has a stable ID in `person.id`, the person type in `person.type`, the age group in `person.ageGroup` and the date they joined in `admissionDate`. See [Partners and people](https://cnpja.com/en-us/docs/api/partners).

### Simples Nacional and SIMEI

Present only with `company.taxRegime` in `expand`. See [Tax regime](https://cnpja.com/en-us/docs/api/tax-regime).

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `simples_nacional.last_update` | `company.taxRegime.updatedAt` | |
| `simples_nacional.simples_optant` | `company.taxRegime.type.code` | An optant when the code is `SIMPLES_NACIONAL`, `SIMPLES_NACIONAL_HYBRID` or `SIMPLES_NACIONAL_SIMEI`. Otherwise v2 states the regime, such as `PRESUMED_PROFIT`. |
| `simples_nacional.simples_included` | `company.taxRegime.simplesNacionalStartDate` | Start of the current period. Present only when the code is `SIMPLES_NACIONAL`, `SIMPLES_NACIONAL_HYBRID` or `SIMPLES_NACIONAL_SIMEI`. |
| `simples_nacional.simples_excluded` | `company.taxRegime.timeline[].endDate` | v0 had already stopped filling this field. In v2, past periods come with `company.taxRegime.view=DETAILED`, in the `timeline` items whose `type.code` is `SIMPLES_NACIONAL_PERIOD`. |
| `simples_nacional.simei_optant` | `company.taxRegime.type.code` | An optant when the code is `SIMPLES_NACIONAL_SIMEI`. v2 also returns the period start in `company.taxRegime.simeiStartDate`. |

In v2, Simples Nacional optants that also opted into the IBS or CBS regular regime have the code `SIMPLES_NACIONAL_HYBRID`. With `company.taxRegime.view=DETAILED`, `company.taxRegime.regularIbsStartDate` and `company.taxRegime.regularCbsStartDate` hold the start of their current period in each regular regime, and either can be `null`.

### State registrations

Present only with `stateRegistry` in `expand`. See [State registrations](https://cnpja.com/en-us/docs/api/state-registrations).

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `sintegra.last_update` | `stateRegistry.updatedAt` | |
| `sintegra.home_state_registration` | `stateRegistry.registrations[].number` | No field of its own: use the registration whose `state` equals `address.state`. |
| `sintegra.registrations[].number` | `stateRegistry.registrations[].number` | |
| `sintegra.registrations[].state` | `stateRegistry.registrations[].state` | |
| `sintegra.registrations[].enabled` | `stateRegistry.registrations[].isEnabled` | |

v2 registrations also include the type, the status date and restrictions as a recipient, and interstate restrictions come in `stateRegistry.taxRestrictions`.

### Files and maps

| CNPJá v0 | CNPJá v2 | Notes |
|---|---|---|
| `files.registration` | No field | Use Issue CNPJ dossier with `certificates=RFB_REGISTRATION`. |
| `files.membership` | No field | Use Issue CNPJ dossier with `certificates=RFB_PARTNERS`. |
| `maps.roads` | No field | Use Render CNPJ map with `type=ROADMAP`. |
| `maps.satellite` | No field | Use Render CNPJ map with `type=SATELLITE`. |
| `maps.street` | No field | Use Render CNPJ street view. |

## Behavior differences

### Freshness and caching

In v2, each resource comes from the cache while it is within `maxAge` and is queried live, before the response, when it is older or was never fetched. `maxAge` ranges from `5m` to `1y`, with a `45d` default, and each resource reports its last update in its own `updatedAt`. Outdated registration data, tax regime and state registrations are queried in parallel. See [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

### Partial success

When a live query fails or runs past its [time limit](https://cnpja.com/en-us/docs/api/limits-and-errors#timeout-and-size), the response is `206`, with the same shape as `200`: the affected resource keeps its cached data, or comes back empty if it was never fetched, and is not billed. With no cached data to fall back on, such as on a CNPJ's first lookup, the request fails instead of returning `206`. Treat `206` as success and compare each resource's `updatedAt` with the limit you asked for. See [Partial success](https://cnpja.com/en-us/docs/api/reference#description/partial-success).

### Errors

New for v0 users: a `maxAge` in the wrong format, or a resource parameter without its resource in `expand`, is rejected with `400` `REQUEST_VALIDATION_FAILED`. In v2, every error body carries `status`, `code`, `message` and `traceId`. Handle errors by `code`, which is stable, and use `message` for display only. See [Limits and errors](https://cnpja.com/en-us/docs/api/limits-and-errors).

### Limits

Request limits apply across all operations, v0 included. With a key, the requests-per-second and concurrent-request limits apply to the whole organization, across all of its keys, and follow its plan; without a key, they apply to each source IP. The values are in [Usage limits](https://cnpja.com/en-us/docs/api/limits-and-errors#usage-limits). Above them, the response is `429`, not billed. Each request has a maximum duration, and the client timeout must stay above it, as shown in [Timeout and size](https://cnpja.com/en-us/docs/api/limits-and-errors#timeout-and-size).

### Billing

Each resource in the response is billed separately, as a live query, which costs more, or a cached query, and each unit has its own quota in the plan, renewed every month. On Pro, the quota of each unit can grow with [packs](https://cnpja.com/en-us/docs/billing/packs-and-budget#packs), and the organization can set a budget to pay for each query beyond the quota at the per-query overage price, so an exhausted quota does not have to stop the integration. When a quota runs out with no overage budget, or the month's budget runs out, every request made with one of the organization's keys gets `402`, as explained in [Quota used up](https://cnpja.com/en-us/docs/billing/quota-exhausted#api); on Free, which has no packs or budget, this happens as soon as the quota runs out. Requests without a key, rejected requests and server failures are not billed, as listed in [What is not charged](https://cnpja.com/en-us/legal/metering#not-charged). In Lookup CNPJ, registration data and each resource requested in `expand` count separately. Certificates, maps and street view are billed when you call each one's operation. Responses carry no cost headers: track usage under [Usage](https://cnpja.com/en-us/organizations/current/usage). See [Queries and quota](https://cnpja.com/en-us/docs/billing/queries-and-quotas), what each operation consumes in the [Metering Rules](https://cnpja.com/en-us/legal/metering#api) and [Pricing](https://cnpja.com/en-us/pricing).

### Public access

v0 requires a key. In v2, Lookup CNPJ and Lookup CEP accept requests without a key, as long as the URL has no query parameters at all; any parameter results in `401` `API_KEY_MISSING`. These requests use the default values, such as `maxAge=45d` with no `expand`, are not billed and follow limits of their own, counted per source IP. See [Public access](https://cnpja.com/en-us/docs/api/public-access).

### Language and labels

v2 codes are fixed and in English, and the text goes in `label`. `label` values and error messages come in Portuguese by default; send `Accept-Language: en-US` to get them in English. Codes, field names and official source data are never translated. Use `code` in your logic and `label` for display only. See [Language](https://cnpja.com/en-us/docs/api/reference#description/language).

## No equivalent

- `enable_cache_fallback`: falling back to the cache on failures is always on.
- `address.state_ibge`: derive it from the first 2 digits of `address.municipality`.
- `sintegra.home_state_registration`: filter `stateRegistry.registrations` by the UF in `address.state`, or request `stateRegistry.states=ORIGIN`.
- Links in `files` and `maps`: the response carries no URLs; call the file's operation when you need it.
- Uppercase country name in `home_country`: use `person.domicileCountry.code` or `.label`.

## Step by step

1. Confirm the API key used in v0 under [API keys](https://cnpja.com/en-us/organizations/current/api-keys).
2. Replace the `/companies/` path with `/v2/establishments/`, keeping the same CNPJ.
3. Convert `company_max_age` into `maxAge`, and `simples_max_age` and `sintegra_max_age` into `expand` values with their own limits.
4. Choose the UFs for state registrations: `ALL` to keep the v0 behavior, or only the ones you need.
5. Update how you read fields following the tables above, including the new codes for type, size and status.
6. Replace the `files` and `maps` links with calls to Issue CNPJ dossier, Render CNPJ map and Render CNPJ street view, if you used them.
7. Treat `206` as success and base error handling on `code`.
8. Test with a few CNPJs using your key and compare with the v0 responses.
9. Switch traffic to v2 and track consumption under [Usage](https://cnpja.com/en-us/organizations/current/usage).
