Skip to main content

Create Sale

curl -X POST https://api.example.com/sales/ \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "rut_cliente": "12345678-9",
    "tipo_dte": 33,
    "items": [
      {
        "product_id": 1,
        "cantidad": 2
      },
      {
        "product_id": 5,
        "cantidad": 1
      }
    ],
    "payments": [
      {
        "payment_method_id": 1,
        "amount": 50000
      },
      {
        "payment_method_id": 2,
        "amount": 30000,
        "transaction_code": "AUTH123456"
      }
    ],
    "descripcion": "Venta en tienda"
  }'
{
  "id": 123,
  "folio": 1001,
  "tipo_dte": 33,
  "fecha_emision": "2024-03-15T14:30:00Z",
  "monto_neto": 67226.89,
  "iva": 12773.11,
  "monto_total": 80000.00,
  "descripcion": "Venta en tienda",
  "created_at": "2024-03-15T14:30:00Z",
  "related_sale_id": null,
  "referencias": null,
  "customer": {
    "id": 45,
    "rut": "12345678-9",
    "razon_social": "Juan Pérez",
    "giro": "Comercio",
    "direccion": "Av. Principal 123",
    "comuna": "Santiago",
    "ciudad": "Santiago",
    "email": "[email protected]",
    "current_balance": 0,
    "is_active": true,
    "created_at": "2024-01-10T10:00:00Z"
  },
  "details": [
    {
      "product_id": 1,
      "cantidad": 2,
      "precio_unitario": 25000.00,
      "descuento": 0,
      "subtotal": 50000.00,
      "product": {
        "id": 1,
        "codigo_interno": "PROD-001",
        "nombre": "Producto A",
        "precio_neto": 25000.00,
        "stock_actual": 48
      }
    },
    {
      "product_id": 5,
      "cantidad": 1,
      "precio_unitario": 30000.00,
      "descuento": 0,
      "subtotal": 30000.00,
      "product": {
        "id": 5,
        "codigo_interno": "PROD-005",
        "nombre": "Producto B",
        "precio_neto": 30000.00,
        "stock_actual": 19
      }
    }
  ]
}
rut_cliente
string
required
Customer’s RUT (Chilean tax ID). Must be valid format with verification digit (e.g., “12345678-9”).
tipo_dte
integer
default:33
SII document type code:
  • 33: Factura Electrónica (Invoice)
  • 34: Factura Exenta (Exempt Invoice)
  • 39: Boleta Electrónica (Receipt)
  • 61: Nota de Crédito (Credit Note)
items
array
required
Array of sale items. Each item includes:
product_id
integer
required
ID of the product to sell
cantidad
decimal
required
Quantity to sell
payments
array
required
Array of payment methods used. Multiple payments supported (split payment).
payment_method_id
integer
required
ID of the payment method (see Payment Methods)
amount
decimal
required
Amount paid with this method. Total of all payments must equal or exceed sale total.
transaction_code
string
Transaction authorization code (e.g., credit card voucher number)
descripcion
string
Optional description or notes for the sale
referencias
array
Document references (required for adjustment documents: types 56, 61, 111, 112)
tipo_documento
string
required
Referenced document type code (e.g., “33”, “801”, “52”)
folio
string
required
Folio number of the referenced document
fecha
string
required
Date of referenced document (YYYY-MM-DD format)
sii_reason_code
integer
required
SII reason code for the reference:
  • 1: Anula documento (Cancels document)
  • 2: Corrige texto (Corrects text)
  • 3: Corrige monto (Corrects amount)
id
integer
Unique sale ID
folio
integer
Fiscal folio number assigned from CAF (correlative per DTE type)
tipo_dte
integer
SII document type code
fecha_emision
datetime
Document emission timestamp
monto_neto
decimal
Net amount (before tax)
iva
decimal
VAT amount (19% of net amount)
monto_total
decimal
Total amount (net + VAT)
customer
object
Customer information (see Customers)
details
array
Array of sale line items with product details and pricing

Transaction Flow

The sale creation endpoint executes the following atomic transaction:
  1. Cash Session Validation: Verifies the seller has an active cash session open
  2. Customer Validation: Confirms customer exists in the system
  3. Reference Validation: For adjustment documents (56, 61, 111, 112), validates required references
  4. Product Validation: Checks each product exists and is active
  5. Stock Control: For products with stock control enabled:
    • Validates sufficient stock available
    • Decrements stock quantity
    • Creates stock movement record (type: SALIDA, reason: VENTA)
  6. Payment Validation: Ensures total payments equal or exceed sale total
  7. Folio Assignment: Assigns next available folio from CAF (Código de Autorización de Folios)
  8. Internal Credit: If payment method is CREDITO_INTERNO, increases customer’s balance
  9. DTE Generation: Generates XML for electronic tax document
  10. Database Commit: Persists all changes atomically
If any step fails, the entire transaction is rolled back.

Error Responses

{
  "detail": "El vendedor (ID 5) no tiene turno de caja abierto."
}

List Sales

curl -X GET https://api.example.com/sales/?skip=0&limit=50 \
  -H "Authorization: Bearer YOUR_TOKEN"
[
  {
    "id": 123,
    "folio": 1001,
    "tipo_dte": 33,
    "fecha_emision": "2024-03-15T14:30:00Z",
    "monto_neto": 67226.89,
    "iva": 12773.11,
    "monto_total": 80000.00,
    "customer": {
      "id": 45,
      "rut": "12345678-9",
      "razon_social": "Juan Pérez"
    },
    "details": [
      {
        "product_id": 1,
        "cantidad": 2,
        "precio_unitario": 25000.00,
        "subtotal": 50000.00,
        "product": {
          "id": 1,
          "nombre": "Producto A"
        }
      }
    ]
  }
]
skip
integer
default:0
Number of records to skip for pagination (0-based offset)
limit
integer
default:50
Maximum number of records to return (1-200)
Returns sales ordered by creation date descending (most recent first), with customer and product details eagerly loaded.

Get Sale PDF

curl -X GET https://api.example.com/sales/123/pdf \
  -H "Authorization: Bearer YOUR_TOKEN"
Generates an HTML preview of the tax document, ready for printing or PDF conversion. The format (80mm thermal or letter size) is determined by system settings.
sale_id
integer
required
ID of the sale to generate PDF for
Returns HTML content rendered from Jinja2 template with sale, issuer, and customer data.

Error Responses

{
  "detail": "Venta ID 123 no encontrada"
}

Build docs developers (and LLMs) love