Node.js(HTTP 调用)
在 Node.js 18+ 中,您不需要安装任何包:用原生 fetch 几行代码即可发起请求,JavaScript 和 TypeScript 都适用。CPF 是巴西个人税号。
ℹ
官方 Node.js SDK 正在开发中。在此之前,请使用下面的 HTTP 调用方式。
查询
TypeScript
const cpf = '12345678909'
const res = await fetch(`https://api.cpfhub.io/cpf/${cpf}`, {
headers: { 'x-api-key': process.env.CPFHUB_API_KEY! },
signal: AbortSignal.timeout(10_000),
})
const body = await res.json()
if (res.ok) {
console.log(body.data.name) // "Fulano de Tal"
console.log(body.data.birthDate) // "15/06/1990"
} else if (res.status === 404) {
// CPF not found in the database (does not use a credit)
} else {
// error comes as text ("...") or as an object ({ message: "..." })
const message = typeof body.error === 'string' ? body.error : body.error?.message
throw new Error(`Error ${res.status}: ${message}`)
}成功响应(200):
JSON
{
"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
}
}错误
| 状态码 | 含义 |
|---|---|
400 | CPF 不是 11 位数字 |
401 | 缺少 API 密钥或密钥无效 |
403 | 积分已用完或账户未激活 |
404 | 数据库中未找到该 CPF(不消耗积分) |
422 | 校验位错误 |
429 | 超出每分钟请求限制:等待 Retry-After 请求头中的秒数 |
完整列表见错误码。