ForHosting KIT · Developer Utilities

Spain NIE control letter

X1234567 gets the control letter L, so the full NIE is X1234567L. Type the prefix and seven digits to complete a foreign resident's number, or paste a whole NIE to check its letter before a contract or payroll file. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Number
X1234567
You get
Calculation mode
Compute
Input
X1234567
Prefix
X
Prefix digit
0

A Spanish NIE (Número de Identidad de Extranjero) is the foreigner identity number used across tax, banking, employment, and residency workflows in Spain. Unlike a pure digit string, every NIE starts with X, Y, or Z, continues with seven digits, and ends with a single control letter that proves the body was typed or stored without a common slip. This capability implements the official mapping and mod-23 rule shared with Spanish DNI check letters: substitute 0 for X, 1 for Y, or 2 for Z, treat the result as an eight-digit integer, take that integer modulo 23, and look up the letter in the fixed table TRWAGMYFPDXBNJZSQVHLCKE. Use compute mode when you hold only the prefix and seven digits and need the correct trailing letter for a form or master-data import; use validate mode when a partner, CRM, or user submits a full NIE and you need a clear valid flag plus the expected control letter for error messages.

How the Spanish NIE control letter is calculated

A Spanish NIE always has nine characters when written without spaces or hyphens: one leading letter from the set X, Y, Z; seven decimal digits; and one trailing control letter from A through Z (not every letter appears—the table has twenty-three positions). The leading letter is not decorative. Historically X, Y, and Z mark successive ranges of numbers assigned to foreign residents; for the check-letter formula they are rewritten as digits so the same DNI table can be reused.

Map X to zero, Y to one, and Z to two, then concatenate that digit with the seven numeric characters to form an eight-digit string such as 01234567 for the classic body X1234567. Interpret that string as an integer (leading zeros do not change the modular value) and compute the remainder when divided by twenty-three. Index into the official sequence T, R, W, A, G, M, Y, F, P, D, X, B, N, J, Z, S, Q, V, H, L, C, K, E using that remainder as a zero-based index.

The letter at that index is the only valid control letter for that body. Example: X1234567 maps to 01234567, remainder nineteen, control letter L, full NIE X1234567L. The algorithm is deterministic and local: no Spanish government network call, no registry lookup, and no clock.

Where Spain NIE letter checks fit real workflows

HR and payroll teams onboard non-Spanish employees and must attach the correct NIE control letter before writing the identifier onto contracts, social-security filings, or bank payment files that reject invalid tax IDs. Property, banking, and telecom signup forms validate a full nine-character NIE at account creation so a single mistyped letter does not create a phantom customer and a compliance mismatch.

Data-quality jobs walk every NIE column in a CRM or ERP export, call validate mode, and quarantine rows where valid is false—catching both human edits and off-by-one bugs in generators that confuse DNI digit-only bodies with NIE prefix rules. Do not treat a passing NIE letter check as proof of active residency with Spanish authorities or of a specific work permit; it only proves the control letter is consistent with the eight-digit mapped body under the standard mod-23 table.

Audit NIE columns in CRM and ERP exports

Batch-validate every Spanish NIE in a customer or employee file and quarantine rows whose control letter no longer matches the recomputed expected value.

What is a Spanish NIE control letter?

It is the last character of a nine-character NIE. After mapping the leading X, Y, or Z to 0, 1, or 2, the eight-digit value modulo 23 indexes the official table TRWAGMYFPDXBNJZSQVHLCKE to produce that letter.

What happens with wrong length, wrong prefix, or punctuation?

A well-formed NIE with a wrong letter returns valid false in validate mode. Failed validation tasks are not billed.

Is the Spain NIE control letter tool free?

The browser tool is free and runs locally.

How is the NIE control letter calculated?

Replace X with 0, Y with 1 or Z with 2, divide the eight-digit number by 23 and use the remainder to pick the letter from the table TRWAGMYFPDXBNJZSQVHLCKE. For X1234567 the remainder is 19, so the letter is L.

Is the NIE letter calculated like the DNI letter?

Yes. It uses the same 23-letter table and the same division as the Spanish DNI. The only difference is that the leading X, Y or Z is first turned into 0, 1 or 2.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/enc/nie-spain

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/enc/nie-spain \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"number":"X1234567"}'
{
  "number": "X1234567"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "enc.nie_spain",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Malformed input fails with invalid_input instead of silent stripping, so you never store a half-cleaned foreigner ID. The same pure arithmetic runs free in the browser and at the published API price.

Input contract, modes, and error behaviour

Send a number field with the NIE body. Wrong length is an explicit error that names the mode and the length you sent—eight for compute, nine for validate—so client forms can show a precise message without guessing. Empty strings, missing number, invalid mode values, prefixes other than X Y Z, non-digit middles, and non-letter trailers fail before any modular result is trusted for storage and are not billed. Prefer compute when you complete foreigner identifiers for forms; prefer validate when you gate a lookup or import on a full NIE from a user or partner file. Spaces and hyphens are stripped so common display forms such as X-1234567-L still parse; dots, slashes, and other punctuation are rejected as malformed input with invalid_input so a silently cleaned value never reaches storage. Mode compute is the default: the engine requires exactly eight characters after normalization—prefix X, Y, or Z plus seven digits—and returns control_letter, the full nie string with the letter appended, prefix_digit (0, 1, or 2), mapped_number, and remainder. Mode validate requires exactly nine characters including the trailing control letter; the response reports valid true when that letter matches the recomputed expected_control_letter, and valid false when the body is well formed but the letter is wrong. Validate always echoes the given control_letter, expected_control_letter, prefix, digits, mapped_number, remainder, and the corrected nie built with the expected letter. Alias keys such as nie, text, value, code, id, and n are accepted for the same payload field so integrations that already speak nie do not need a rename. Because the path is pure parse with no clock, random source, or network, the same JSON body always yields the same JSON result on the free browser tool and on the API at $0.002 per successful request.

Where Spain NIE letter checks fit real workflows

Immigration and relocation platforms recompute expected_control_letter and show a friendly correction when an applicant pastes a number with a wrong trailing character.

Complete a NIE body for payroll and contracts

Call compute mode with X/Y/Z plus seven digits, store the returned nie with control_letter, and feed the full identifier into HR, tax, or banking systems.

Validate typed or imported full NIEs at entry

When a form or CSV submits nine characters, call validate mode and block save when valid is false, showing expected_control_letter to help correct a single mistype.

How do I compute the Spain NIE control letter with this API?

POST with number set to prefix plus seven digits (for example X1234567) and mode compute (or omit mode). The response includes control_letter, nie (full nine characters), prefix_digit, mapped_number, and remainder.

How do I validate a full Spanish NIE?

POST with the nine-character NIE and mode validate. The response includes valid, control_letter (given), expected_control_letter, prefix, digits, mapped_number, remainder, and nie built with the expected letter.

Read the full KIT documentation →