Function Signature
function generateInvoice ( invoiceData : InvoiceInput ) : {
invoice : Invoice ;
accessKey : string ;
}
Description
Generates a complete invoice object from the provided invoice data. This function automatically generates an access key (clave de acceso) based on the invoice information and returns both the complete invoice structure and the generated access key.
The access key is generated using the date, document code, RUC, environment, establishment, emission point, and sequential number from the invoice data.
Parameters
The invoice data object containing all information needed to generate the invoice. Show InvoiceInput properties
infoTributaria
Omit<TaxInfo, 'claveAcceso'>
required
Tax information for the invoice. The access key (claveAcceso) is automatically generated. Environment type. 1 for pruebas (testing), 2 for produccion (production).
codDoc
'01' | '03' | '04' | '05' | '06' | '07'
required
Document code. 01 for FACTURA, 03 for LIQUIDACIÓN DE COMPRA, 04 for NOTA DE CRÉDITO, 05 for NOTA DE DÉBITO, 06 for GUÍA DE REMISIÓN, 07 for COMPROBANTE DE RETENCIÓN.
regimenMicroempresas
'CONTRIBUYENTE RÉGIMEN MICROEMPRESAS'
Microenterprise regime status.
contribuyenteRimpe
'CONTRIBUYENTE NEGOCIO POPULAR - RÉGIMEN RIMPE' | 'CONTRIBUYENTE RÉGIMEN RIMPE'
RIMPE regime status.
Invoice information including buyer details, totals, and payment information. Show Invoice Info properties
Emission date in format DD/MM/YYYY.
Whether obligated to keep accounting records.
tipoIdentificacionComprador
'04' | '05' | '06' | '07' | '08'
required
Buyer identification type. 04 for RUC, 05 for CÉDULA, 06 for PASAPORTE, 07 for VENTA A CONSUMIDOR FINAL, 08 for IDENTIFICACIÓN DEL EXTERIOR.
Buyer’s identification number.
Total with taxes breakdown.
Payment information array.
Array of invoice line items. Array of detail objects, each containing:
codigoPrincipal: Main product code
codigoAuxiliar: Auxiliary product code
descripcion: Product description
cantidad: Quantity
precioUnitario: Unit price
descuento: Discount amount
precioTotalSinImpuesto: Total price without tax
impuestos: Tax information
Reimbursement information (optional).
Retention information (optional).
infoSustitutivaGuiaRemision
RemisionGuideSustitutiveInfo
Substitutive remission guide information (optional).
Other third-party values (optional).
Negotiable type information (optional). Fiscal machine information (optional). Additional information (optional).
Return Value
The complete invoice object ready for XML generation. The root invoice object. XML namespace for digital signature. Value: http://www.w3.org/2000/09/xmldsig#
XML namespace for schema instance. Value: http://www.w3.org/2001/XMLSchema-instance
Document ID. Value: comprobante
Document version. Value: 1.0.0
Tax information with the generated access key included.
The automatically generated access key (clave de acceso) for the invoice. This is a 49-digit numerical string generated based on the invoice information.
Example
import { generateInvoice } from 'open-factura' ;
const invoiceData = {
infoTributaria: {
ambiente: '1' ,
tipoEmision: '1' ,
razonSocial: 'Mi Empresa S.A.' ,
nombreComercial: 'Mi Empresa' ,
ruc: '1234567890001' ,
codDoc: '01' ,
estab: '001' ,
ptoEmi: '001' ,
secuencial: '000000001' ,
dirMatriz: 'Av. Principal 123'
},
infoFactura: {
fechaEmision: '15/03/2026' ,
dirEstablecimiento: 'Av. Principal 123' ,
obligadoContabilidad: 'SI' ,
tipoIdentificacionComprador: '05' ,
razonSocialComprador: 'Juan Pérez' ,
identificacionComprador: '0987654321' ,
direccionComprador: 'Calle Secundaria 456' ,
totalSinImpuestos: '100.00' ,
totalDescuento: '0.00' ,
totalConImpuestos: {
totalImpuesto: [
{
codigo: '2' ,
codigoPorcentaje: '2' ,
descuentoAdicional: '0.00' ,
baseImponible: '100.00' ,
tarifa: '12' ,
valor: '12.00'
}
]
},
importeTotal: '112.00' ,
moneda: 'DOLAR' ,
pagos: {
pago: [
{
formaPago: '01' ,
total: '112.00' ,
plazo: '0' ,
unidadTiempo: 'dias'
}
]
}
},
detalles: {
detalle: [
{
codigoPrincipal: 'PROD001' ,
codigoAuxiliar: 'AUX001' ,
descripcion: 'Producto de ejemplo' ,
cantidad: '1.000000' ,
precioUnitario: '100.000000' ,
descuento: '0.00' ,
precioTotalSinImpuesto: '100.00' ,
impuestos: {
impuesto: [
{
codigo: '2' ,
codigoPorcentaje: '2' ,
tarifa: '12' ,
baseImponible: '100.00' ,
valor: '12.00'
}
]
}
}
]
}
};
const { invoice , accessKey } = generateInvoice ( invoiceData );
console . log ( 'Access Key:' , accessKey );
console . log ( 'Invoice:' , invoice );
See Also