# Migrate from CNPJá Open to v2

> How to move CNPJá Open's Consulta CNPJ and Consulta CEP to v2, with or without an API key: new paths, renamed fields and codes as text.

CNPJá Open is the public API at `open.cnpja.com`, with Consulta CNPJ and Consulta CEP and no authentication. v2 keeps both lookups available without a key, through [public access](https://cnpja.com/en-us/docs/api/reference#description/public-access), and opens the rest of the API with a key.

## Overview

- **Host and paths:** `open.cnpja.com/office/:taxId` becomes `api.cnpja.com/v2/establishments/:taxId`, and `open.cnpja.com/zip/:code` becomes `api.cnpja.com/v2/postal-codes/:code`.
- **Still possible without a key:** [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d) and [Lookup CEP](https://cnpja.com/en-us/docs/api/reference#tag/postal-codes/GET/v2/postal-codes/%7bcode%7d) accept public requests, with no query parameters and no billing.
- **Higher limits:** CNPJá Open allows 5 queries per minute per IP. v2 public access limits requests per second and concurrent requests, also per IP, and, with a key, the limits belong to the organization and grow with its plan, as shown in [Usage limits](https://cnpja.com/en-us/docs/api/limits-and-errors#usage-limits).
- **Renamed fields:** names change, such as `alias` to `tradeName`, `founded` to `incorporationDate` and `members` to `partners`.
- **Codes as text:** numeric codes, such as registration status and company size, become `code` and `label` pairs, with `code` as text.
- **Optional resources need a key:** Simples Nacional, MEI and SUFRAMA only come back in v2 with a key and `expand`.

Effort: low to medium. Switching the URL is simple, and most of the work is adapting field parsing, following the mapping tables below.

## Prerequisites

- **Public access:** if you only use the fields v2 returns without a key, you don't need an account. Just change the URL. See [Public access](https://cnpja.com/en-us/docs/api/public-access).
- **Account and key:** for Simples Nacional, MEI, SUFRAMA, `maxAge` or more volume, [sign up](https://cnpja.com/en-us/signup), create a key in [API keys](https://cnpja.com/en-us/organizations/current/api-keys) and follow [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`.
- **What carries over:** CNPJ and CEP without punctuation, dates as `YYYY-MM-DD`, date-times in ISO 8601 and the partners' masked CPF.
- **What changes:** host, paths, field names, numeric codes, countries as numeric M49, limits and the error format.

## Matching operations

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| Consulta CNPJ, `GET /office/:taxId` | [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d) | Accepts public access. With a key, accepts `maxAge` and `expand`. See [CNPJ lookup](https://cnpja.com/en-us/docs/api/cnpj-lookup). |
| Consulta CEP, `GET /zip/:code` | [Lookup CEP](https://cnpja.com/en-us/docs/api/reference#tag/postal-codes/GET/v2/postal-codes/%7bcode%7d) | Accepts public access. The response is the same with or without a key. See [CEP and addresses](https://cnpja.com/en-us/docs/api/postal-codes). |

## Request example

CNPJá Open:

```http
GET /office/37335118000180 HTTP/1.1
Host: open.cnpja.com
```

CNPJá v2, with public access:

```http
GET /v2/establishments/37335118000180 HTTP/1.1
Host: api.cnpja.com
```

CNPJá v2, with a key and the resources that match `simples`, `simei` and `suframa`:

```http
GET /v2/establishments/37335118000180?expand=company.taxRegime,suframa HTTP/1.1
Host: api.cnpja.com
Authorization: YOUR_API_KEY
```

## Parameters

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `taxId` (path) | `taxId` (path) | 14 characters without punctuation. v2 accepts uppercase letters in alphanumeric CNPJs. |
| `code` (path) | `code` (path) | 8 digits without a hyphen, keeping leading zeros. |

CNPJá Open has no query parameters. In v2, the parameters below bring back parts of the Open response and require a key:

| Part of the Open response | v2 parameter |
|---|---|
| `company.simples`, `company.simei` | `expand=company.taxRegime` |
| `company.simples.history`, `company.simei.history` | `expand=company.taxRegime` with `company.taxRegime.view=DETAILED` |
| `suframa` | `expand=suframa` |

With a key, `maxAge` sets the maximum age of the data, from `5m` to `1y`, with a `45d` default. See [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

## Response fields

The v2 column paths are from [Lookup CNPJ](https://cnpja.com/en-us/docs/api/reference#tag/establishments/GET/v2/establishments/%7btaxId%7d), except in the CEP section.

### Identification and status

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `taxId` | `taxId` | |
| `updated` | `updatedAt` | Last update from the [Receita Federal](https://solucoes.receita.fazenda.gov.br/Servicos/cnpjreva/). |
| `alias` | `tradeName` | Can be `null`. |
| `founded` | `incorporationDate` | |
| `head` | `type.code` | `true` becomes `HEAD`, `false` becomes `BRANCH`. |
| `statusDate` | `statusDate` | |
| `status.id` | `status.code` | Number becomes a text code. See the code table. |
| `status.text` | `status.label` | |
| `reason.id` | `statusReason.code` | 2-character text with a leading zero: `1` becomes `01`. |
| `reason.text` | `statusReason.label` | |
| `specialDate` | `specialStatusDate` | |
| `special.id` | `specialStatus.code` | 3-character text. |
| `special.text` | `specialStatus.label` | |

### Address and contacts

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `address.street` | `address.street` | Can be `null`. |
| `address.number` | `address.number` | Can be `null`. |
| `address.details` | `address.details` | Can be `null`. |
| `address.district` | `address.district` | Can be `null`. |
| `address.city` | `address.city` | |
| `address.state` | `address.state` | UF abbreviation. |
| `address.zip` | `address.postalCode` | |
| `address.municipality` | `address.municipality` | Number becomes 7-character text: `3550308` becomes `"3550308"`. |
| `address.country.id` | `address.country.code` | Numeric M49 code becomes alpha-2: `76` becomes `BR`. |
| `address.country.name` | `address.country.label` | |
| `phones[].type` | `phones[].type.code` | Same `LANDLINE` and `MOBILE` codes. |
| `phones[].area` and `phones[].number` | `phones[].number` | A single E.164 field, such as `+5511971564144`. The area code (DDD) is the 2 digits after `+55`. |
| `emails[].ownership` | `emails[].type.code` | v2 also uses `ACADEMIC` and `GOVERNMENT`, which CNPJá Open returned as `CORPORATE`. |
| `emails[].address` | `emails[].address` | |
| `emails[].domain` | No equivalent | Take it from the address, after the `@`. |

### Activities

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `mainActivity.id` | `primaryActivity.code` | Number becomes 7-character text with leading zeros. |
| `mainActivity.text` | `primaryActivity.label` | |
| `sideActivities[].id` | `secondaryActivities[].code` | Same rule as `primaryActivity.code`. |
| `sideActivities[].text` | `secondaryActivities[].label` | |

### Company and partners

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `company.id` | `company.id` | |
| `company.name` | `company.legalName` | |
| `company.equity` | `company.shareCapital` | |
| `company.jurisdiction` | `company.federativeEntity.label` | `company.federativeEntity.code` holds `BR` for the União, the UF abbreviation, or the municipality's IBGE code. |
| `company.nature.id` | `company.legalNature.code` | Number becomes 4-character text: `2062` becomes `"2062"`. |
| `company.nature.text` | `company.legalNature.label` | |
| `company.size.id` | `company.size.code` | See the code table. |
| `company.size.acronym` | No equivalent | Derive it from `company.size.code`. |
| `company.size.text` | `company.size.label` | |
| `company.members[]` | `company.partners[]` | Always present. |
| `company.members[].since` | `company.partners[].admissionDate` | Can be `null`. |
| `company.members[].role.id` | `company.partners[].role.code` | 2-character text: `49` becomes `"49"`. |
| `company.members[].role.text` | `company.partners[].role.label` | |
| `company.members[].person.id` | `company.partners[].person.id` | UUID, the same value as in v2. Use it with [Lookup person](https://cnpja.com/en-us/docs/api/reference#tag/people/GET/v2/people/%7bid%7d), with a key. If an `id` stored a while ago gets a `404` `PERSON_NOT_FOUND`, look up the CNPJ again to get the current one. |
| `company.members[].person.type` | `company.partners[].person.type.code` | See the code table. |
| `company.members[].person.name` | `company.partners[].person.legalName` | |
| `company.members[].person.taxId` | `company.partners[].person.taxId` | Masked CPF, such as `***650245**`, or CNPJ. `null` for `FOREIGNER` and `UNKNOWN`. |
| `company.members[].person.age` | `company.partners[].person.ageGroup` | Same ranges, such as `31-40`. |
| `company.members[].person.country.id` | `company.partners[].person.domicileCountry.code` | Numeric M49 becomes alpha-2. On CNPJá Open, `country` comes only for `FOREIGN`. In v2, the country comes for every person type. |
| `company.members[].person.country.name` | `company.partners[].person.domicileCountry.label` | |
| `company.members[].agent.person` | `company.partners[].legalRepresentative.person` | Same shape as `person`. |
| `company.members[].agent.role` | `company.partners[].legalRepresentative.role` | `id` and `text` become `code` and `label`. |

More on partners in [Partners and people](https://cnpja.com/en-us/docs/api/partners).

### Tax regime and SUFRAMA

These fields require a key and `expand`. See [Tax regime](https://cnpja.com/en-us/docs/api/tax-regime) and [SUFRAMA](https://cnpja.com/en-us/docs/api/suframa).

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `company.simples.optant` | `company.taxRegime.type.code` | Optant when the code is `SIMPLES_NACIONAL`, `SIMPLES_NACIONAL_HYBRID` or `SIMPLES_NACIONAL_SIMEI`. Requires `expand=company.taxRegime`. |
| `company.simples.since` | `company.taxRegime.simplesNacionalStartDate` | Start of the current period. Present only for [Simples Nacional optants](https://consopt.www8.receita.fazenda.gov.br/consultaoptantes), with the code `SIMPLES_NACIONAL`, `SIMPLES_NACIONAL_HYBRID` or `SIMPLES_NACIONAL_SIMEI`. Can be `null`. |
| `company.simples.history[]` | `company.taxRegime.timeline[]` with `type.code` `SIMPLES_NACIONAL_PERIOD` | Requires `company.taxRegime.view=DETAILED`. |
| `company.simei.optant` | `company.taxRegime.type.code` | Optant when the code is `SIMPLES_NACIONAL_SIMEI`. |
| `company.simei.since` | `company.taxRegime.simeiStartDate` | Start of the current period. Present only with the code `SIMPLES_NACIONAL_SIMEI`. Can be `null`. |
| `company.simei.history[]` | `company.taxRegime.timeline[]` with `type.code` `SIMEI_PERIOD` | Requires `company.taxRegime.view=DETAILED`. |
| `history[].from` | `timeline[].startDate` | |
| `history[].until` | `timeline[].endDate` | Can be `null`. |
| `history[].text` | `timeline[].details` | Can be `null`. |
| `suframa[]` | `suframa.registrations[]` | Requires `expand=suframa`. |
| `suframa[].number` | `suframa.registrations[].number` | |
| `suframa[].since` | `suframa.registrations[].registrationDate` | |
| `suframa[].approved` | `suframa.registrations[].hasApprovedProject` | |
| `suframa[].approvalDate` | `suframa.registrations[].approvalDate` | Can be `null`. |
| `suframa[].status.id` | `suframa.registrations[].status.code` | See the code table. |
| `suframa[].status.text` | `suframa.registrations[].status.label` | |
| `suframa[].incentives[]` | Official [Mapa dos Incentivos Fiscais](https://www.gov.br/suframa/pt-br/zfm/mapa-dos-incentivos-fiscais-ultima-versao-28-09.xlsx) spreadsheet | SUFRAMA no longer publishes the incentives in its registry lookup and directs users to this spreadsheet, so the API does not return them. The criteria it applied are in [Estimate incentives](https://cnpja.com/en-us/docs/data/suframa#incentives). |

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`. The v2 `timeline` also holds future events and periods of other regimes, so filter by `type.code`.

### CEP

The v2 column paths are from [Lookup CEP](https://cnpja.com/en-us/docs/api/reference#tag/postal-codes/GET/v2/postal-codes/%7bcode%7d).

| CNPJá Open | CNPJá v2 | Notes |
|---|---|---|
| `updated` | `updatedAt` | Last update from [Correios (DNE)](https://buscacepinter.correios.com.br/app/endereco/index.php). |
| `code` | `code` | |
| `street` | `street` | Can be `null`. |
| `number` | `number` | Can be `null`. |
| `district` | `district` | Can be `null`. |
| `city` | `city` | |
| `state` | `state` | |
| `municipality` | `municipality` | Number becomes 7-character text. |

v2 also returns `type`, `status`, `name` and `details`. The CEP type defines which fields are filled: for a locality CEP, only `city`, `state` and `municipality`. See [CEP and addresses](https://cnpja.com/en-us/docs/api/postal-codes).

### Code table

| Open field | Open value | v2 `code` |
|---|---|---|
| `status.id` | `1` Nula | `VOID` |
| `status.id` | `2` Ativa | `ACTIVE` |
| `status.id` | `3` Suspensa | `SUSPENDED` |
| `status.id` | `4` Inapta | `NONCOMPLIANT` |
| `status.id` | `8` Baixada | `CLOSED` |
| `company.size.id` | `1` ME | `MICRO` |
| `company.size.id` | `3` EPP | `SMALL` |
| `company.size.id` | `5` Demais | `OTHER` |
| `person.type` | `LEGAL` | `ESTABLISHMENT` |
| `person.type` | `NATURAL` | `INDIVIDUAL` |
| `person.type` | `FOREIGN` | `FOREIGNER` |
| `person.type` | `UNKNOWN` | `UNKNOWN` |
| `suframa[].status.id` | `1` Ativa | `ACTIVE` |
| `suframa[].status.id` | `2` Inativa | `INACTIVE` |
| `suframa[].status.id` | `3` Bloqueada | `BLOCKED` |
| `suframa[].status.id` | `4` Cancelada | `CANCELLED` |
| `suframa[].status.id` | `5` Cancelada Ag. Rec. | No equivalent |

> **Important:** v2 codes are text and can have leading zeros, such as `01` in `statusReason.code` and `0111301` in a [CNAE](https://concla.ibge.gov.br/busca-online-cnae.html?view=estrutura). Compare them as text, without converting to numbers.

## Behavior differences

### Freshness and caching

CNPJá Open states CNPJ data is updated within 45 days for active establishments. 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`. Public access always uses the `45d` default and does not run a live query for a CNPJ that is not cached yet, as explained in [Public access](https://cnpja.com/en-us/docs/api/public-access); with a key, you choose `maxAge`. See [Max age and partial success](https://cnpja.com/en-us/docs/api/max-age).

### Partial success

Lookup CNPJ can return `206` on public access too. 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).

### CEP

CNPJá Open states global CEP updates every 15 days. In v2, Lookup CEP serves cached data only and doesn't accept `maxAge`, and `updatedAt` shows the last update from Correios.

### Errors

The CNPJá Open spec documents only the `200` response. A CNPJ or CEP that doesn't exist, for example, now returns `404` `ESTABLISHMENT_NOT_FOUND` or `404` `POSTAL_CODE_NOT_FOUND`. 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

CNPJá Open allows 5 queries per minute per IP. 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 and with no `Retry-After` header. 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).

> **Important:** a request is public only when the URL has no query parameters at all. Any parameter, including `?utm_source=site`, returns `401` `API_KEY_MISSING`.

### 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). With a key, in Lookup CNPJ, the registration, `company.taxRegime` and `suframa` count separately, and a CNPJ or CEP that doesn't exist, with `404`, is billed too, as detailed in [What is charged even without a useful result](https://cnpja.com/en-us/legal/metering#charged-without-result). 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).

### Language and labels

`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

- `company.size.acronym`: derive it from `company.size.code`.
- `emails[].domain`: take it from the email address.
- A separate `phones[].area`: the area code is inside `phones[].number`, in E.164.
- Numeric M49 country codes in `address.country.id` and `person.country.id`: v2 uses alpha-2.
- SUFRAMA status `5` Cancelada Ag. Rec.: v2 has only `ACTIVE`, `INACTIVE`, `BLOCKED` and `CANCELLED`.

## Step by step

1. List the places in your system that call `open.cnpja.com` and the fields each one reads.
2. Choose between public access and a key. Use a key if you need Simples Nacional, MEI, SUFRAMA, `maxAge`, limits that grow with the plan or other operations.
3. If you use a key, [sign up](https://cnpja.com/en-us/signup), create the key in [API keys](https://cnpja.com/en-us/organizations/current/api-keys) and send it in `Authorization`, with no prefix.
4. Change the host to `api.cnpja.com` and the paths to `/v2/establishments/:taxId` and `/v2/postal-codes/:code`. Without a key, send no query parameters.
5. Adapt field parsing following the tables above, including text codes, E.164 phones and alpha-2 countries.
6. Treat `206` as success, handle errors by `code`, and retry `429`, `500` and `504` after a wait.
7. Test with a few CNPJs and CEPs, with or without a key, and compare the results with CNPJá Open.
8. Switch traffic to v2. With a key, track consumption in [Usage](https://cnpja.com/en-us/organizations/current/usage).

> **Tip:** public access is a free courtesy, limited and revocable at any time, with no service level, as explained in [Public access](https://cnpja.com/en-us/docs/api/public-access). Move to a key when a rejection by a limit isn't acceptable.
