Look up CNPJ in Node.js
This guide shows how to look up a CNPJ with the CNPJAPI REST API in Node.js, using native fetch (Node 18+). The response comes as JSON, with fields in PascalCase (RazaoSocial, SituacaoCadastral, ...).
Prerequisites
- Node.js 18+ (
fetchis native; on earlier versions, usenode-fetchorundici). - The examples use top-level
await, so run them as an ES module (a.mjsfile, or"type": "module"inpackage.json). In CommonJS, wrap the code in anasyncfunction. - An API key from CNPJAPI. Create your account at https://app.cnpjapi.com.br and generate the key (see Authentication).
Simple lookup
const CNPJ = "00776574000156"; // only the 14 digits, without punctuation
const API_KEY = "cnpj_sua_chave";
const resposta = await fetch(`https://api.cnpjapi.com.br/${CNPJ}`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (!resposta.ok) {
throw new Error(`Falha na consulta: HTTP ${resposta.status}`);
}
const empresa = await resposta.json();
console.log(empresa.RazaoSocial);
console.log(empresa.SituacaoCadastral.Descricao);
console.log(empresa.AtividadePrincipal.Descricao);
Handling errors and rate limits
When you exceed the per-minute limit or the monthly quota, the API responds 429 with a Retry-After header (seconds). Treat it as recoverable:
const resposta = await fetch(`https://api.cnpjapi.com.br/${CNPJ}`, {
headers: { Authorization: `Bearer ${API_KEY}` },
});
if (resposta.status === 429) {
const espera = Number(resposta.headers.get("Retry-After") ?? 60);
await new Promise((r) => setTimeout(r, espera * 1000));
// retry...
} else if (resposta.status === 404) {
console.log("CNPJ não encontrado na base pública");
} else if (resposta.ok) {
const empresa = await resposta.json();
console.log(empresa.RazaoSocial);
}
State Registration (IE, premium)
On a plan that includes State Registration (IE), look up a company's IE from the official SEFAZ source. Pass uf for a single state (1 credit) or omit it for the nationwide sweep (3 credits):
const resposta = await fetch(
`https://api.cnpjapi.com.br/consulta/ie/${CNPJ}?uf=SP`,
{ headers: { Authorization: `Bearer ${API_KEY}` } },
);
if (!resposta.ok) {
throw new Error(`IE lookup failed: HTTP ${resposta.status}`);
}
const { resultados } = await resposta.json();
for (const ie of resultados) {
console.log(ie.uf, ie.ie, ie.situacao);
}
Full contract (fields, coverage, credits) at Look up the State Registration.
Next steps
Create your free account at https://app.cnpjapi.com.br and make your first lookup in minutes.