CPFHub.io
Start for free

Look up a CPF with Express

Look up a CPF (Brazil's individual taxpayer ID) with Express by creating an endpoint on your server that calls the CPFHub.io API with the native fetch of Node.js 18+. The API key stays on the server only.

Using Cursor, Lovable, v0 or another AI? Copy the Simple Lookup prompt and paste it into your assistant.

Open in Cursor

Before you start

  1. Create a free account at app.cpfhub.io: the Free plan includes 50 credits and does not ask for a card.
  2. Copy your API key from app.cpfhub.io/api-keys.
  3. Store the key in an environment variable, never in your code:
bash
export CPFHUB_API_KEY="your_api_key"

Install the dependency:

bash
npm install express

Example

JavaScript
// server.js
const express = require('express')
const app = express()

app.get('/cpf/:cpf', async (req, res) => {
  const cpf = req.params.cpf.replace(/\D/g, '')
  if (cpf.length !== 11) {
    return res.status(400).json({ error: 'CPF must have 11 digits' })
  }

  try {
    const response = 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 response.json()

    if (response.ok) return res.json(body.data)
    if (response.status === 404) return res.status(404).json({ error: 'CPF not found' })
    if (response.status === 422) return res.status(422).json({ error: 'Invalid CPF' })

    // 401, 403, 429 and 5xx: key, credit or limit problem. Log it and do not expose it to the client.
    console.error('CPFHub.io', response.status, body.error)
    res.status(503).json({ error: 'CPF lookup unavailable at the moment' })
  } catch (err) {
    console.error('CPFHub.io', err)
    res.status(503).json({ error: 'CPF lookup unavailable at the moment' })
  }
})

app.listen(3000)

Run it with node server.js and test with curl http://localhost:3000/cpf/12345678909.

Response

Found CPF (200, uses 1 credit):

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
  }
}

CPF not found (404, does not use a credit):

JSON
{
  "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

StatusWhat it meansWhat to do
404CPF is not in the databaseTreat it as "not found". Does not use a credit.
400 / 422CPF does not have 11 digits or has an invalid check digitFix the input. Does not use a credit.
401Missing or invalid API keyCheck the CPFHUB_API_KEY variable.
403Credits used up or inactive accountCheck your balance with GET /quota or top up in the dashboard.
429Per-minute request limitWait the number of seconds in the Retry-After header and try again.
5xxTemporary failureTry 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.

Next steps