CPF and CNPJ in JavaScript and TypeScript

Validate, format and generate Brazilian tax IDs with the cpf library. Alphanumeric CNPJ support, built-in types and zero runtime dependencies.

01 / Input

Validate and format

Brazilian tax ID

Validation uses the normalized original input. The mask is a formatting preview.

Processed locally. No document number is sent.

02 / Output

Result

Formatted preview
—

Output of format

Normalized value
—

Enter a document number or load an example.

Validation checks the structure and check digits. It does not verify issuance, ownership or registration status.

In your project

Installation and usage

Available for Node.js and browsers, with ESM and CommonJS exports.

npm install cpf
View the API reference
example.tsJavaScript / TypeScript
import cpf, { cnpj } from 'cpf'

cpf.isValid('529.982.247-25')
// true

cnpj.isValid('12.ABC.345/01DE-35')
// true

cpf.format('5299822')
// '529.982.2'

Documentation

Implementation guides

Read the documentation