Skip to main content

Reference implementation for validating Spanish tax IDs (DNI, NIE, CIF). Pure functions, no dependencies, fully typed.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kreyo_nif_validator-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kreyo_nif_validator-0.1.0-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file kreyo_nif_validator-0.1.0.tar.gz.

File metadata

  • Download URL: kreyo_nif_validator-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for kreyo_nif_validator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6d0ed917a7e1dbdcbc4f84c7b59652048fbf57310a6bc147a4e8acd12aec85aa
MD5 e0419497e6c8e9c42f31bcff8af065bd
BLAKE2b-256 da36fb4e8d704ec7541cf5c8205884c74fa2b533332d7295d89c441a48cefc37

See more details on using hashes here.

File details

Details for the file kreyo_nif_validator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kreyo_nif_validator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 48cccdea9735cb8e88ef709f797978ab4c01c9619fb074e0ef7393095eec7b89
MD5 637ff4282fe156ecfacc688d2884948b
BLAKE2b-256 60db004625d05d9cd3c5e5d1ee86cf28b4aa0f61718a796b7a58c351f4de39c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page