Guides · Spanish company data
What the nine characters of a Spanish company's tax number mean, how the last one is computed, the mistakes that make a good number fail, and how to check one officially
Every Spanish company has a tax identification number, the NIF (número de identificación fiscal). Many people, and many forms, still call it the CIF (código de identificación fiscal), the name of the code that companies had before 2008. Today CIF is simply common shorthand for a company's NIF, which is nine characters long. A company NIF looks like this:
B 1234567 4
│ │ └─ control character (here a digit)
│ └────── seven digits
└─────────── letter for the type of entity (B = limited liability company)
That example is built from made-up digits to show the format. We do not know whether it has been issued to anyone, and it is not meant to identify a company.
This guide explains what each part means, how the control character is computed, how a company's NIF differs from a person's, the mistakes that make a correct number fail a check, and how to confirm a NIF with the authorities.
What the nine characters mean
The structure is set by the general regulation on tax management, Royal Decree 1065/2007 (BOE-A-2007-15984, article 22), and developed by Order EHA/451/2008 (BOE-A-2008-3580), in force since 1 July 2008. Under article 2 of the order, the NIF of a legal entity, or of an entity without legal personality, has nine characters:
- A letter that gives the legal form of a Spanish entity, or says that the entity is foreign or is the permanent establishment of a non-resident entity.
- A random number of seven digits.
- A control character.
Article 3 of the order, in the wording given by Order HAP/5/2016 (BOE-A-2016-358), lists the letters for Spanish entities:
- A: public limited companies (sociedades anónimas, S.A.)
- B: limited liability companies (sociedades de responsabilidad limitada, S.L.)
- C: general partnerships (sociedades colectivas)
- D: limited partnerships (sociedades comanditarias)
- E: joint ownership arrangements (comunidades de bienes), estates in abeyance (herencias yacentes) and other entities without legal personality not covered by another letter
- F: cooperatives
- G: associations
- H: owners' associations of buildings under the horizontal property regime (comunidades de propietarios)
- J: civil-law partnerships (sociedades civiles)
- P: local authorities
- Q: public bodies
- R: religious congregations and institutions
- S: bodies of the State administration and of the autonomous regions
- U: temporary business associations (uniones temporales de empresas)
- V: other types not covered by any other letter
Two further letters are N for foreign entities (article 4) and W for the permanent establishment in Spain of a non-resident entity (article 5).
The NIF stays the same whatever changes the entity goes through. The exception is a change of legal form or nationality (article 1 of the order), so a NIF on an old document may not be the one the entity uses today.
What changed in 2008
The earlier structure was set by Decree 2423/1975 (BOE-A-1975-21698), which Royal Decree 1065/2007 repealed with effect from 1 January 2008. The code also had nine characters. For Spanish entities, the two digits after the first character showed the province of the registered office when the entity was founded, followed by a five-digit sequence number within that province. The preamble of Order EHA/451/2008 says that this province information disappears. A NIF from before 2008 that has not changed keeps the digits it was given under the old system, but the seven digits of a new NIF are random. Neither tells you where the company is today.
The 2008 order also changed the meaning of some letters, as its preamble explains. No letter was withdrawn:
- G used to cover "associations and other types not defined". It was split into G (associations), J (civil partnerships), U (temporary business associations) and V (other types).
- Q used to include religious congregations and institutions, which moved to the new letter R.
- N used to mean non-resident entities. It now means foreign entities, and the new letter W covers permanent establishments.
Under the order's transitional provision, the tax agency was to notify a new NIF during 2008 to each entity that needed a different letter, and entities could also ask for one from 1 July 2008. Order HAP/5/2016 later widened E to cover other entities without legal personality and confirmed J for all civil partnerships.
The NIF of a person
A person's NIF follows different rules, set by articles 19 and 20 of Royal Decree 1065/2007:
- Spanish nationals: the number of the national identity card (DNI), eight digits, followed by a check letter.
- Foreign nationals: the foreigner's identity number (NIE), which starts with X, Y or Z, followed by seven digits and a check letter.
- K, L and M: numbers the tax agency assigns to people who have no DNI or NIE. K is for Spanish nationals under 14 resident in Spain, L for Spanish nationals resident abroad, and M for foreign nationals without an NIE. Each has an initial letter, seven alphanumeric characters and a check letter.
So a NIF that starts with a digit, or with X, Y or Z, belongs to a person, not to a company. A sole trader (autónomo) does business under their personal NIF.
How the control character is computed
Neither Royal Decree 1065/2007 nor Order EHA/451/2008 gives the arithmetic for the control character of an entity's NIF, or says which letters take a digit and which take a letter. The rule below is the one the validator in this article and our API apply, and it reproduces the NIFs that public bodies publish for themselves. We checked it against two of them: the tax agency's own NIF, Q2826000H, given on its transparency page, and that of the Instituto de Crédito Oficial, Q2876002C, given in BOE-A-2025-5467.
The control value depends only on the seven digits. The leading letter does not enter the sum, but it decides whether the NIF ends in a digit, a letter or either.
- Take the seven digits and number them from 1 to 7, left to right.
- Double each digit in an odd position (1st, 3rd, 5th, 7th). If the result has two digits, add them together (16 counts as 1 + 6 = 7). Add up the four results.
- Add the digits in even positions (2nd, 4th, 6th) to that sum.
- Take the last digit of the total. The control digit is 10 minus that digit, or 0 if the last digit is already 0.
- If the entity takes a letter instead of a digit, the control digit picks it from
JABCDEFGHI: 0 is J, 1 is A, 2 is B, and so on up to 9, which is I.
Worked example with the made-up digits 1234567:
digits: 1 2 3 4 5 6 7
odd positions: 1×2=2 3×2=6 5×2=10→1+0=1 7×2=14→1+4=5 sum 14
even positions: 2 + 4 + 6 sum 12
total 26, last digit 6, control digit 10 − 6 = 4, letter D
The control digit is 4 and the matching letter is D. Which of the two the NIF ends in depends on its first letter:
- A, B, E, H: always a digit. With B, the NIF is
B12345674. - N, P, Q, R, S, W: always a letter.
- C, D, F, G, J, U, V: this validator, like our API, accepts either.
The tax agency's NIF works the same way. For 2826000 the odd positions
give 4 + 4 + 0 + 0 = 8 and the even positions 8 + 6 + 0 = 14. The total is
22, the control digit is 8, and Q takes a letter: 8 picks H, which gives
Q2826000H.
For a person
The Ministry of the Interior
publishes how the check letter of a DNI is computed:
divide the eight-digit number by 23 and look up the remainder in the string
TRWAGMYFPDXBNJZSQVHLCKE, where remainder 0 is T and remainder 22 is E. The
ministry's own example is 12345678: the remainder is 14, so the letter is Z,
and the NIF is 12345678Z. For an NIE, replace X with 0, Y with 1 and Z with
2, and apply the same rule to the resulting eight digits.
The K, L and M numbers also end in a check letter, but we have not found the rule for it published, so the validator below only checks that they end in a letter and does not judge which one.
A validator in Python
This function returns True for a NIF whose control character checks out,
False for one that cannot be valid, and None for a K, L or M number
whose last letter it cannot judge. It
removes spaces, hyphens and dots and a leading ES country code before it
checks anything.
import re
DIGIT_ONLY = "ABEH" # control character must be a digit
LETTER_ONLY = "NPQRSW" # control character must be a letter
EITHER = "CDFGJUV" # a digit or a letter is accepted
CONTROL_LETTERS = "JABCDEFGHI"
DNI_LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE"
def normalize(raw):
s = re.sub(r"[\s.\-]", "", raw).upper()
if len(s) == 11 and s.startswith("ES"): # VAT form: drop the country code
s = s[2:]
return s
def check_nif(raw):
"""True if valid, False if not, None if this check cannot judge it."""
s = normalize(raw)
if not re.fullmatch(r"[A-Z0-9]{9}", s):
return False
first, body, last = s[0], s[1:8], s[8]
if first in DIGIT_ONLY + LETTER_ONLY + EITHER:
if not body.isdigit():
return False
total = 0
for i, ch in enumerate(body):
n = int(ch)
if i % 2 == 0: # 1st, 3rd, 5th and 7th digit
n = n * 2
n = n // 10 + n % 10 # 16 counts as 1 + 6
total += n
digit = (10 - total % 10) % 10
letter = CONTROL_LETTERS[digit]
if first in DIGIT_ONLY:
return last == str(digit)
if first in LETTER_ONLY:
return last == letter
return last in (str(digit), letter)
if first in "KLM": # check-letter calculation not implemented
return None if last.isalpha() else False
if first in "XYZ": # NIE: X, Y, Z count as 0, 1, 2
s = str("XYZ".index(first)) + s[1:]
if not s[:8].isdigit():
return False
return last == DNI_LETTERS[int(s[:8]) % 23]
Some results:
check_nif("Q2826000H") # True (the tax agency)
check_nif("ES B-1234567-4") # True (made-up digits, VAT form)
check_nif("B12345670") # False (the control digit should be 4)
check_nif("12345678Z") # True (the Interior ministry's example)
check_nif("K1234567X") # None (made-up, letter not checked)
A True means the number is well formed. It does not mean that the tax
agency has issued it or that the company exists. The official checks below
answer part of that: whether the tax agency identifies the NIF, and whether
it is registered for intra-EU VAT. Neither says by itself that a company is
still trading.
Common mistakes
- Spaces, hyphens and dots.
B-12345674,B 1234567 4andB.12345674are the same NIF, written for easier reading. Remove the separators before comparing or looking up. Copying from a PDF or a web page can also bring in a non-breaking space that looks like an ordinary one. - The
ESprefix. A Spanish VAT number for trade within the EU (NIF-IVA) is the NIF with Spain's country code in front (article 25.1 of Royal Decree 1065/2007), as inESB12345674(made-up digits). AddingESdoes not make a NIF a VAT number: see VIES below. TheESprefix is excluded from the check-character calculation; our lookup by NIF accepts the NIF with or without it. - Letter O for zero, letter I for one. The seven middle characters of a company NIF are always digits, and the first character is never O or I.
- Reading the province. The first two digits were a province code only for numbers assigned before July 2008, and showed the province where the company was founded, not where it is today. Do not derive a location from them.
- Old letter meanings. A G that once meant "associations and other types" now means associations only. Interpret the letters using the 2016 list above.
- Trusting an old document. A change of legal form or nationality is the exception to the rule that a NIF does not change, so a NIF taken from an old invoice or contract may no longer be the company's current one. Confirm it with the official checks below.
- Discarding a number because the arithmetic fails. A failed control character means the number does not pass the check: it may have been mistyped or copied wrong somewhere, or it may not be a Spanish NIF at all. Ask the user to check it at the source. If the NIF is already in your records, look into it before you discard it: the error may be in your data.
How to check a NIF officially
The control character only proves that a number is well formed. To find out whether it belongs to a real entity, use the official services:
- The tax agency's census check. The Agencia Tributaria offers a check of third-party NIFs. For a legal entity, if it identifies the NIF you give it, it returns the current NIF and the registered name, and says whether the entity has been deregistered or its NIF revoked. It requires Cl@ve Móvil, an electronic certificate or the electronic DNI, and is meant for cleaning up taxpayers' identification data before filing a tax return.
- VIES for the EU VAT number. The European Commission's VIES checks a VAT number such as
ESB12345674. A Spanish company only gets a VAT number for trade within the EU (NIF-IVA) when it registers in the register of intra-EU operators (Registro de Operadores Intracomunitarios, ROI). A company that does not trade with other EU countries can therefore have a valid NIF and still show as not valid in VIES. - The list of revoked NIFs. The tax agency publishes cumulative files of revoked and rehabilitated NIFs that anyone can download. The files cover NIFs revoked after 1 December 2006. Under the tax agency's FAQ, publication of the revocation in the BOE makes the NIF invalid for identification purposes in tax matters until it is rehabilitated, and the public registers make no new entries for the entity unless the NIF is rehabilitated.
We know of no official free search that gives a company's NIF from its name, and the company data that the commercial register must provide free of charge under article 17.5 of the Commercial Code do not include it. Two rules point to where it usually appears. The invoicing regulation (Royal Decree 1619/2012, articles 6.1.d and 7.1.d) lists the issuer's NIF among the details that every invoice, full or simplified, must carry, and the law on information society services (Law 34/2002, article 10.1.e) lists the provider's NIF among the information its website must make available. Invoices and the legal notice on the company's website are the places to look first.
Looking up a company by NIF with BORMEAPI
BORMEAPI turns the company notices published in the BORME since 2009 into structured events grouped by company. The BORME does not consistently print the NIF. On the Basic plan (€19 a month plus VAT) and above, you can look a company up by its NIF:
GET /v1/companies/by-nif/B12345674
The NIF can be sent with or without the ES prefix: ESB12345674 is looked
up as B12345674. If we cannot match a well-formed NIF,
the answer says so: it means we could not link that NIF to a company, not
that the company does not exist. If the NIF is on the tax agency's list of
revoked NIFs, the answer carries that entry, even when we have no company
linked to that NIF. If we hold nothing under a NIF whose
control character fails, the answer says that instead, with the character
the arithmetic expects, so that you
can check the number at its source rather than look for it elsewhere. Our
article on the check character as a data-quality
instrument shows what that arithmetic
found in our own data.