Generator · Spanish data

Spanish NIE

Endpoint GET /v1/es/nie

What it covers

Returns a valid Spanish NIE (foreigner identity number): an `X`/`Y`/`Z` prefix, 7 digits, and a correct control letter. Set `invalid=true` for a NIE with a deliberately wrong control letter.

How it’s calculated

The NIE control letter is the Spanish DNI mod-23 check character: replace the X/Y/Z prefix with 0/1/2, prepend it to the 7-digit body to form an 8-digit number, take number mod 23, and index the 23-letter alphabet TRWAGMYFPDXBNJZSQVHLCKE.

Algorithm

  1. Start from the NIE body: a prefix letter (one of X, Y, Z) and exactly 7 body digits.
  2. Substitute the prefix with a digit: X->0, Y->1, Z->2. Concretely, prefixValue = index of the prefix in the string "XYZ" (X=0, Y=1, Z=2).
  3. Form an 8-digit numeric string by concatenating that prefixValue digit in front of the 7 body digits, then parse it as an integer N. (E.g. prefix Y + body 1234567 -> "1"+"1234567" = "11234567", N = 11234567.)
  4. Compute the remainder R = N mod 23. R is in the range 0..22.
  5. Index the control alphabet ALPHABET = "TRWAGMYFPDXBNJZSQVHLCKE" (23 characters, 0-indexed) at position R. The character at that position is the control letter.
  6. The full NIE is prefix + 7 body digits + control letter (9 characters total).

Worked example

NIE Y1234567?

Prefix Y -> prefixValue = indexOf('Y') in "XYZ" = 1.
Prepend to body 1234567: numeric string = "1" + "1234567" = "11234567", so N = 11234567.
N mod 23: 23 * 488459 = 11234557; 11234567 - 11234557 = 10, so R = 10.
ALPHABET = "TRWAGMYFPDXBNJZSQVHLCKE"; position 10 (0-indexed) = T(0) R(1) W(2) A(3) G(4) M(5) Y(6) F(7) P(8) D(9) X(10) -> 'X'.
Control letter = X.

The control letter is X, giving the full NIE Y1234567X.

Edge cases

  • The prefix is limited to X, Y, Z. Historically X was assigned first, then Y, and now Z; each maps to substitute digit 0, 1, 2 respectively before the mod-23 computation.
  • The generator draws the first body digit in the range 1-9 (no leading zero) for the common shape, though a leading zero is still mathematically valid since the numeric value is what feeds the mod-23 step.

Gotchas

  • The check character is a LETTER, not a digit — it is the mod-23 residue mapped through the fixed non-alphabetical alphabet TRWAGMYFPDXBNJZSQVHLCKE, so you cannot just append the remainder as a number.
  • The modulus is applied to the full 8-digit number formed AFTER substituting the prefix letter with its digit — you must prepend the 0/1/2 substitute, not run mod-23 on the 7 body digits alone.
  • The control alphabet is shared with the DNI; the only NIE-specific step is the X/Y/Z -> 0/1/2 prefix substitution.

Parameters

ParameterTypeDescription
countinteger1–1000Number of records to return, 1–1000 (your plan may lower this). Omitted returns a single record; set it for a batch.
edgebooleanRestrict output to valid edge-case NIEs from the rare corners.
extremebooleanReturn a correct value wrapped in a hostile encoding (untrimmed whitespace, invisible/zero-width characters, BOM, or bidi/combining marks). Homoglyphs are excluded so the value stays machine-parseable; it still validates after normalisation.
invalidbooleanReturn a NIE with a deliberately wrong control letter.
seedintegerInteger seed for reproducible output: the same seed always yields the same record.

Response fields

FieldTypeDescription
valuestringThe full NIE: prefix + 7 digits + control letter.
prefixstringThe prefix letter, one of `X`, `Y`, `Z`.
digitsstringThe 7-digit body, leading zeros preserved.
letterstringThe single control letter from `TRWAGMYFPDXBNJZSQVHLCKE`.

Other generators for Spain

See all generators for Spain →

The same kind of identifier elsewhere

Try it

Generate a live example below — every response is valid by format and entirely synthetic.