Skip to main content
From nothing to a signed, accepted sandbox invoice and a verified webhook. Everything here runs against the sandbox: Cufea’s simulated DIAN. It needs no certificate, no RUT and no DIAN habilitación, and nothing reaches a real DIAN endpoint. You will:
  1. get a sandbox API key from the developer console;
  2. register a webhook endpoint;
  3. create and send an invoice;
  4. follow its status, by polling or by webhook;
  5. verify the webhook signature.
Every endpoint is in the API reference, generated from the same OpenAPI document the API serves at https://api.cufea.com/openapi.json.

Before you start

You need nothing installed but curl. The commands below use these two variables:

1. Get a sandbox key

Open the developer console at app.cufea.com/signup and sign up with an email and a password. You get a sandbox tenant, and the console asks you to verify your email before anything else: open the link it emails you, then log in again. Once verified, choose Create your sandbox API key on the dashboard to get a first cufea_sandbox_ key. The key is shown once: copy it into CUFEA_KEY now. More keys can be issued, and revoked, on the console’s Keys page. Check it. /v1/tenant answers for any valid key:
A missing or wrong key answers 401; every error is application/problem+json:
The sandbox is configured for you (a generated certificate and a numbering range with series SBX); GET /v1/tenant/configuration shows "ready_to_invoice":true.

2. Register a webhook endpoint

Webhooks tell you when the DIAN answered, so you do not have to poll. The endpoint must be an https URL on a public host name: localhost and private addresses are refused. To receive on your machine, expose it through an HTTPS tunnel of your choice.
An empty event_types subscribes to every type: invoice.accepted, invoice.rejected, note.accepted, note.rejected and event.registered. secret is shown once; keep it where your receiver can read it, for step 5. The console’s Webhooks page does the same and lists the latest deliveries.

3. Create and send an invoice

One request creates the invoice, signs it, stores it and, with "send": true, queues it for the DIAN. This one is a cash sale of one service with 19% IVA to a company customer:
The answer is 201 Created with a Location: /v1/invoices/SBX1 header:
  • document_id is the series and the number: SBX1 for a new tenant’s first invoice.
  • totals, taxes, withholdings, lines and qr are what the signed XML says, read from it: print your receipt from them. qr.content is the value to encode in the printed QR.
  • payments lists one entry per means of payment, in order: a sale paid with cash and a card sends two (see Payments).
  • xml_base64 is the signed UBL XML, yours to keep.
  • send: queued means a worker will send it; the state stays prepared until then.
  • Idempotency-Key makes retries safe. If the answer is lost, repeat the exact request: you get the stored answer (with Idempotent-Replayed: true) and no second number is taken. Reusing a key with a different body answers 422 (idempotency_key_reused). GET /v1/documents?kind=invoice&reference=ORDER-1001 also tells you whether an invoice with your reference exists.

Line taxes: taxed, exempt and excluded

Each line lists its taxes. A retail catalog usually needs these five: An exempt item is IVA at 0%: the XML informs IVA with 0.00, and its value counts in the taxable base (TaxExclusiveAmount). An excluded item takes no scheme and no rate: the XML informs no IVA for that line (Anexo 1.9, FAX01 and FAX05) and its value stays out of the taxable base (FAU04). An excluded entry may sit next to another tax such as INC, but not next to an IVA entry, and it never carries scheme or percent; those requests answer 422. A line with an empty taxes list produces the same XML as an excluded one.

Customers without full fiscal data

Only the identification and the name of an identified buyer are required. Without fiscal_responsibilities the buyer gets ["R-99-PN"], without tax_scheme it gets ZZ, and without address the XML leaves out the buyer’s PhysicalLocation and RegistrationAddress (both optional in the Anexo 1.9, FAK07 and FAK28). Send the address when the sale happens outside your premises: FAK07 requires it then.
For an anonymous buyer send "kind": "final_consumer" and nothing else (an email and an address are optional). Cufea fills the consumidor final party the Anexo prescribes: natural person, id type 13, id 222222222222, name consumidor final, R-99-PN and ZZ.

Line discounts

A line takes an optional discount, as a percentage (discount_rate) or an amount (discount_amount): decimal strings with at most two decimals, one or the other, never both (422 validation_error). The amount cannot exceed quantity x unit_price and the rate cannot exceed 100 (422 invoice_invalid). The line value is quantity x unit_price minus the discount, and its taxes apply to that discounted value. The XML carries the discount as a line AllowanceCharge (Anexo 1.9, FBE01-FBE09); AllowanceTotalAmount stays 0.00, since it only sums document-level discounts. Note lines accept the same fields.
The laptop line is worth 180000.00 with 34200.00 of IVA; the mouse line is worth 45000.00 with 8550.00 of IVA. Without "send": true, send it later with POST /v1/invoices/SBX1/send, which answers 202. A body that is not valid answers 422 with the offending fields and never echoes your values:

4. Follow the status

Read the invoice back until state is final (accepted or rejected). The sandbox usually answers within a few seconds:
dian_status_code 00 is the DIAN’s “processed correctly”, and qr.url now links the document in the DIAN catalog. delivery is the email of the invoice to its buyer. In production Cufea sends it after the DIAN accepts the invoice; the sandbox never emails anyone, so here it is skipped. Download the PDF with GET /v1/invoices/SBX1/pdf, or the email’s subject and ZIP with GET /v1/invoices/SBX1/email-content. A rejection lists the DIAN rules in dian_errors. Or let the webhook tell you: when the state becomes final your endpoint receives
The payload carries ids and DIAN codes only, never customer data: read the invoice back for the rest. Answer any 2xx within 10 seconds. No answer, 408, 429 or 5xx is retried after 1 min, 5 min, 30 min, 2 h and 6 h; any other status fails the delivery at once. A delivery can arrive more than once, so deduplicate on the body’s id.

5. Verify the webhook signature

X-Cufea-Signature is t=<unix seconds>,v1=<hex>. t is when this attempt was sent (a retry is signed again with a fresh t), and v1 is the hex HMAC-SHA256 of t, a . and the raw request body, keyed with the endpoint’s secret. Check it before trusting the body:
  • compute it over the bytes exactly as received, before any JSON parsing (re-serializing the JSON changes the bytes, and the signature no longer matches);
  • compare in constant time;
  • reject a t more than 300 seconds away from your clock, in either direction: a captured request replayed later then fails;
  • accept the request when any v1= entry matches (several appear while a secret is rotated), and ignore keys you do not know.
Python (standard library only):
Node.js (no dependencies):
Read the raw body in your framework: await request.body() in FastAPI or Starlette, request.get_data() in Flask, express.raw({ type: "application/json" }) in Express. Answer 401 when verify is false.

Next