Look up CNPJ in Go
This guide shows how to look up a CNPJ with the CNPJAPI REST API in Go, using the standard library's net/http package. The response comes as JSON, with fields in PascalCase (RazaoSocial, SituacaoCadastral, ...).
Prerequisites
- Go 1.18+.
- An API key from CNPJAPI. Create your account at https://app.cnpjapi.com.br and generate the key (see Authentication).
Simple lookup
With typed structs for the fields you use:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Empresa struct {
RazaoSocial string `json:"RazaoSocial"`
SituacaoCadastral struct {
Descricao string `json:"Descricao"`
} `json:"SituacaoCadastral"`
}
func main() {
const cnpj = "00776574000156" // only the 14 digits, without punctuation
const apiKey = "cnpj_sua_chave"
req, _ := http.NewRequest(http.MethodGet, "https://api.cnpjapi.com.br/"+cnpj, nil)
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Fatalf("consulta falhou: HTTP %d", resp.StatusCode)
}
var empresa Empresa
if err := json.NewDecoder(resp.Body).Decode(&empresa); err != nil {
log.Fatal(err)
}
fmt.Println(empresa.RazaoSocial)
fmt.Println(empresa.SituacaoCadastral.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. This snippet also uses the strconv and time packages, so add them to your import:
import (
"net/http"
"strconv"
"time"
)
// ...
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusTooManyRequests:
espera, err := strconv.Atoi(resp.Header.Get("Retry-After"))
if err != nil {
espera = 60 // no valid header, wait a minute
}
time.Sleep(time.Duration(espera) * time.Second)
// retry...
case http.StatusNotFound:
fmt.Println("CNPJ não encontrado na base pública")
case http.StatusOK:
// decode the body...
}
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):
type RespostaIE struct {
Resultados []struct {
UF string `json:"uf"`
IE string `json:"ie"`
Situacao string `json:"situacao"`
} `json:"resultados"`
}
req, _ := http.NewRequest(http.MethodGet, "https://api.cnpjapi.com.br/consulta/ie/"+cnpj+"?uf=SP", nil)
req.Header.Set("Authorization", "Bearer "+apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var ie RespostaIE
if err := json.NewDecoder(resp.Body).Decode(&ie); err != nil {
log.Fatal(err)
}
for _, r := range ie.Resultados {
fmt.Println(r.UF, r.IE, r.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.