Skip to main content

nif-validator

Reference implementation for validating Spanish tax identification numbers (DNI, NIE, CIF) in .NET, TypeScript, and Python.

Zero dependencies. Pure functions. Verified against real public CIFs of Spanish companies.

License: MIT CI

🇪🇸 Para developers en español: esta librería valida números de identificación fiscal españoles (DNI, NIE, CIF) en .NET, JavaScript/TypeScript y Python. Implementación de referencia, sin dependencias, con tests sobre CIFs reales de empresas españolas. Documentación completa en español →


Why this library exists

Validating a Spanish NIF correctly is harder than it looks. The algorithm has three variants (DNI, NIE, CIF), the CIF check digit follows specific rules per organization type, and most snippets you find online are incomplete or wrong.

This is a clean reference implementation, kept identical across three languages, verified with the same fixtures, and tested against real public Spanish company CIFs (Banco Santander, BBVA, Inditex, Telefónica, Repsol, Iberdrola).

If you're building anything that touches Spanish tax IDs — invoicing software, accounting tools, KYC flows, e-commerce checkouts — this library is for you.

Install

.NET (multi-target: net8.0 + netstandard2.0)

dotnet add package Kreyo.NifValidator

TypeScript / JavaScript

npm install @kreyo/nif-validator
# or
pnpm add @kreyo/nif-validator
# or
yarn add @kreyo/nif-validator

Python (3.10+)

pip install kreyo-nif-validator

Usage

.NET

using Kreyo.NifValidator;

NifValidator.IsValid("12345678Z");          // true
NifValidator.IsValid("X1234567L");          // true (NIE)
NifValidator.IsValid("A39000013");          // true (Banco Santander)
NifValidator.IsValid("12345678A");          // false (wrong control letter)

NifValidator.Normalize(" 12345678-z ");     // "12345678Z"
NifValidator.GetNifType("X1234567L");       // "NIE"
NifValidator.GetNifType("invalid");         // null

// Optional: include deprecated types K, L, M, T (pre-2008)
NifValidator.IsValid("K12345674", new NifValidatorOptions { IncludeDeprecated = true });

TypeScript

import { isValid, normalize, getNifType } from "@kreyo/nif-validator";

isValid("12345678Z");           // true
isValid("X1234567L");           // true (NIE)
isValid("A39000013");           // true (Banco Santander)
isValid("12345678A");           // false

normalize(" 12345678-z ");      // "12345678Z"
getNifType("X1234567L");        // "NIE"
getNifType("invalid");          // null

isValid("K12345674", { includeDeprecated: true });  // true

Python

from nif_validator import is_valid, normalize, get_nif_type, ValidationOptions

is_valid("12345678Z")           # True
is_valid("X1234567L")           # True (NIE)
is_valid("A39000013")           # True (Banco Santander)
is_valid("12345678A")           # False

normalize(" 12345678-z ")       # "12345678Z"
get_nif_type("X1234567L")       # "NIE"
get_nif_type("invalid")         # None

is_valid("K12345674", ValidationOptions(include_deprecated=True))  # True

What gets validated

Type Format Example Notes
DNI 8 digits + control letter 12345678Z For Spanish citizens
NIE X/Y/Z + 7 digits + letter X1234567L For foreign residents
CIF Letter + 7 digits + letter or digit A39000013 For companies and other legal entities

CIF type letters and their control rules:

Type Meaning Control
A Sociedad Anónima digit only
B Sociedad Limitada digit only
C Sociedad Colectiva digit or letter
D Sociedad Comanditaria digit or letter
E Comunidad de Bienes digit only
F Cooperativa digit or letter
G Asociación digit or letter
H Comunidad de Propietarios digit only
J Sociedad Civil digit or letter
N No residente letter only
P Corporación local letter only
Q Organismo público letter only
R Religiosa letter only
S Órgano de la Administración letter only
U Unión Temporal de Empresas digit or letter
V Otros tipos digit or letter
W Establecimiento permanente extranjero letter only
K, L, M, T Deprecated by AEAT (pre-2008) only with includeDeprecated

What this library does NOT do

  • It does not check whether a NIF is registered with AEAT. This is a syntactic and check-digit validation. A NIF can be mathematically valid and still not exist in the tax registry. To verify that a NIF is active, query VIES (for EU tax IDs) or AEAT's services directly.
  • It does not validate the company name or address. Just the format of the identifier.
  • It does not detect "ghost" NIFs (mathematically valid but known to be unused). That's a business-rule concern, not a format-validation one.

API

All three implementations expose the same three functions:

Function Returns Throws
isValid(nif, options?) boolean never
normalize(nif) canonical string on invalid
getNifType(nif) "DNI" | "NIE" | "CIF" | null never

Whitespace (spaces, tabs, newlines) and hyphens are ignored everywhere. Input is case-insensitive.

Algorithm details

For technical details on the validation algorithm (DNI letter calculation, NIE prefix mapping, CIF check digit/letter computation), see docs/algorithm.md.

Made by Kreyo

This library is part of Kreyo, a developer-first platform of APIs for Spanish electronic invoicing: VeriFactu, FacturaE, AI invoice extraction, and PDF generation.

If you're building software that needs Spanish tax compliance, you might also like:

  • Kreyo VeriFactu — generate, sign, and submit AEAT registros in real time
  • Kreyo FacturaE — generate signed FacturaE 3.2.2 XML for B2G and B2B
  • Kreyo Extract — extract structured data from received invoices with AI
  • Kreyo PDF — generate invoices and documents as PDFs

Contributing

Issues and PRs welcome. Please see CONTRIBUTING.md for details.

License

MIT © Kreyo

Metadata

Release files for kreyo-nif-validator 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kreyo-nif-validator 0.1.0
File Size Uploaded Provenance
kreyo_nif_validator-0.1.0.tar.gz 7.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kreyo-nif-validator 0.1.0
File Interpreter ABI Platform
kreyo_nif_validator-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / kreyo_nif_validator-0.1.0.tar.gz

Download URL kreyo_nif_validator-0.1.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6d0ed917a7e1dbdcbc4f84c7b59652048fbf57310a6bc147a4e8acd12aec85aa
BLAKE2b-256 checksum
How to use checksums
da36fb4e8d704ec7541cf5c8205884c74fa2b533332d7295d89c441a48cefc37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / kreyo_nif_validator-0.1.0-py3-none-any.whl

Download URL kreyo_nif_validator-0.1.0-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
48cccdea9735cb8e88ef709f797978ab4c01c9619fb074e0ef7393095eec7b89
BLAKE2b-256 checksum
How to use checksums
60db004625d05d9cd3c5e5d1ee86cf28b4aa0f61718a796b7a58c351f4de39c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Security for kreyo-nif-validator 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page