Look up a CPF with Go
Look up a CPF (Brazil's individual taxpayer ID) with Go using net/http from the standard library. No external dependencies.
Using Cursor, Lovable, v0 or another AI? Copy the Simple Lookup prompt and paste it into your assistant.
Before you start
- Create a free account at app.cpfhub.io: the Free plan includes 50 credits and does not ask for a card.
- Copy your API key from app.cpfhub.io/api-keys.
- Store the key in an environment variable, never in your code:
export CPFHUB_API_KEY="sua_api_key"Example
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
)
type response struct {
Data *struct {
Name string `json:"name"`
BirthDate string `json:"birthDate"`
} `json:"data"`
// error is a string ("...") or an object ({"message": "..."})
Error json.RawMessage `json:"error"`
}
func main() {
cpf := "12345678909"
req, err := http.NewRequest(http.MethodGet, "https://api.cpfhub.io/cpf/"+cpf, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("x-api-key", os.Getenv("CPFHUB_API_KEY"))
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
var body response
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
log.Fatal(err)
}
switch resp.StatusCode {
case http.StatusOK:
fmt.Println(body.Data.Name, body.Data.BirthDate)
case http.StatusNotFound:
fmt.Println("CPF not found (does not use a credit)")
case http.StatusTooManyRequests:
fmt.Println("Per-minute limit reached. Try again in", resp.Header.Get("Retry-After"), "s")
default:
log.Fatalf("Error %d: %s", resp.StatusCode, body.Error)
}
}Save it as main.go and run it with go run main.go.
Response
Found CPF (200, uses 1 credit):
{
"success": true,
"data": {
"cpf": "12345678909",
"name": "Fulano de Tal",
"nameUpper": "FULANO DE TAL",
"gender": "M",
"birthDate": "15/06/1990",
"day": 15,
"month": 6,
"year": 1990
}
}CPF not found (404, does not use a credit):
{
"success": false,
"data": null,
"error": { "message": "CPF não encontrado na base de dados" }
}The CPF 12345678909 is fictional, used only in the examples. gender can be null. The error.message text comes from the API in Portuguese ("CPF not found in the database").
Errors
| Status | What it means | What to do |
|---|---|---|
404 | CPF is not in the database | Treat it as "not found". Does not use a credit. |
400 / 422 | CPF does not have 11 digits or has an invalid check digit | Fix the input. Does not use a credit. |
401 | Missing or invalid API key | Check the CPFHUB_API_KEY variable. |
403 | Credits used up or inactive account | Check your balance with GET /quota or top up in the dashboard. |
429 | Per-minute request limit | Wait the number of seconds in the Retry-After header and try again. |
5xx | Temporary failure | Try again after a few seconds. |
The error field can be text ("error": "...") or an object ("error": { "message": "..." }), depending on the status. The examples above handle both formats. Full list in Error Codes.