Skip to content

Create an invoice

Emission is two calls: persist the document, then submit it to Hacienda’s ATV.

  1. POST /v1/invoices — persist a draft. No clave/consecutivo yet — fiscal numbers are unrecoverable, so they are never burned on a draft.
  2. POST /v1/hacienda/submit/:invoiceId — allocate clave + consecutivo, XAdES-sign, and submit to ATV.

Need the fiscal number before (and independent of) the ATV submit — e.g. to print it on a POS receipt? Sign in between with POST /v1/invoices/:id/sign; see Two-phase emit. The key needs the invoices:write, invoices:sign and invoices:submit scopes. This guide shows the raw payloads.

Minimum payload — Tiquete Electrónico (TE)

Section titled “Minimum payload — Tiquete Electrónico (TE)”

TE (docType: "04") doesn’t require a receiver:

Ventana de terminal
curl -X POST https://api.emite.cr/v1/invoices \
-H "x-api-key: $EMITECR_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{
"docType": "04",
"currency": "CRC",
"saleCondition": "01",
"paymentMethods": [{ "type": "01" }],
"lines": [
{
"lineNumber": 1,
"code": "8399000000000",
"quantity": 1,
"unitOfMeasure": "Sp",
"description": "Consultoría TI - bloque mensual",
"unitPrice": 100000,
"subtotal": 100000,
"taxes": [{ "codigo": "01", "codigoTarifa": "08", "tarifa": 13, "monto": 13000 }],
"total": 113000
}
]
}'

The response is a draft — persisted, clave/consecutivo still null, nothing sent to Hacienda yet. Submit it:

Ventana de terminal
curl -X POST https://api.emite.cr/v1/hacienda/submit/$INVOICE_ID \
-H "x-api-key: $EMITECR_API_KEY"

This builds the XML, XAdES-signs with the tenant’s .p12, gets an ATV OAuth token, and POSTs to ATV /recepcion. Processing is asynchronous — see Status & polling.

FE (docType: "01") requires a receiver — reference an existing party by id, or inline it:

{
"docType": "01",
"receiverId": "9f3b8c1d-…",
"currency": "CRC"
}
{
"docType": "01",
"receiver": {
"identificationType": "02",
"identificationNumber": "3101999001",
"name": "Cliente S.A.",
"location": { "provincia": "1", "canton": "01", "distrito": "01" },
"email": "facturacion@cliente.cr"
}
}

receiver and receiverId are mutually exclusive (RECEIVER_BOTH_PROVIDED). FE without either → RECEIVER_REQUIRED_FOR_FE. When you pass receiverId, the party’s data is snapshotted into the invoice at creation — later edits to the party don’t mutate the issued document.

{
"paymentMethods": [
{ "type": "02", "amount": 113000 },
{ "type": "04", "amount": 226000 }
]
}
  • With more than one method, every amount is required (PAYMENT_AMOUNT_REQUIRED) and the sum must equal totalComprobante within ±0.01 (PAYMENT_METHODS_SUM_MISMATCH).
  • A single method may omit amount (defaults to the total).
  • At least one method is required unless saleCondition is a credit code (02, 08 or 10); only those three may omit the field or send an empty array, and then the XML emits no <MedioPago>. Anything else is rejected with PAYMENT_METHOD_REQUIRED — Hacienda itself rejects such a document with error -517, so 03 consignación and 04 apartado are not exempt despite sounding like it.
  • type: "99" (Otros) requires otherDescription (3–100 chars).
{
"unitPrice": 100000,
"subtotal": 90000,
"discounts": [{ "codigoDescuento": "04", "monto": 10000 }],
"taxes": [{ "codigo": "01", "codigoTarifa": "08", "tarifa": 13, "monto": 11700 }],
"total": 101700
}

codigoDescuento: "99" (Otros) requires naturaleza (5–80 chars). The XML declares that text as both NaturalezaDescuento and CodigoDescuentoOTRO, which Hacienda demands for code 99 (-43 without it); the other codes take naturaleza optionally (≤ 80 chars).

Full-line discounts (Regalía / Bonificación)

Section titled “Full-line discounts (Regalía / Bonificación)”

Codes 01, 02, 03 are special — the discount must equal 100% of the line (monto = qty × unitPrice, else LINE_DISCOUNT_FULL_REQUIRED) and can’t combine with any other discount (LINE_DISCOUNT_FULL_EXCLUSIVE). They differ in where the IVA goes:

Code Meaning IVA destination
01 Regalía Free product; emisor absorbs the IVA too <ImpuestoAsumidoEmisorFabrica>
02 Regalía con IVA al cliente Free product, and no IVA either: the IVA is computed on the subtotal, which is 0 (Hacienda refuses an IVA on the full price for this code) None: the IVA is 0
03 Bonificación Like 01, framed as a sales bonus <ImpuestoAsumidoEmisorFabrica>

When the receptor has an authorized exoneration, embed it inside the tax:

{
"taxes": [{
"codigo": "01", "codigoTarifa": "08", "tarifa": 13, "monto": 13000,
"exoneracion": {
"tipoDocumento": "01",
"numeroDocumento": "AL-001-2024",
"nombreInstitucion": "01",
"fechaEmision": "2024-01-15T10:00:00-06:00",
"tarifaExonerada": 13,
"montoExoneracion": 13000
}
}]
}

tipoDocumento: "99" requires tipoDocumentoOtro; nombreInstitucion: "99" requires nombreInstitucionOtros.

