tribu
v0.1.0-experimental.1
Published
SUNAT electronic invoicing for TypeScript: issue invoices, receipts and notes to Peru's tax authority.
Maintainers
Readme
Tribu
Issue Peruvian electronic payment documents (comprobantes de pago electrónicos) to SUNAT from TypeScript.
const { record } = await sunat.invoice({
customer: { ruc: '20123456786', name: 'CLIENTE SAC' },
items: [{ description: 'Teclado', quantity: 2, price: 118 }],
});Tribu calculates the amounts, validates the Document against SUNAT's rules before sending it, builds the UBL 2.1 XML, signs it, sends it, and hands back one plain JSON Record for you to save. You never write XML, never compute IGV, and never type a SUNAT code by hand.
[!WARNING] Experimental. Tribu is published only as an experimental version. SUNAT's beta environment accepts every document type it issues (Milestone 0 results), but it has not been verified in production. Try it in beta first. If you use it in production, read Trying it in production and please tell us how it went.
[!IMPORTANT] Detracciones work only on Invoices in soles, for the goods and services with one confirmed rate (Detracciones). Perceptions, retentions, advance payments, exports, free operations, ISC and ICBPER are out of scope (see What v0.1 covers).
Why Tribu
Without Tribu you compute the IGV, write the UBL XML, sign it, zip it, send it over SOAP, and decode SUNAT's answer yourself. With Tribu that is one call, SUNAT's rules are checked before sending, and every error tells you how to fix it. See the comparison, step by step.
Requirements
- Node.js 22.12 or later. Tribu runs on the server only: it holds your Certificate and SOL password, so
tribu.createrefuses to run in a browser. - ESM only.
Installation
npm install tribu@experimental
# or
pnpm add tribu@experimentalThe experimental versions (0.1.0-experimental.N) are tested against SUNAT beta only. A stable version will follow once Tribu is verified in production.
Quick start
import { readFileSync } from 'node:fs';
import tribu, { Status } from 'tribu';
const sunat = tribu.create({
env: 'beta',
issuer: { ruc: '20000000001', name: 'EMPRESA DE PRUEBA SAC', ubigeo: '150101' },
sol: { user: 'MODDATOS', password: 'MODDATOS' }, // SUNAT's public beta credentials
certificate: { pfx: readFileSync('certificate.pfx'), passphrase },
nextNumber: (series) => db.nextNumber(series), // your database reserves the Number
});
const { record } = await sunat.invoice({
customer: { ruc: '20123456786', name: 'CLIENTE SAC' },
items: [{ description: 'Teclado', quantity: 2, price: 118 }],
});
await db.save(record); // save the Record before doing anything else
record.id; // 'F001-1'
record.status.code; // 'accepted'
record.qr; // the QR text for the Printed RepresentationIn production, sol and env usually come from the environment: SUNAT_ENV, SUNAT_SOL_USER and SUNAT_SOL_PASSWORD. Pass the SOL user without the RUC; Tribu adds it.
tribu.create opens the Certificate at once, so a wrong passphrase or an expired Certificate fails at startup, not on your first sale. A Certificate that expires within 30 days shows up in sunat.warnings.
[!CAUTION] Production is the default. Without
env: 'beta'(orSUNAT_ENV=beta), Tribu talks to SUNAT's production service, and every Document is legally binding. See Configuration.
Documentation
| | |
|---|---|
| Examples | Recipes for everything Tribu does |
| Documents | Amounts and IGV, Invoices, Receipts, notes, voiding |
| Records and errors | What to save, statuses, errors, network failures |
| Testing your code | fakeSunat() |
| Configuration | Environments, options, entry points |
| SUNAT deadlines | The deadlines Tribu checks |
| Trying it in production | What you need and what is not verified yet |
The whole guide: docs/guide. The vocabulary: GLOSSARY.md.
What v0.1 covers
| Area | Included |
|---|---|
| Documents | Invoices, Receipts (individually or in a Daily Summary), Credit and Debit Notes |
| Reporting | Daily Summary, Voided Documents, waitFor, retry |
| Lines | Taxed, exempt or unaffected; units NIU and ZZ |
| IGV | General and the reduced rate for restaurants and hotels |
| Currency | PEN by default, USD |
| Payment Terms | Cash, or credit with Installments |
| Detracciones | On Invoices in soles; the amount and the net amount pending are calculated |
Not yet: detracciones on Receipts or in USD, perceptions, retentions, advance payments, exports and Invoices without RUC, free operations, ISC, ICBPER, the dispatch guide, PDF and QR images, and official support for OSEs (a custom transport works, but OSEs are not tested).
Contributing
Bug reports, production reports and improvements are welcome. Read CONTRIBUTING.md first: it explains how to report a problem without exposing your credentials, and how to propose a change.
