{
  "openapi": "3.1.0",
  "info": {
    "title": "CPFHub.io API",
    "version": "1.0",
    "description": "REST API for querying Brazilian CPF (Cadastro de Pessoas Físicas) identity data. Returns full name, gender, and date of birth for a given CPF number.\n\nCommon use cases: KYC, user onboarding, fraud prevention, form auto-fill.\n\nAuthentication: include your API Key in the `x-api-key` request header. Obtain your key at https://app.cpfhub.io.",
    "contact": {
      "name": "CPFHub.io Support",
      "url": "https://cpfhub.io",
      "email": "suporte@cpfhub.io"
    },
    "license": {
      "name": "Proprietary",
      "url": "https://cpfhub.io/termos"
    },
    "x-llms-txt": "https://cpfhub.io/llms.txt",
    "x-openapi-json": "https://cpfhub.io/openapi.json",
    "x-openapi-yaml": "https://cpfhub.io/openapi.yaml"
  },
  "servers": [
    {
      "url": "https://api.cpfhub.io",
      "description": "Production"
    }
  ],
  "security": [
    { "ApiKeyAuth": [] }
  ],
  "tags": [
    {
      "name": "CPF",
      "description": "Identity lookup by CPF number"
    },
    {
      "name": "Bulk",
      "description": "Asynchronous batch CPF lookups"
    },
    {
      "name": "Quota",
      "description": "Account credit balance and plan status"
    }
  ],
  "paths": {
    "/cpf/bulk": {
      "post": {
        "operationId": "submitBulkCpf",
        "summary": "Submit a batch of CPFs for asynchronous lookup",
        "description": "Accepts a list of CPFs and queues them for background processing. Returns immediately with a `jobId` used to poll `/cpf/bulk/{jobId}` for status.\n\n- Invalid-format CPFs are filtered out before processing and reported in `data.invalid`.\n- Only CPFs found in the database (`found`) consume credits, same rule as `GET /cpf/{cpf}`.\n- Maximum 10,000 CPFs per request (`CPF_BULK_MAX`). Exceeding it returns `400`.\n- Per-CPF results are returned inline by `GET /cpf/bulk/{jobId}` once processed.",
        "tags": ["Bulk"],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/BulkSubmitRequest" },
              "example": {
                "cpfs": ["10415045606", "11144477735", "39114856620"],
                "fileName": "clientes-junho.csv",
                "format": "CSV"
              }
            }
          }
        },
        "responses": {
          "202": {
            "description": "Batch accepted and queued for processing",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkSubmitResponse" },
                "example": {
                  "success": true,
                  "data": {
                    "jobId": "4a1ef25e-052c-4464-9657-4148f53bb0fc",
                    "status": "processing",
                    "totalRecords": 3,
                    "invalid": 0
                  }
                }
              }
            }
          },
          "400": {
            "description": "Missing/non-array/empty `cpfs` field, or `cpfs.length` over the `CPF_BULK_MAX` limit (default 10,000)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkErrorResponse" },
                "examples": {
                  "emptyCpfs": {
                    "value": {
                      "success": false,
                      "data": null,
                      "error": { "message": "Campo 'cpfs' deve ser um array não vazio." }
                    }
                  },
                  "limitExceeded": {
                    "value": {
                      "success": false,
                      "data": null,
                      "error": { "message": "Limite de 10000 CPFs por lote excedido." }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API Key",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" },
                "example": { "success": false, "error": "API Key inválida" }
              }
            }
          },
          "422": {
            "description": "No valid CPF in the submitted list",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkErrorResponse" },
                "example": {
                  "success": false,
                  "data": null,
                  "error": { "message": "Nenhum CPF válido na lista enviada." }
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" }
              }
            }
          }
        }
      }
    },
    "/cpf/bulk/{jobId}": {
      "get": {
        "operationId": "getBulkStatus",
        "summary": "Check the status of a batch job",
        "description": "Returns progress, counts, and per-CPF results for a batch job created via `POST /cpf/bulk`. Poll this endpoint until `data.status` is `done` or `failed`.\n\n- `data.cpfs` contains one object per processed CPF, enriched from the local database for `found: true` rows (same fields as `GET /cpf/{cpf}`).\n- While `status` is `processing`, `data.cpfs` reflects only what has been persisted so far and grows on each poll.\n- `skipped` CPFs (not processed due to insufficient credits) never appear in `data.cpfs`.\n- Unlike `GET /cpf/{cpf}`, the `cpf` field inside `data.cpfs` is **not** formatted (plain 11 digits, no punctuation).",
        "tags": ["Bulk"],
        "parameters": [
          {
            "name": "jobId",
            "in": "path",
            "required": true,
            "description": "The `jobId` returned by `POST /cpf/bulk`.",
            "schema": { "type": "string", "format": "uuid" }
          }
        ],
        "responses": {
          "200": {
            "description": "Job status returned successfully",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkStatusResponse" },
                "example": {
                  "success": true,
                  "data": {
                    "id": "0c64d9d1-5676-4900-9bae-20929b1b0082",
                    "status": "done",
                    "file_name": "teste-docs-v2.csv",
                    "format": "TXT",
                    "total_records": 3,
                    "processed": 3,
                    "found": 3,
                    "not_found": 0,
                    "skipped": 0,
                    "error": null,
                    "created_at": "2026-06-16T12:05:38.066-03:00",
                    "updated_at": "2026-06-16T12:05:38.134-03:00",
                    "cpfs": [
                      {
                        "cpf": "11144477735",
                        "found": true,
                        "name": "Ana de Exemplo",
                        "nameUpper": "ANA DE EXEMPLO",
                        "gender": "F",
                        "birthDate": "01/01/1990",
                        "day": 1,
                        "month": 1,
                        "year": 1990
                      },
                      {
                        "cpf": "00000000000",
                        "found": false,
                        "name": null,
                        "nameUpper": null,
                        "gender": null,
                        "birthDate": null,
                        "day": null,
                        "month": null,
                        "year": null
                      }
                    ]
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API Key",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" },
                "example": { "success": false, "error": "API Key inválida" }
              }
            }
          },
          "404": {
            "description": "Job not found or does not belong to your account",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkErrorResponse" },
                "example": {
                  "success": false,
                  "data": null,
                  "error": { "message": "Lote não encontrado." }
                }
              }
            }
          }
        }
      }
    },
    "/cpf/{cpf}": {
      "get": {
        "operationId": "getCpf",
        "summary": "Look up a CPF",
        "description": "Returns identity data associated with the given CPF number.\n\n- **Not found (404) does not consume a credit.** Only successful 200 responses are billed.\n- The `gender` field is inferred from the full name.\n- The `cpf` field in the response is always returned formatted as XXX.XXX.XXX-XX.",
        "tags": ["CPF"],
        "parameters": [
          {
            "name": "cpf",
            "in": "path",
            "required": true,
            "description": "CPF number with or without formatting. Valid inputs: `12345678909` or `123.456.789-09`.",
            "schema": {
              "type": "string",
              "pattern": "^\\d{11}$|^\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}$",
              "example": "12345678909"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "CPF found — identity data returned",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/CPFResponse" },
                "example": {
                  "success": true,
                  "data": {
                    "cpf": "123.456.789-09",
                    "name": "John Doe",
                    "nameUpper": "JOHN DOE",
                    "gender": "M",
                    "birthDate": "15/06/1990",
                    "day": 15,
                    "month": 6,
                    "year": 1990
                  }
                }
              }
            }
          },
          "400": {
            "description": "Invalid CPF format",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "INVALID_CPF_FORMAT",
                    "message": "Invalid CPF format. Expected 11 digits."
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API Key",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "INVALID_API_KEY",
                    "message": "The provided API key is invalid."
                  }
                }
              }
            }
          },
          "403": {
            "description": "Suspended API key or insufficient credits",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "examples": {
                  "suspendedKey": {
                    "value": {
                      "success": false,
                      "error": {
                        "code": "SUSPENDED_API_KEY",
                        "message": "Your API key has been suspended. Contact support."
                      }
                    }
                  },
                  "insufficientCredits": {
                    "value": {
                      "success": false,
                      "error": {
                        "code": "INSUFFICIENT_CREDITS",
                        "message": "Your account has no remaining credits."
                      }
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "CPF not found — does not consume a credit",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "CPF_NOT_FOUND",
                    "message": "CPF not found in our database."
                  }
                }
              }
            }
          },
          "422": {
            "description": "CPF check digits are invalid",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "INVALID_CPF_DIGITS",
                    "message": "CPF check digits are invalid."
                  }
                }
              }
            }
          },
          "429": {
            "description": "Rate limit exceeded",
            "headers": {
              "Retry-After": {
                "description": "Seconds until the next request will be accepted",
                "schema": { "type": "integer", "example": 1 }
              }
            },
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "RATE_LIMIT_EXCEEDED",
                    "message": "Too many requests. Please retry after 1 second."
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal server error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "INTERNAL_ERROR",
                    "message": "An unexpected error occurred. Please try again."
                  }
                }
              }
            }
          },
          "503": {
            "description": "API temporarily unavailable",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/ErrorResponse" },
                "example": {
                  "success": false,
                  "error": {
                    "code": "SERVICE_UNAVAILABLE",
                    "message": "API temporarily unavailable. Please try again shortly."
                  }
                }
              }
            }
          }
        }
      }
    },
    "/quota": {
      "get": {
        "operationId": "getQuota",
        "summary": "Check credit balance and plan status",
        "description": "Returns the current plan, remaining credits, and billing status for the authenticated account. Read-only — never consumes a credit.\n\n- **Works even when the credit pool is exhausted.** Unlike other endpoints, `/quota` is exempt from the \"insufficient credits\" block, so you can always check your balance.\n- Response payload is identical to the MCP tool `get_quota_info`.",
        "tags": ["Quota"],
        "responses": {
          "200": {
            "description": "Quota returned successfully",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/QuotaResponse" },
                "example": {
                  "success": true,
                  "data": {
                    "plan": "Professional",
                    "remainingCredits": 4500,
                    "billingStatus": "active",
                    "userId": "123e4567-e89b-12d3-a456-426614174000",
                    "email": "usuario@exemplo.com"
                  }
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid API Key",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" },
                "example": { "success": false, "error": "API Key inválida" }
              }
            }
          },
          "403": {
            "description": "Inactive user account",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" },
                "example": { "success": false, "error": "Usuário inativo. Entre em contato com o suporte." }
              }
            }
          },
          "500": {
            "description": "Internal server error (subscription lookup failed)",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/BulkAuthErrorResponse" },
                "example": { "success": false, "error": "Erro ao validar assinatura" }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiKeyAuth": {
        "type": "apiKey",
        "in": "header",
        "name": "x-api-key",
        "description": "Obtain your API Key at https://app.cpfhub.io. Include it in every request: `x-api-key: your-api-key`"
      }
    },
    "schemas": {
      "CPFResponse": {
        "type": "object",
        "required": ["success", "data"],
        "description": "Successful CPF lookup response",
        "properties": {
          "success": {
            "type": "boolean",
            "description": "Always true for 200 responses",
            "example": true
          },
          "data": { "$ref": "#/components/schemas/CPFData" }
        }
      },
      "CPFData": {
        "type": "object",
        "required": ["cpf", "name", "nameUpper", "gender", "birthDate", "day", "month", "year"],
        "description": "Identity data associated with the CPF",
        "properties": {
          "cpf": {
            "type": "string",
            "description": "Formatted CPF (XXX.XXX.XXX-XX)",
            "example": "123.456.789-09"
          },
          "name": {
            "type": "string",
            "description": "Full name in title case",
            "example": "John Doe"
          },
          "nameUpper": {
            "type": "string",
            "description": "Full name in uppercase",
            "example": "JOHN DOE"
          },
          "gender": {
            "type": "string",
            "enum": ["M", "F"],
            "description": "Gender inferred from the full name (M = male, F = female)",
            "example": "M"
          },
          "birthDate": {
            "type": "string",
            "description": "Date of birth in DD/MM/YYYY format",
            "pattern": "^\\d{2}/\\d{2}/\\d{4}$",
            "example": "15/06/1990"
          },
          "day": {
            "type": "integer",
            "minimum": 1,
            "maximum": 31,
            "description": "Day of birth",
            "example": 15
          },
          "month": {
            "type": "integer",
            "minimum": 1,
            "maximum": 12,
            "description": "Month of birth",
            "example": 6
          },
          "year": {
            "type": "integer",
            "minimum": 1900,
            "description": "Year of birth",
            "example": 1990
          }
        }
      },
      "QuotaResponse": {
        "type": "object",
        "required": ["success", "data"],
        "description": "Successful quota/credit balance response",
        "properties": {
          "success": {
            "type": "boolean",
            "description": "Always true for 200 responses",
            "example": true
          },
          "data": { "$ref": "#/components/schemas/QuotaData" }
        }
      },
      "QuotaData": {
        "type": "object",
        "required": ["plan", "remainingCredits", "billingStatus", "userId", "email"],
        "description": "Credit balance and plan status for the authenticated account",
        "properties": {
          "plan": {
            "type": "string",
            "description": "Current plan name, or \"unknown\" if the account has no active subscription",
            "example": "Professional"
          },
          "remainingCredits": {
            "type": "integer",
            "description": "Credits available for new lookups in the current billing period",
            "example": 4500
          },
          "billingStatus": {
            "type": "string",
            "enum": ["active", "free"],
            "description": "Billing status of the account",
            "example": "active"
          },
          "userId": {
            "type": "string",
            "format": "uuid",
            "description": "Unique identifier of the user",
            "example": "123e4567-e89b-12d3-a456-426614174000"
          },
          "email": {
            "type": "string",
            "format": "email",
            "description": "Email address associated with the account",
            "example": "usuario@exemplo.com"
          }
        }
      },
      "BulkSubmitRequest": {
        "type": "object",
        "required": ["cpfs"],
        "description": "Request body for submitting a batch of CPFs",
        "properties": {
          "cpfs": {
            "type": "array",
            "items": { "type": "string" },
            "minItems": 1,
            "description": "List of CPFs, formatted or not. Max 10,000 per request (`CPF_BULK_MAX`) — exceeding it returns 400.",
            "example": ["10415045606", "11144477735", "39114856620"]
          },
          "fileName": {
            "type": "string",
            "description": "Optional reference name for the batch, shown in history.",
            "example": "clientes-junho.csv"
          },
          "format": {
            "type": "string",
            "enum": ["CSV", "TXT", "Texto"],
            "description": "Optional, informational only — origin format of the batch."
          }
        }
      },
      "BulkSubmitResponse": {
        "type": "object",
        "required": ["success", "data"],
        "description": "Response after a batch is accepted",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "data": {
            "type": "object",
            "properties": {
              "jobId": { "type": "string", "format": "uuid", "description": "Use to poll /cpf/bulk/{jobId}" },
              "status": { "type": "string", "enum": ["processing"] },
              "totalRecords": { "type": "integer", "description": "Valid CPFs accepted for processing" },
              "invalid": { "type": "integer", "description": "CPFs discarded for invalid format" }
            }
          }
        }
      },
      "BulkStatusResponse": {
        "type": "object",
        "required": ["success", "data"],
        "description": "Aggregate status of a batch job",
        "properties": {
          "success": { "type": "boolean", "example": true },
          "data": {
            "type": "object",
            "properties": {
              "id": { "type": "string", "format": "uuid" },
              "status": { "type": "string", "enum": ["processing", "done", "failed"] },
              "file_name": { "type": "string", "nullable": true },
              "format": { "type": "string", "nullable": true },
              "total_records": { "type": "integer" },
              "processed": { "type": "integer" },
              "found": { "type": "integer", "description": "Consumes 1 credit each" },
              "not_found": { "type": "integer", "description": "Does not consume credits" },
              "skipped": { "type": "integer", "description": "Not processed due to insufficient credits" },
              "error": { "type": "string", "nullable": true },
              "created_at": { "type": "string", "format": "date-time" },
              "updated_at": { "type": "string", "format": "date-time" },
              "cpfs": {
                "type": "array",
                "description": "Per-CPF results, populated incrementally as the job processes. Skipped CPFs never appear here.",
                "items": { "$ref": "#/components/schemas/BulkResultItem" }
              }
            }
          }
        }
      },
      "BulkResultItem": {
        "type": "object",
        "required": ["cpf", "found", "name", "nameUpper", "gender", "birthDate", "day", "month", "year"],
        "description": "Per-CPF result inside BulkStatusResponse.data.cpfs",
        "properties": {
          "cpf": { "type": "string", "description": "Unformatted CPF (11 digits, no punctuation) — unlike GET /cpf/{cpf}", "example": "11144477735" },
          "found": { "type": "boolean", "description": "Whether the CPF was found in the database (consumes 1 credit)" },
          "name": { "type": "string", "nullable": true, "example": "Ana de Exemplo" },
          "nameUpper": { "type": "string", "nullable": true, "example": "ANA DE EXEMPLO" },
          "gender": { "type": "string", "nullable": true, "enum": ["M", "F", null], "example": "F" },
          "birthDate": { "type": "string", "nullable": true, "example": "01/01/1990" },
          "day": { "type": "integer", "nullable": true, "example": 1 },
          "month": { "type": "integer", "nullable": true, "example": 1 },
          "year": { "type": "integer", "nullable": true, "example": 1990 }
        }
      },
      "BulkErrorResponse": {
        "type": "object",
        "required": ["success", "error"],
        "description": "Error response for /cpf/bulk validation and lookup errors (does not include an error code field)",
        "properties": {
          "success": { "type": "boolean", "example": false },
          "data": { "nullable": true },
          "error": {
            "type": "object",
            "required": ["message"],
            "properties": {
              "message": { "type": "string", "example": "Lote não encontrado." }
            }
          }
        }
      },
      "BulkAuthErrorResponse": {
        "type": "object",
        "required": ["success", "error"],
        "description": "Error response for /cpf/bulk authentication errors — error is a plain string, unlike other endpoints",
        "properties": {
          "success": { "type": "boolean", "example": false },
          "error": { "type": "string", "example": "API Key inválida" }
        }
      },
      "ErrorResponse": {
        "type": "object",
        "required": ["success", "error"],
        "description": "Error response",
        "properties": {
          "success": {
            "type": "boolean",
            "description": "Always false for error responses",
            "example": false
          },
          "error": {
            "type": "object",
            "required": ["code", "message"],
            "properties": {
              "code": {
                "type": "string",
                "description": "Machine-readable error code",
                "enum": [
                  "INVALID_CPF_FORMAT",
                  "INVALID_CPF_DIGITS",
                  "MISSING_API_KEY",
                  "INVALID_API_KEY",
                  "SUSPENDED_API_KEY",
                  "INSUFFICIENT_CREDITS",
                  "CPF_NOT_FOUND",
                  "RATE_LIMIT_EXCEEDED",
                  "INTERNAL_ERROR",
                  "SERVICE_UNAVAILABLE"
                ],
                "example": "CPF_NOT_FOUND"
              },
              "message": {
                "type": "string",
                "description": "Human-readable error description",
                "example": "CPF not found in our database."
              }
            }
          }
        }
      }
    }
  }
}
