Go
Consulte CPF com net/http da biblioteca padrão. Sem dependências externas.
Integrando com Cursor, Lovable, v0 ou outra IA?
Copie o prompt de integração e cole direto no seu assistente de IA - ele saberá exatamente como usar a API.
Pré-requisitos
- Go 1.18+
- Uma API Key em app.cpfhub.io
Exemplo
Go
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
)
type Response struct {
Data struct {
Name string `json:"name"`
BirthDate string `json:"birthDate"`
Gender string `json:"gender"`
} `json:"data"`
}
func main() {
cpf := "12345678909"
apiKey := os.Getenv("CPFHUB_API_KEY")
req, err := http.NewRequest("GET", "https://api.cpfhub.io/cpf/"+cpf, nil)
if err != nil {
panic(err)
}
req.Header.Set("x-api-key", apiKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
panic(fmt.Sprintf("API error: %d", resp.StatusCode))
}
var result Response
json.NewDecoder(resp.Body).Decode(&result)
fmt.Println(result.Data.Name) // "Fulano de Tal"
fmt.Println(result.Data.BirthDate) // "15/06/1990"
fmt.Println(result.Data.Gender) // "M"
}Campos retornados
| Campo JSON | Tipo Go | Exemplo |
|---|---|---|
name | string | "Fulano de Tal" |
nameUpper | string | "FULANO DE TAL" |
gender | string | "M" |
birthDate | string | "15/06/1990" |
day | int | 15 |
month | int | 6 |
year | int | 1990 |
Tratamento de erros
| HTTP | error.code | Descrição |
|---|---|---|
| 404 | CPF_NOT_FOUND | CPF não consta na base |
| 422 | INVALID_CPF_DIGITS | Dígitos verificadores inválidos |
| 429 | RATE_LIMIT_EXCEEDED | Muitas requisições |
| 401 | UNAUTHORIZED | API Key inválida ou ausente |
Links
Atualizado em 17 de maio de 2026