Skip to main content

openUBL - Peruvian SUNAT Electronic Documents Library

Project description

openUBL

PyPI npm

Genera, firma y valida documentos electrónicos UBL 2.1 para SUNAT (Perú) — sin complicaciones.

openUBL es una biblioteca Python con API REST incluida. Genera XML válido para facturas, notas de crédito, notas de débito, comunicaciones de baja, resúmenes diarios, percepciones y retenciones. Firma digitalmente con XMLDSig (RSA-SHA-256 / SHA-256) y valida contra las reglas SUNAT antes de enviar.

¿Por qué openUBL?

  • Sin dependencias externas — todo en Python. No necesitas servicios de terceros para generar XML.
  • API REST lista para usar — levantas FastAPI y generas documentos vía HTTP.
  • SDK TypeScript — tipos generados desde OpenAPI, con autocompletado completo.
  • Firma digital integrada — firma con certificado PEM sin herramientas externas.
  • Validación SUNAT — detecta errores antes del envío, no después.
  • Documentación completa — guías paso a paso en https://darvin2c.github.io/openUBL

Instalación rápida

Python

pip install openubl
from openubl.models import Invoice, Proveedor, Cliente, DocumentoVentaDetalle
from openubl.renderer import render_invoice
from openubl.enricher import ContentEnricher

invoice = Invoice(
    serie="F001",
    numero=1,
    proveedor=Proveedor(ruc="20100066603", razonSocial="Softgreen S.A.C."),
    cliente=Cliente(nombre="Carlos", numeroDocumentoIdentidad="12121212121", tipoDocumentoIdentidad="6"),
    detalles=[DocumentoVentaDetalle(descripcion="Item", cantidad=10, precio=100)],
)

enricher = ContentEnricher()
enricher.enrich(invoice)

xml = render_invoice(invoice)
print(xml)  # XML UBL 2.1 listo para firmar

TypeScript / JavaScript

npm install @openubl/sdk
import { createInvoice, SDK_VERSION, checkApiVersion } from "@openubl/sdk";
import { zInvoice } from "@openubl/sdk/zod.gen";

const invoice = zInvoice.parse({
  serie: "F001",
  numero: 1,
  proveedor: { ruc: "20100066603", razonSocial: "Softgreen S.A.C." },
  cliente: { nombre: "Carlos", numeroDocumentoIdentidad: "12121212121", tipoDocumentoIdentidad: "6" },
  detalles: [{ descripcion: "Item", cantidad: 10, precio: 100 }],
});

const { data, error } = await createInvoice({ body: invoice });
if (error) throw new Error(JSON.stringify(error));
console.log(data.xml); // XML UBL 2.1 generado

Levantar la API REST

uv run uvicorn openubl.main:app --reload

Abre http://localhost:8000/docs para Swagger UI interactivo.

Documentos soportados

Tipo Código SUNAT Endpoint
Factura 01 POST /api/v1/invoice/create
Boleta 03 POST /api/v1/invoice/create
Nota de Crédito 07 POST /api/v1/credit-note/create
Nota de Débito 08 POST /api/v1/debit-note/create
Comunicación de Baja RA POST /api/v1/voided-documents/create
Resumen Diario RC POST /api/v1/summary-documents/create
Percepción 40 POST /api/v1/perception/create
Retención 20 POST /api/v1/retention/create

Validación de versión

Verifica que tu SDK y la API compartan la misma versión:

from openubl.version import check_api_version

result = check_api_version("http://localhost:8000")
assert result["ok"], f"Desfase: SDK {result['sdk_version']} vs API {result['api_version']}"
import { checkApiVersion } from "@openubl/sdk";

const result = await checkApiVersion("http://localhost:8000");
if (!result.ok) throw new Error(`Desfase: SDK ${result.sdkVersion} vs API ${result.apiVersion}`);

Documentación

Contribuir

Consulta AGENTS.md para el contexto técnico completo del proyecto.

Licencia

MIT © openUBL

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openubl-1.0.1.tar.gz (785.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openubl-1.0.1-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file openubl-1.0.1.tar.gz.

File metadata

  • Download URL: openubl-1.0.1.tar.gz
  • Upload date:
  • Size: 785.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openubl-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2cc80f4b795ddf2b5fd00e44e5cfac8fb081dc5c8ac6bcada1e269febe1c67c5
MD5 9bf388aa75aa2c88869b1d8a83620308
BLAKE2b-256 8eeb3d2916b04c896e9f0efdf58cbd5c67343d4e281d005ee694864499b79d65

See more details on using hashes here.

File details

Details for the file openubl-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: openubl-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for openubl-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f8a1673b627cd0938b43ae776fac617e1ba1904adf9fb605e0cc3052fecc6540
MD5 218c4f13ce98393e4dbfa104cd441871
BLAKE2b-256 4b48e018a064995a4e6c7fd52569b1c8e02d15e7fd69a23ab4c6d1c8871de03f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page