Each line can say why it is on the document with transactionType (Anexo nota 22, <TipoTransaccion>). Without it the line is a normal sale, which is how Hacienda reads a line that doesn’t carry one. The other codes declare what is not a sale to a customer: the business consuming its own goods (02 exempt, 03 taxed) or services (04 exempt, 05 taxed), membership fees (06, and 07 exempt), capital goods (08 to 12) and goods withdrawn or given away without consideration (13).

To declare self-consumption, send an electronic invoice (01) with selfConsumption: true and no receiver, receiverId or paymentMethods. For example, the lunch a restaurant serves its manager:

{
"docType": "01",
"currency": "CRC",
"saleCondition": "01",
"selfConsumption": true,
"lines": [
{
"lineNumber": 1,
"code": "6331000000000",
"quantity": 1,
"unitOfMeasure": "Sp",
"description": "Almuerzo del gerente",
"unitPrice": 10000,
"subtotal": 10000,
"taxes": [{ "codigo": "01", "codigoTarifa": "08", "tarifa": 13, "monto": 1300 }],
"total": 11300
}
]
}

The API fills in the rest:

  • The receiver is your own company, taken from its profile. Sending another is refused with SELF_CONSUMPTION_RECEIVER_PROVIDED.
  • Each line without a transactionType gets the self-consumption one by its CAByS and its IVA: 03 or 02 for goods (CAByS 0 to 4), and 05 or 04 for a service (CAByS 5 to 9), taxed when it carries IVA at a rate above zero.
  • Without paymentMethods, the payment method is 99 (other) named «Autoconsumo». Hacienda requires a payment method unless the sale is on credit (-517).
  • A credit or debit note that corrects a self-consumption invoice carries selfConsumption: true too; the one POST /v1/invoices/:id/credit-note builds carries it on its own. Any other document type is refused with SELF_CONSUMPTION_NOT_ALLOWED_FOR_DOCTYPE.

If you send transactionType yourself:

  • A code that says exempt can’t carry IVA at a rate above zero, and one that says taxed must carry it (LINE_TRANSACTION_TYPE_TAX_MISMATCH). Hacienda doesn’t check this; the API does.
  • A ticket (04) and a payment receipt (10) have no such field (LINE_NOT_ALLOWED_FOR_DOCTYPE with transaction_type).
  • A note that corrects a line with a transaction type declares it too, as the Anexo asks; POST /v1/invoices/{id}/credit-note copies it.

Separately, commercialUnitOfMeasure (<UnidadMedidaComercial>, up to 20 characters) names the business’s own unit, such as «Caja de 12», next to unitOfMeasure, which stays required.

  • USD — the server auto-resolves the BCCR rate; pass exchangeRate to override.
  • Other currencies — exchangeRate is required (EXCHANGE_RATE_REQUIRED).

The currency must be in the tenant’s enabledCurrencies, else CURRENCY_NOT_ENABLED. Per-unit excise taxes are currently blocked on non-CRC invoices (EXCISE_NOT_SUPPORTED_FOR_CURRENCY).

Don’t replicate the IVA tariff table client-side — ask the API:

Ventana de terminal
GET /v1/cabys/8399000000000/suggested-iva
{
"cabysCode": "8399000000000",
"rate": 13,
"suggested": { "taxCode": "01", "codigoTarifa": "08", "tarifa": 13, "label": "IVA general 13%" }
}

Copy them into the tax entry: taxCode goes in codigo; codigoTarifa and tarifa keep their names.

  1. DTO validation — types, lengths, ranges, conditional rules. A field the endpoint doesn’t define is rejected (400 VALIDATION_ERROR).
  2. Cross-field validators — full-line discounts, payment sums, receiver-vs-docType.
  3. Service pre-checks — enabled currency, the company’s default branch, terminal and economic activity (412 TENANT_NOT_CONFIGURED when one is missing), idempotency.
  4. Transaction — invoice + lines + taxes + exonerations + discounts + payment methods in one transaction. The consecutivo is allocated later, when the document is signed.
  5. Math reconciliation — the amounts you send are recomputed server-side. On mismatch → 422 INVOICE_TOTALS_MISMATCH with params.lineNumber / field / expected / received.

Step 5 is the most common first-integration failure. Compute every line with exact decimals (in JavaScript, a library such as decimal.js — never binary floats), following the same rules as the server:

  • montoTotal = quantity × unitPrice (no intermediate rounding)
  • subtotal = montoTotal − Σ discounts.monto
  • each IVA monto = base × tarifa / 100, rounded to 5 decimals — where base is the subtotal plus the excise that stacks into it (codes 02, 04, 05, 12), or the montoTotal on a line with a full-line discount 01 or 03
  • total = subtotal + Σ taxes.monto, minus any exonerated amount and any tax the issuer assumes (an exoneración never lowers the tax monto, which stays the gross IVA)
  • tolerance: ±0.005 per value on each line, ±0.01 on the document total

Excise taxes add their own formulas — see Excise taxes.

Code Cause
INSUFFICIENT_SCOPE The API key lacks a scope the call requires — params.required names it
CABYS_INVALID code isn’t exactly 13 digits
RECEIVER_REQUIRED_FOR_FE FE without a receiver
INVOICE_TOTALS_MISMATCH Client amounts diverged from the server recomputation
CURRENCY_NOT_ENABLED Currency not enabled on the company
PAYMENT_METHODS_SUM_MISMATCH amount sum ≠ totalComprobante
TENANT_NOT_CONFIGURED The company lacks a default branch, terminal or economic activity (params.missing) — or, when signing, its Hacienda credentials for that environment
TENANT_LOCATION_MISSING Signing a document while the company has no fiscal address — complete it in the web app

The other codes, by area, are in the error reference.