.NET / C#
Consulte CPF com HttpClient e System.Text.Json nativos do .NET. Sem pacotes externos.
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
- .NET 6+
- Uma API Key em app.cpfhub.io
Exemplo
C#
using System.Net.Http;
using System.Net.Http.Json;
var cpf = "12345678909";
var apiKey = Environment.GetEnvironmentVariable("CPFHUB_API_KEY")!;
using var http = new HttpClient();
http.DefaultRequestHeaders.Add("x-api-key", apiKey);
var response = await http.GetFromJsonAsync<ApiResponse>(
$"https://api.cpfhub.io/cpf/{cpf}"
);
Console.WriteLine(response!.Data.Name); // "Fulano de Tal"
Console.WriteLine(response!.Data.BirthDate); // "15/06/1990"
Console.WriteLine(response!.Data.Gender); // "M"
record ApiResponse(CpfData Data);
record CpfData(
string Cpf,
string Name,
string NameUpper,
string Gender,
string BirthDate,
int Day,
int Month,
int Year
);Com IHttpClientFactory (ASP.NET Core)
C#
// Program.cs
builder.Services.AddHttpClient("cpfhub", client =>
{
client.BaseAddress = new Uri("https://api.cpfhub.io/");
client.DefaultRequestHeaders.Add("x-api-key",
builder.Configuration["CPFHUB_API_KEY"]);
});
// CpfService.cs
public class CpfService(IHttpClientFactory factory)
{
public async Task<CpfData?> LookupAsync(string cpf)
{
var client = factory.CreateClient("cpfhub");
var response = await client.GetFromJsonAsync<ApiResponse>($"cpf/{cpf}");
return response?.Data;
}
}Campos retornados
| Propriedade | JSON | Exemplo |
|---|---|---|
Cpf | cpf | "12345678909" |
Name | name | "Fulano de Tal" |
NameUpper | nameUpper | "FULANO DE TAL" |
Gender | gender | "M" |
BirthDate | birthDate | "15/06/1990" |
Day | day | 15 |
Month | month | 6 |
Year | year | 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