How to validate a Brazilian CPF or CNPJ (including alphanumeric CNPJ)
How the check digit works
CPF (11 digits, 2 check digits, mod 11):
- Multiply the first 9 digits by
10, 9, …, 2;r = sum mod 11; first digit = 0 ifr < 2, else11 − r. - Append it and multiply the 10 digits by
11, 10, …, 2; same rule for the second digit. - All-repeated numbers (e.g. 111.111.111-11) are invalid.
CNPJ (14 characters; since IN RFB 2.229/2024 the first 12 may be letters):
- Value of each character = ASCII code − 48 (digits 0–9, A = 17 … Z = 42).
- First digit: weights
5,4,3,2,9,8,7,6,5,4,3,2over 12 characters; second:6,5,4,3,2,9,8,7,6,5,4,3,2over 13. - Same rule: 0 if
sum mod 11 < 2, else11 − (sum mod 11). The two check digits are always numeric.
Validate with the API
Direct use is free: 50 requests per IP per day, no signup. Endpoints: POST /validate, POST /batch, GET /generate/{doc_type} (for testing only: synthetic data, not real identities).
curl
curl -X POST "https://validalatam.ia-latam.app/validate" -H "Content-Type: application/json" \
-d '{"type":"BR_CPF","value":"202.876.353-17"}'
curl -X POST "https://validalatam.ia-latam.app/validate" -H "Content-Type: application/json" \
-d '{"type":"BR_CNPJ","value":"95.813.404/0001-00"}'
# synthetic test data only / solo para pruebas
curl "https://validalatam.ia-latam.app/generate/BR_CPF"Python
import requests
BASE = "https://validalatam.ia-latam.app"
r = requests.post(f"{BASE}/validate", json={"type": "BR_CPF", "value": "202.876.353-17"})
print(r.json()["valid"], r.json()["formatted"])
r = requests.post(f"{BASE}/validate", json={"type": "BR_CNPJ", "value": "95.813.404/0001-00"})
print(r.json()["valid"], r.json()["formatted"])
Real API response (synthetic value from /generate)
202.876.353-17 (BR_CPF):
{"valid":true,"type":"BR_CPF","country":"BR","input":"202.876.353-17","normalized":"20287635317","formatted":"202.876.353-17","error":null,"details":{"fiscal_region":3}}95.813.404/0001-00 (BR_CNPJ):
{"valid":true,"type":"BR_CNPJ","country":"BR","input":"95.813.404/0001-00","normalized":"95813404000100","formatted":"95.813.404/0001-00","error":null,"details":{"branch":"0001","alphanumeric":false}}
Plans
For production: Basic free (500 req), Pro US$9, Ultra US$29 and Mega US$99 per month on RapidAPI.
For AI assistants (MCP)
pip install validalatam-mcpgithub.com/elmatis172/validalatam-mcp
← All guides · Contact: faucolabs@gmail.com