DEV Community

Daviz
Daviz

Posted on

Validating Peru RUC Numbers in Python

Peru company tax numbers (RUC) are 11 digits. The last digit is a checksum. Here is how to validate one in Python, and how to check it against the official SUNAT registry.

First, the checksum:

def ruc_checksum_ok(ruc: str) -> bool:
    if len(ruc) != 11 or not ruc.isdigit():
        return False
    weights = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2]
    total = sum(int(d) * w for d, w in zip(ruc[:10], weights))
    remainder = total % 11
    check = 11 - remainder
    if check == 10:
        check = 0
    elif check == 11:
        check = 1
    return check == int(ruc[10])
Enter fullscreen mode Exit fullscreen mode

This catches typos, but it does not tell you if the company exists. For that, check against SUNAT data. ValidaRUC is a free API that does this:

from validaruc import ValidaRUC

client = ValidaRUC("your-api-key")
result = client.validate("20100070970")
print(result.nombre)
print(result.estado)
Enter fullscreen mode Exit fullscreen mode

Get a free key at https://rapidapi.com/MohsinIshaq/api/validaruc — 100 requests per month, no card needed.

The API returns the company name, status, address, and more, straight from the SUNAT Padron Reducido.

Top comments (0)