Spain VAT number (NIF / NIE / CIF)
Spain has three distinct identifier types under the VAT umbrella: the NIF for Spanish individuals, the NIE for foreign nationals, and the CIF for legal entities. Each has its own checksum logic — VAT-Scan identifies the type and runs the correct algorithm automatically.
Spain's VAT system uses a single 9-character body (after the ES prefix) but the first and last character determines which of three identifier types it is. The Agencia Tributaria (AEAT) administers all three. Understanding which type you're dealing with is essential because the validation rules differ completely.
NIF — Número de Identificación Fiscal
For Spanish individuals and sole traders. Format: 8 digits followed by a letter control character. The letter is derived from the 8-digit number using modulo 23 against the table TRWAGMYFPDXBNJZSQVHLCKE. Example: ES12345678Z.
NIE — Número de Identidad de Extranjero
For foreign nationals resident in Spain. Format: X, Y, or Z prefix + 7 digits + letter. The letter is computed the same way as NIF, after substituting X→0, Y→1, Z→2. Example: ESX1234567L.
CIF — Código de Identificación Fiscal
For legal entities (corporations, foundations, non-profits). Format: letter type code + 7 digits + control character (digit or letter depending on entity type). The control character is derived from a weighted checksum on the 7-digit body. Example: ESB12345674.
ES^ES[A-Z0-9]\d{7}[A-Z0-9]$NIF and NIE (MOD23): Take the 8 numeric digits (substituting X/Y/Z for NIE), compute modulo 23, and look up the result in the letter table TRWAGMYFPDXBNJZSQVHLCKE. The 23-letter table was deliberately chosen because 10 (maximum digit) mod 23 cycles through all letters without repetition.
CIF (weighted checksum): Apply alternating weights to the 7-digit body (positions 1, 3, 5, 7 are doubled using the digital root method; positions 2, 4, 6 are added directly). Sum all results, take (10 − (sum mod 10)) mod 10 for the numeric check. Convert to a letter from JABCDEFGHI for the letter check. Entity type codes A, B, E, H require a digit control; K, P, Q, S require a letter; all others accept either.
- A — Sociedad Anónima (SA, public limited company)
- B — Sociedad de Responsabilidad Limitada (SRL/SL, limited company)
- C — Sociedad Colectiva
- D — Sociedad Comanditaria
- E — Comunidad de Bienes / Herencias yacentes
- F — Sociedad Cooperativa
- G — Asociación / Fundación
- H — Comunidad de Propietarios
- J — Sociedad Civil
- N — Entidad Extranjera
- P — Entidad Local (municipality)
- Q — Organismo Público
- R — Congregación Religiosa
- S — Órgano de la Administración del Estado
- U — Unión Temporal de Empresas (UTE)
- V — Others not classified
- W — Permanent establishment of a foreign entity
Spanish invoices (facturas) are governed by Real Decreto 1619/2012. The NIF/CIF must appear on both the supplier's and buyer's sections when both parties are VAT-registered. For intra-EU transactions the ES prefix is included.
- Assuming all Spanish numbers are CIFs: Freelancers and sole traders use the NIF format. The first character tells you which type you have — a digit means NIF, X/Y/Z means NIE, a letter (not X/Y/Z) means CIF.
- Wrong control character for the entity type: CIFs for type A entities must end in a digit, not a letter. Using the wrong form fails validation.
- Omitting or misspelling the entity letter: The CIF type letter (B, A, S etc.) is significant and part of the checksum. A typo here changes the expected control character entirely.