Look up CNPJ in Excel
This guide shows how to look up a CNPJ (the Brazilian company tax ID) right inside Excel, no coding: you use Power Query (built into Excel) to call the CNPJAPI REST API and pull the company data - legal name, registration status, main activity - into the spreadsheet. It is ideal for enriching a whole column of CNPJs at once.
Before Power Query, the guide starts from scratch: creating the account and generating your API key.
Before you start: create the account and generate the API key
To use the API you need an API key (a token that identifies your account). It is free to start.
- Go to https://app.cnpjapi.com.br and create your account on the free plan (no card required).
- Confirm your e-mail and sign in to the portal.
- Inside the portal, generate your API key. It starts with
cnpj_(for example,cnpj_a1b2c3...). Copy it and keep it somewhere safe - the key is shown once. - This key goes in the
Authorizationheader, asBearer cnpj_your_key, on every request. Power Query handles that for you (steps below).
Details in Authentication. Treat the key like a password: whoever holds it consumes your quota.
Look up one CNPJ (a single cell)
In Excel (Windows), open Data → Get Data → From Other Sources → Blank Query. The Power Query Editor opens. Click Advanced Editor and paste:
let
Source = Json.Document(
Web.Contents(
"https://api.cnpjapi.com.br",
[
RelativePath = "00776574000156",
Headers = [#"Authorization" = "Bearer cnpj_your_key"]
]
)
)
in
Source
Replace 00776574000156 with the CNPJ (14 digits only, no punctuation) and cnpj_your_key with your key. Click Done.
Why
RelativePathinstead of the full URL? Power Query blocks refresh on "dynamic" URLs. Keeping the host fixed inWeb.Contents(...)and passing the CNPJ inRelativePathkeeps Refresh working without warnings.
On the first run Power Query may ask for the source access level: choose Anonymous. The key already travels in the Authorization header; no credential setup is needed.
The result comes back as a record. Click the fields to expand. The main ones:
RazaoSocial- legal name;SituacaoCadastral→Descricao- "Ativa" (active), "Baixada" (closed), etc.;AtividadePrincipal→Descricao- main activity (CNAE) spelled out.
Click Home → Close & Load to drop the result into the sheet.
Look up a whole column of CNPJs
The most useful case: you have a list of CNPJs and want data for all of them. The idea is to create a function and apply it to the column.
1. Store the key in a parameter (optional but recommended). In the Power Query Editor: Manage Parameters → New Parameter, name ApiKey, type Text, current value cnpj_your_key. That keeps the key in one place.
2. Create the function. New Query → Blank Query → Advanced Editor and paste:
(cnpj as text) as record =>
let
Clean = Text.Select(cnpj, {"0".."9"}),
Response = Web.Contents(
"https://api.cnpjapi.com.br",
[
RelativePath = Clean,
Headers = [#"Authorization" = "Bearer " & ApiKey],
ManualStatusHandling = {404, 429}
]
),
Status = Value.Metadata(Response)[Response.Status],
Record =
if Status = 200 then Json.Document(Response)
else [RazaoSocial = "error " & Text.From(Status), SituacaoCadastral = null, AtividadePrincipal = null]
in
Record
Rename this query to LookupCNPJ (right-click → Rename). Text.Select strips punctuation, so it works even if your column has 00.776.574/0001-56. ManualStatusHandling keeps a 404 (not found) or 429 (rate limit) from breaking the whole refresh.
3. Apply it to the column. Select the query holding your list of CNPJs (say, a table with a CNPJ column). Go to Add Column → Invoke Custom Function, pick LookupCNPJ, and pass the CNPJ column as the argument. Power Query adds a column of records; click the expand icon (⇔) at its top and check RazaoSocial, SituacaoCadastral, AtividadePrincipal. Close & Load.
Done: every row now carries the company data.
Rate limit (and when to upgrade)
Power Query fires the calls in parallel, quite fast. On the free plan the API accepts a limited number of lookups per minute - when you exceed it, it returns 429 (rate limited) and the extra rows come back with an error.
For large lists you have two options:
- Split into batches and refresh a bit at a time; or
- Upgrade to a plan with a higher limit and a comfortable monthly quota, and run the whole list at once.
See the plan limits. If you enrich CNPJ spreadsheets often, a paid plan pays for itself quickly in time saved.
Notes
- It only works in Excel for Windows (full Power Query is not in Excel for Mac or Excel on the web). On Mac or in the browser, use Google Sheets.
- Keep the API key safe: do not share the sheet with the key embedded with anyone who should not consume your quota.
Next steps
Create your free account at https://app.cnpjapi.com.br and enrich your first spreadsheet in minutes.