npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

cpf-cnpj-utils

v0.1.0

Published

Utility library to validate, generate and look up Brazilian CPF and CNPJ values.

Readme

cpf-cnpj-utils

Utility library to validate, format, parse, generate and look up Brazilian CPF and CNPJ values in JavaScript and TypeScript.

Installation

npm install cpf-cnpj-utils

Usage

import {
  formatCPF,
  fetchCNPJ,
  generateCPF,
  generateCNPJ,
  parseDocument,
  validateCPF,
  validateCNPJ,
} from "cpf-cnpj-utils";

validateCPF("529.982.247-25"); // true
validateCNPJ("04.252.011/0001-10"); // true

formatCPF("52998224725"); // "529.982.247-25"
generateCPF(); // "12345678909"
generateCNPJ(); // "12ABC34501DE35" or "04252011000110"

parseDocument("04.252.011/0001-10");
await fetchCNPJ("04.252.011/0001-10");

CommonJS is also supported:

const { validateCPF } = require("cpf-cnpj-utils");

validateCPF("529.982.247-25");

Test Data Generation

Generated values are mathematically valid and intended for tests, fixtures and development environments only. They do not represent issued documents.

By default, generateCNPJ() can return either a numeric or alphanumeric CNPJ. You can control the output:

import { generateCPF, generateCNPJ, generateDocument } from "cpf-cnpj-utils";

generateCPF();
generateCPF({ formatted: true });

generateCNPJ({ alphanumeric: false });
generateCNPJ({ alphanumeric: true });
generateCNPJ({ alphanumeric: true, formatted: true });

generateDocument("CPF");
generateDocument("CNPJ", { alphanumeric: true });

Alphanumeric CNPJ values use A-Z and 0-9 in the first 12 positions, while the last 2 positions remain numeric check digits.

CNPJ Lookup

fetchCNPJ() fetches company data from the public Minha Receita API.

import { fetchCNPJ, lookupCNPJ } from "cpf-cnpj-utils";

const company = await fetchCNPJ("04.252.011/0001-10");

company.cnpj;
company.razao_social;
company.descricao_situacao_cadastral;
company.qsa;
company.cnaes_secundarios;

// Alias:
await lookupCNPJ("04.252.011/0001-10");

The default API base URL is https://minhareceita.org. You can point the client to a self-hosted Minha Receita instance or pass a custom fetch implementation:

await fetchCNPJ("04.252.011/0001-10", {
  baseUrl: "http://localhost:8000",
  timeoutMs: 5000,
});

The public Minha Receita API does not provide an availability SLA. For production workloads or high-volume usage, consider running your own Minha Receita server.

Utilities

import {
  detectDocumentType,
  formatCNPJ,
  formatCPF,
  normalizeDocument,
  parseDocument,
  stripDocument,
  validateDocument,
} from "cpf-cnpj-utils";

stripDocument("04.252.011/0001-10"); // "04252011000110"
normalizeDocument("\u00e1-b.c/123"); // "ABC123"

formatCPF("52998224725"); // "529.982.247-25"
formatCNPJ("04252011000110"); // "04.252.011/0001-10"

detectDocumentType("529.982.247-25"); // "CPF"
validateDocument("04.252.011/0001-10"); // true

parseDocument("12.ABC.345/01DE-35");
// {
//   type: "CNPJ",
//   normalized: "12ABC34501DE35",
//   formatted: "12.ABC.345/01DE-35",
//   valid: true,
//   alphanumeric: true
// }

Validator Classes

import { CPFValidator, CNPJValidator } from "cpf-cnpj-utils";

CPFValidator.validate("52998224725");
CNPJValidator.validate("04252011000110");

API

  • validateCPF(cpf: string): boolean
  • validateCNPJ(cnpj: string): boolean
  • validateDocument(data: string): boolean
  • isValidCPF(cpf: string): boolean
  • isValidCNPJ(cnpj: string): boolean
  • generateCPF(options?: { formatted?: boolean }): string
  • generateCNPJ(options?: { formatted?: boolean; alphanumeric?: boolean }): string
  • generateDocument(type: "CPF" | "CNPJ" | "cpf" | "cnpj", options?: object): string
  • fetchCNPJ(cnpj: string, options?: FetchCNPJOptions): Promise<MinhaReceitaCNPJData>
  • lookupCNPJ(cnpj: string, options?: FetchCNPJOptions): Promise<MinhaReceitaCNPJData>
  • formatCPF(cpf: string): string
  • formatCNPJ(cnpj: string): string
  • stripDocument(data: string): string
  • normalizeDocument(data: string): string
  • detectDocumentType(data: string): "CPF" | "CNPJ" | null
  • parseDocument(data: string): ParsedDocument
  • CPFValidator.validate(cpf: string): boolean
  • CNPJValidator.validate(cnpj: string): boolean

Development

npm install
npm test
npm run pack:check

License

ISC