Skip to main content
Open Factura provides two core functions for creating electronic invoices that comply with Ecuador’s SRI requirements.

Overview

The invoice generation process involves two steps:
  1. Generate the invoice object with generateInvoice() - Creates the complete invoice structure and calculates the access key
  2. Convert to XML with generateInvoiceXml() - Transforms the invoice object into XML format required by SRI

Generating an Invoice

1

Import the required functions

import { generateInvoice, generateInvoiceXml } from 'open-factura';
2

Prepare your invoice data

Create an InvoiceInput object with all required tax, invoice, and detail information:
const invoiceData = {
  infoTributaria: {
    ambiente: "1", // 1 = Testing, 2 = Production
    tipoEmision: "1",
    razonSocial: "Mi Empresa S.A.",
    nombreComercial: "Mi Empresa",
    ruc: "1234567890001",
    codDoc: "01", // 01 = Invoice
    estab: "001", // Establishment code
    ptoEmi: "001", // Emission point
    secuencial: "000000001", // Sequential number
    dirMatriz: "Av. Principal 123",
  },
  infoFactura: {
    fechaEmision: "15/03/2026",
    dirEstablecimiento: "Av. Principal 123",
    obligadoContabilidad: "SI",
    tipoIdentificacionComprador: "04",
    razonSocialComprador: "Cliente ABC",
    identificacionComprador: "0987654321001",
    direccionComprador: "Calle Secundaria 456",
    totalSinImpuestos: "100.00",
    totalDescuento: "0.00",
    totalConImpuestos: {
      totalImpuesto: [
        {
          codigo: "2", // IVA
          codigoPorcentaje: "2", // 12%
          baseImponible: "100.00",
          tarifa: "12.00",
          valor: "12.00",
        },
      ],
    },
    propina: "0.00",
    importeTotal: "112.00",
    moneda: "DOLAR",
    pagos: {
      pago: [
        {
          formaPago: "01", // Cash
          total: "112.00",
        },
      ],
    },
  },
  detalles: {
    detalle: [
      {
        codigoPrincipal: "PROD001",
        descripcion: "Product Description",
        cantidad: "1.000000",
        precioUnitario: "100.000000",
        descuento: "0.00",
        precioTotalSinImpuesto: "100.00",
        impuestos: {
          impuesto: [
            {
              codigo: "2",
              codigoPorcentaje: "2",
              tarifa: "12.00",
              baseImponible: "100.00",
              valor: "12.00",
            },
          ],
        },
      },
    ],
  },
};
The claveAcceso (access key) is automatically generated based on the invoice data. You don’t need to provide it manually.
3

Generate the invoice object

Call generateInvoice() to create the invoice structure and access key:
const { invoice, accessKey } = generateInvoice(invoiceData);

console.log('Access Key:', accessKey);
// Access Key: 1503202601123456789000110010010000000011234567891
The function returns:
  • invoice: Complete invoice object with XML structure
  • accessKey: 49-digit unique identifier for the invoice
4

Convert to XML format

Transform the invoice object into XML string required by SRI:
const invoiceXml = generateInvoiceXml(invoice);

console.log(invoiceXml);
// <?xml version="1.0" encoding="UTF-8"?>
// <factura id="comprobante" version="1.0.0">
//   <infoTributaria>
//     ...
//   </infoTributaria>
//   ...
// </factura>
The XML is formatted with pretty printing for readability.

Access Key Generation

The access key is automatically generated using the following components:
ComponentDescriptionSource
DateEmission date (DDMMYYYY)infoFactura.fechaEmision
Document CodeType of document (01 for invoice)infoTributaria.codDoc
RUCTax identification numberinfoTributaria.ruc
EnvironmentTesting (1) or Production (2)infoTributaria.ambiente
EstablishmentEstablishment codeinfoTributaria.estab
Emission PointPoint of emissioninfoTributaria.ptoEmi
SequentialSequential numberinfoTributaria.secuencial
Numeric CodeRandom 8-digit numberAuto-generated
Emission TypeAlways “1”Fixed
Verification DigitModulus 11 checksumCalculated

Complete Example

Here’s a minimal working example:
import { generateInvoice, generateInvoiceXml } from 'open-factura';

const { invoice, accessKey } = generateInvoice({
  infoTributaria: {
    ambiente: "1",
    tipoEmision: "1",
    razonSocial: "My Company",
    nombreComercial: "My Store",
    ruc: "1234567890001",
    codDoc: "01",
    estab: "001",
    ptoEmi: "001",
    secuencial: "000000001",
    dirMatriz: "Main Street 123",
  },
  infoFactura: {
    fechaEmision: "15/03/2026",
    dirEstablecimiento: "Main Street 123",
    obligadoContabilidad: "SI",
    tipoIdentificacionComprador: "04",
    razonSocialComprador: "Customer Name",
    identificacionComprador: "0987654321001",
    direccionComprador: "Customer Address",
    totalSinImpuestos: "100.00",
    totalDescuento: "0.00",
    totalConImpuestos: {
      totalImpuesto: [{
        codigo: "2",
        codigoPorcentaje: "2",
        baseImponible: "100.00",
        tarifa: "12.00",
        valor: "12.00",
      }],
    },
    importeTotal: "112.00",
    moneda: "DOLAR",
    pagos: {
      pago: [{ formaPago: "01", total: "112.00" }],
    },
  },
  detalles: {
    detalle: [{
      codigoPrincipal: "PROD001",
      descripcion: "Product Name",
      cantidad: "1.000000",
      precioUnitario: "100.000000",
      descuento: "0.00",
      precioTotalSinImpuesto: "100.00",
      impuestos: {
        impuesto: [{
          codigo: "2",
          codigoPorcentaje: "2",
          tarifa: "12.00",
          baseImponible: "100.00",
          valor: "12.00",
        }],
      },
    }],
  },
});

const invoiceXml = generateInvoiceXml(invoice);

console.log('Generated access key:', accessKey);
console.log('XML ready for signing:', invoiceXml.substring(0, 100) + '...');

Next Steps

Sign the Document

Learn how to digitally sign the XML with your P12 certificate

Submit to SRI

Submit the signed document to SRI’s reception endpoint

API Reference

For detailed type information and all available fields:

Invoice Types

View complete type definitions for InvoiceInput and Invoice

Build docs developers (and LLMs) love