Skip to main content

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

invoiceData
InvoiceInput
required
The invoice data object containing all information needed to generate the invoice.

Return Value

invoice
Invoice
The complete invoice object ready for XML generation.
accessKey
string
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

Build docs developers (and LLMs) love