stdnum-centroamerica
Validate, format and compact Central American identity and tax document numbers: DPI, NIT, DUI, NRC, RTN, cédulas, RUC, DIMEX and passport MRZs. No dependencies, fully typed, Python 3.11 or later. Works in any project: scripts, FastAPI, Flask, Django, DRF, Celery or notebooks.
Installation
With uv (recommended):
uv add stdnum-centroamerica
uv add "stdnum-centroamerica[pydantic]" # FastAPI / Pydantic v2 integration
uv add "stdnum-centroamerica[django]" # Django / DRF integration
uv add "stdnum-centroamerica[wtforms]" # Flask-WTF / WTForms integration
With pip, inside a virtual environment:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install stdnum-centroamerica
python -m pip install "stdnum-centroamerica[django]"
More options (Poetry, PDM, installing from GitHub, editable mode, hashes) in docs/installation.md.
Usage
Each document has a country.document key (ni.cedula, gt.dpi, ...) and three operations: validate, format and compact.
>>> import stdnum_centroamerica as sca
>>> sca.is_valid("ni.cedula", "001-010190-0001N")
True
>>> sca.is_valid("ni.ruc", "J0310000000001") # legal entity
True
>>> sca.is_valid("ni.ruc", "0000300766011M") # a RUC cannot start with four zeros
False
>>> sca.make("ni.cedula").format("0010101900001n")
'001-010190-0001N'
>>> sca.make("ni.cedula").compact("001-010190-0001n")
'0010101900001N'
>>> sca.make("gt.dpi").format("1234567890101")
'1234 56789 0101'
validate() also returns the reason for a failure, which is useful for error messages:
>>> result = sca.validate("sv.dui", "12345678-5")
>>> result.is_valid
False
>>> result.error
<ErrorCode.CHECKSUM: 'checksum'>
>>> bool(sca.validate("sv.dui", "12345678-4"))
True
ErrorCode |
Value | Meaning |
|---|---|---|
FORMAT |
"format" |
wrong length, characters or structure |
COMPONENT |
"component" |
a component (date, department, municipality, prefix) is out of range |
CHECKSUM |
"checksum" |
the check digit or letter does not match |
ErrorCode is a StrEnum, so it serializes to JSON as plain text.
Options
The Nicaraguan cédula does not check its letter by default, because the CSE does not publish the formula (modulo 23, from unofficial sources). Options are passed as keyword arguments:
>>> sca.is_valid("ni.cedula", "001-010190-0001A") # the correct letter is N
True
>>> sca.is_valid("ni.cedula", "001-010190-0001A", strict_letter=True)
False
>>> sca.is_valid("ni.cedula", "282-010190-0001A", strict_municipality=True) # 282 is not in the list
False
The document classes can also be used directly:
>>> from stdnum_centroamerica.ni import Cedula
>>> Cedula(strict_municipality=True).is_valid("001-010190-0001N")
True
>>> Cedula.check_letter("0010101900001")
'N'
A misspelled option raises TypeError instead of being silently ignored.
Parsing components
parse() reads the components encoded in the number and returns an immutable object, or None if the number is not valid. It is available for ni.cedula, hn.identidad, gt.dpi, pa.cedula and the three MRZ formats.
>>> cedula = sca.parse("ni.cedula", "001-010190-0001N")
>>> cedula.municipality_code, cedula.birth_day, cedula.birth_month, cedula.birth_year_two_digits
(1, 1, 1, 90)
>>> import datetime
>>> cedula.birth_year_candidates(datetime.date(2026, 11, 5))
(1990,)
>>> sca.parse("hn.identidad", "0906-1990-00001").municipality_code
6
>>> sca.parse("gt.dpi", "1234 56789 0101").department_code
1
>>> sca.parse("pa.cedula", "PE-5-687").kind
<CedulaKind.BORN_ABROAD: 'born_abroad'>
>>> mrz = "P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\nL898902C36UTO7408122F1204159ZE184226B<<<<<10"
>>> sca.is_valid("mrz.td3", mrz)
True
>>> sca.parse("mrz.td3", mrz).surname
'ERIKSSON'
The number does not encode the century, so birth_year_candidates() returns the possible years (it may be empty: 29/02/01 exists neither in 1901 nor in 2001). For the same reason it cannot be used to decide legal age: someone born in 2010 also gets the candidate 1910.
Components come from the structure of the number and are not checked against any registry. Birth dates and the other components are personal data: they are hidden from repr() so they do not end up in a log by accident, but treat them accordingly (see docs/security.md).
Test data
generate() returns a synthetic valid number, useful for fixtures and sample data. It is random and may match a real number by chance: do not use it as data about people.
>>> sca.is_valid("gt.dpi", sca.generate("gt.dpi"))
True
Catalog and custom documents
>>> len(sca.keys())
18
>>> info = sca.describe()[0]
>>> info.key, info.country, info.name, info.level
('gt.nit', 'GT', 'NIT', <Level.COMPLETE: 'complete'>)
An unknown key raises UnknownDocumentError (with suggestions when it resembles an existing key). sca.register(key, cls) adds or replaces a document; the class must implement the Document protocol (is_valid, validate, format, compact). Call it at application startup.
Supported documents
complete = validates format and check digit; format = validates length, characters and basic ranges.
| Country | Document | Key | Level |
|---|---|---|---|
| Guatemala | NIT | gt.nit |
complete |
| Guatemala | DPI (CUI) | gt.dpi |
complete (check digit and department) |
| El Salvador | DUI | sv.dui |
complete |
| El Salvador | NIT | sv.nit |
complete (also accepts the 9-digit DUI) |
| El Salvador | NRC | sv.nrc |
format |
| Honduras | Número de identidad | hn.identidad |
format (valid department and municipality) |
| Honduras | RTN | hn.rtn |
format |
| Nicaragua | Cédula | ni.cedula |
format (complete with strict_letter=True: the check letter is off by default) |
| Nicaragua | RUC | ni.ruc |
format |
| Costa Rica | Cédula física | cr.cedula_fisica |
format |
| Costa Rica | Cédula jurídica | cr.cedula_juridica |
format |
| Costa Rica | DIMEX | cr.dimex |
format |
| Costa Rica | NITE | cr.nite |
format |
| Panama | Cédula | pa.cedula |
format |
| Panama | RUC | pa.ruc |
format (the DV is not validated) |
| International | Passport MRZ (TD3) | mrz.td3 |
complete (ICAO 9303) |
| International | ID card MRZ (TD1) | mrz.td1 |
complete (ICAO 9303) |
| International | ID document MRZ (TD2) | mrz.td2 |
complete (ICAO 9303) |
Notes:
- Panama: volume and entry have variable length, so the number can only be read with its hyphens:
compact()turns both1-12-345and11-2-345into112345. Storesca.canonical(key, number), which keeps them (8-926-1601) and drops leading zeros (8-0926-01601is the same number). - All zeros: a number whose digits are all zero (
00000000-0,00) is rejected withCOMPONENT: it is a placeholder, even when its check digit adds up. - Honduras:
hn.identidadaccepts the special codes for naturalized citizens (0890) and dual nationality (0880). - MRZ: accepted with or without line breaks. Dates stay as
YYMMDD, because the century cannot be inferred. Country-specific passport numbers are not validated, only the MRZ. - Nicaragua: the municipality codes and the check letter come from third parties (the CSE does not publish them), and code 888 has a single source. In Panama, provinces 10 to 13 and the PI and AV suffixes are not official either.
- The source of every rule, with the date it was consulted, is in docs/sources.md.
Framework integrations
The core works in any project with no configuration. The integrations are optional and report errors in each framework's own format. Their messages default to English and can be overridden (see each guide).
FastAPI / Pydantic v2 (guide):
from typing import Annotated
from pydantic import BaseModel
from stdnum_centroamerica.contrib.pydantic import Document
Dpi = Annotated[str, Document("gt.dpi")] # validates and returns the canonical form to store
class Customer(BaseModel):
dpi: Dpi
cedula: Annotated[str, Document("ni.cedula", normalize="format", strict_letter=True)]
# When invalid, FastAPI responds with 422 and type="stdnum_checksum" (or format / component).
Django and DRF (guide):
from stdnum_centroamerica.contrib.django import DocumentFormField, DocumentValidator
class Customer(models.Model):
dpi = models.CharField(max_length=13, validators=[DocumentValidator("gt.dpi")])
class CustomerForm(forms.Form):
dpi = DocumentFormField("gt.dpi") # cleaned_data["dpi"] is the canonical form (compact)
Flask (guide):
from stdnum_centroamerica.contrib.wtforms import Document
class CustomerForm(FlaskForm):
dpi = StringField("DPI", validators=[DataRequired(), Document("gt.dpi")])
Complete, tested examples in examples/.
Security
- No dependencies, no network access and no I/O: the package never stores or logs numbers.
- Only
strinputs are accepted: any other type raisesTypeError. Inputs longer than 256 characters or with non-ASCII characters are aFORMATerror before any regular expression runs. This rejects digits from other scripts, Unicode hyphens and invisible spaces. - Regular expressions run in linear time (no catastrophic backtracking), and the tests check this with adversarial inputs.
- Instances and data tables are immutable, so they can be shared between threads.
Details and recommendations for integrators in docs/security.md. To report a vulnerability, see SECURITY.md.
Development
uv sync
uv run pytest # tests (including Hypothesis property tests)
uv run ruff check . && uv run ruff format --check .
uv run mypy # strict mode
See CONTRIBUTING.md for the branching model and how to add a document.
License
MIT
Metadata
Release files for stdnum-centroamerica 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| stdnum_centroamerica-0.2.0.tar.gz | 33.2 kB | With provenance | Details for stdnum_centroamerica-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stdnum_centroamerica-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for stdnum_centroamerica-0.2.0-py3-none-any.whl |
Total release size: 77.3 kB
Release files / stdnum_centroamerica-0.2.0.tar.gz
| Download URL | stdnum_centroamerica-0.2.0.tar.gz |
|---|---|
| Size | 33.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e6207f9620efc4be0ab4b0553388c1cb59894270dff57f8012dda08dce39049
|
|
BLAKE2b-256 checksum How to use checksums |
23fef7e4fda506d8fc488aca673580ebc083970c0fdd675530ca33e3549138df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logRelease files / stdnum_centroamerica-0.2.0-py3-none-any.whl
| Download URL | stdnum_centroamerica-0.2.0-py3-none-any.whl |
|---|---|
| Size | 44.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9b8deaee5fd8a692a594739a28fab3705cfb32a3349a6346378af8c59fcec83f
|
|
BLAKE2b-256 checksum How to use checksums |
587777fc3c6155d315e98d4e1bc60b3dc247c30d5219aa48acb5fd83abb3efb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logSecurity for stdnum-centroamerica 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for stdnum_centroamerica-0.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.