BORME API

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

2026-11-23 · 10 min · guide

By BORMEAPI Engineering

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:

  1. 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.
  2. A random number of seven digits.
  3. 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:

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:

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:

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.

  1. Take the seven digits and number them from 1 to 7, left to right.
  2. 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.
  3. Add the digits in even positions (2nd, 4th, 6th) to that sum.
  4. Take the last digit of the total. The control digit is 10 minus that digit, or 0 if the last digit is already 0.
  5. 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:

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

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:

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.

Get a free API key Read the API reference

build 2026.11.23·7d6a14af