Skip to main content

Introduction

The CompraVenta service is the core module for managing electronic invoices in the SIAT system. It provides functionality for:
  • Building and submitting invoices (facturas)
  • Canceling previously issued invoices
  • Generating CUF (Código Único de Factura)
  • Signing, compressing, and hashing invoice XML

Service Methods

The CompraVenta service exposes two primary methods:

RecepcionFactura

Submit a signed and compressed invoice to SIAT for processing

AnulacionFactura

Cancel a previously accepted invoice

Builder Pattern

The SDK uses a fluent builder pattern for constructing invoices and requests. This ensures type safety and prevents direct manipulation of internal structures.

Invoice Builder

Learn how to build invoices using Factura, Cabecera, and Detalle builders

Accessing the Service

The CompraVenta service is available through the main SIAT client:
package main

import (
    "github.com/ron86i/go-siat"
    "log"
)

func main() {
    // Initialize SIAT client
    client, err := siat.New("https://pilotosiatservicios.impuestos.gob.bo/v2", nil)
    if err != nil {
        log.Fatal(err)
    }

    // Access CompraVenta service
    cvService := client.CompraVenta
    
    // Use the service methods
    // cvService.RecepcionFactura(...)
    // cvService.AnulacionFactura(...)
}

Utility Functions

The models.CompraVenta namespace provides utility functions for invoice processing:

GenerarCUF

Generates the unique invoice code (CUF):
cuf, err := models.CompraVenta.GenerarCUF(
    nit,              // int64: NIT del emisor
    fechaHora,        // time.Time: fecha y hora de emisión
    sucursal,         // int: código de sucursal
    modalidad,        // int: código de modalidad
    tipoEmision,      // int: tipo de emisión
    tipoFactura,      // int: tipo de factura
    tipoDocumentoSector, // int: tipo de documento sector
    numeroFactura,    // int: número de factura
    puntoVenta,       // int: código punto de venta
    codigoControl,    // string: código de control
)

SignXML

Signs an invoice XML document with a digital certificate:
signedXML, err := models.CompraVenta.SignXML(
    xmlBytes,    // []byte: XML serializado
    "key.pem",   // string: ruta al archivo de clave privada
    "cert.crt",  // string: ruta al archivo de certificado
)

Complete Workflow

A typical invoice submission follows this workflow:
  1. Build the Invoice using NewFactura(), NewCabecera(), and NewDetalle() builders
  2. Marshal to XML using xml.Marshal()
  3. Sign the XML using models.CompraVenta.SignXML()
  4. Compress with Gzip to reduce payload size
  5. Calculate SHA256 hash of the compressed bytes
  6. Encode to Base64 for transmission
  7. Build RecepcionFacturaRequest with the encoded file and hash
  8. Submit to SIAT using cvService.RecepcionFactura()
See the complete example for implementation details.

Response Structure

All CompraVenta service methods return a SOAP envelope response:
type EnvelopeResponse[T] struct {
    Body struct {
        Content T
    }
}
The response content includes:
RespuestaServicioFacturacion.Transaccion
boolean
Indicates if the transaction was successful
RespuestaServicioFacturacion.CodigoRecepcion
string
Reception code assigned by SIAT (on success)
RespuestaServicioFacturacion.CodigoEstado
int
Status code of the operation
RespuestaServicioFacturacion.CodigoDescripcion
string
Human-readable description of the result
RespuestaServicioFacturacion.MensajesList
[]MensajeServicio
List of messages or errors from the service

Error Handling

Always check both the error return value and the response transaction status:
resp, err := cvService.RecepcionFactura(ctx, cfg, request)
if err != nil {
    log.Fatalf("HTTP/Network error: %v", err)
}

if !resp.Body.Content.RespuestaServicioFacturacion.Transaccion {
    log.Printf("SIAT rejected invoice: %s",
        resp.Body.Content.RespuestaServicioFacturacion.CodigoDescripcion)
    for _, msg := range resp.Body.Content.RespuestaServicioFacturacion.MensajesList {
        log.Printf("  [%d] %s", msg.Codigo, msg.Descripcion)
    }
}

Build docs developers (and LLMs) love