Skip to content

TypeScript SDK (coming soon)

@emitecr/sdk is the official TypeScript client. Server-side only (Node ≥20) — API keys and the partner credential must never reach a browser or an on-prem device. The npm boundary is not a security boundary; the server enforces auth, the SDK is a typed convenience.

Not published yet. When it is, it will be installed from npm as @emitecr/sdk, and this page will say so.

Subpath exports:

Import Plane
@emitecr/sdk Data plane (emit/read) + invoiceMath
@emitecr/sdk/partner Control plane
@emitecr/sdk/webhooks Webhook signature verification
import { EmitecrClient, invoiceMath } from '@emitecr/sdk';
const emitecr = new EmitecrClient({ apiKey: process.env.EMITECR_API_KEY! });
// Resumable end-to-end: create → submit. Safe to call again with the same
// externalReference after ANY failure — it resumes at the missing step.
const result = await emitecr.invoices.emit(
{
docType: '04',
currency: 'CRC',
saleCondition: '01',
externalReference: ticket.id, // your uuid — permanent, queryable dedupe
situacion: 1,
lines: buildLines(ticket), // compute montos with `invoiceMath`
paymentMethods: [{ type: '01' }],
},
{ idempotencyKey: ticket.id },
);
// → { invoiceId, clave, consecutivo, status, haciendaStatus, resumed, submitted }
  • 429 is treated as backpressure — the Retry-After header is honored.
  • GETs retry automatically; POSTs retry only when you pass an idempotencyKey.
  • Errors are EmitecrApiError with a machine-readable code. If the ATV submit fails after the number was allocated, the error carries the allocated invoice summary (err.invoice.consecutivo / .clave) — a failing submit never loses the number.

Async: the number now, the submit in background

Section titled “Async: the number now, the submit in background”

For a POS that must print the consecutivo/clave on the voucher immediately (partner-minted keys only — see Two-phase emit):

const result = await emitecr.invoices.emit(payload, {
async: true, // returns at the signed checkpoint — no ATV wait
idempotencyKey: ticket.id,
});
// → { consecutivo, clave, status: 'signed', submitted: false, … }

EmiteCR transmits in the background; the verdict arrives via the invoice.accepted | rejected | error webhooks. Retrying with the same externalReference returns the same number.

Conversely, a webhook-less backend can block until Hacienda’s final verdict:

const result = await emitecr.invoices.emit(payload, {
waitForAcceptance: true, // polls until accepted/rejected (or timeout)
timeoutMs: 60_000,
});
// on timeout: status is still 'submitted' — poll `invoices.get` later

async and waitForAcceptance are mutually exclusive.

await emitecr.invoices.get(result.invoiceId); // InvoiceDetail
await emitecr.invoices.list({ externalReference: ticket.id }); // InvoiceListPage — post-disaster reconciliation
await emitecr.invoices.pdf(result.invoiceId); // ArrayBuffer
await emitecr.invoices.xml(result.invoiceId);
await emitecr.cabys.suggestedIva('8399000000000');

get returns the full document (InvoiceDetail); list returns pages of lightweight rows (InvoiceListItem — the table columns, no lines). Both types are generated from the OpenAPI schema, so they follow the API rather than a copy you maintain. Every method still accepts a type parameter (invoices.get<MyShape>(id)) when you want to narrow it yourself.

/v1/catalogs/* needs no credential, and the screen that needs it most runs before one exists — the form asking a new client for their fiscal address, which happens before their tenant is provisioned:

import { EmitecrCatalogsClient } from '@emitecr/sdk';
const catalogs = new EmitecrCatalogsClient();
await catalogs.locations(); // provincia → cantón → distrito
await catalogs.ivaTariffs(); // labeled IVA options

Both catalogs are also on an authenticated client (emitecr.catalogs.*). They change only when Hacienda republishes a codification — cache them.

The server recomputes your totals and rejects drift with INVOICE_TOTALS_MISMATCH. Compute line montos with the re-exported invoiceMath (from @emitecr/invoice-math, decimal.js-based) so your arithmetic matches the server exactly:

import { invoiceMath } from '@emitecr/sdk';
// invoiceMath.lineSubtotalNet, .taxAmount, .computeTotalComprobante, …

A rejected document is never edited — re-emit a corrected one as a NEW emission with an attempt suffix:

await emitecr.invoices.emit({ ...corrected, externalReference: `${ticket.id}:2` });

See Provisioning tenants for @emitecr/sdk/partner and Webhooks for verifyWebhook.