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.
Availability
Section titled “Availability”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 |
Emit a document
Section titled “Emit a document”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 }429is treated as backpressure — theRetry-Afterheader is honored.- GETs retry automatically; POSTs retry only when you pass an
idempotencyKey. - Errors are
EmitecrApiErrorwith a machine-readablecode. If the ATV submit fails after the number was allocated, the error carries the allocatedinvoicesummary (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` laterasync and waitForAcceptance are mutually exclusive.
Read, download, reconcile
Section titled “Read, download, reconcile”await emitecr.invoices.get(result.invoiceId); // InvoiceDetailawait emitecr.invoices.list({ externalReference: ticket.id }); // InvoiceListPage — post-disaster reconciliationawait emitecr.invoices.pdf(result.invoiceId); // ArrayBufferawait 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.
Catalogs before there is a key
Section titled “Catalogs before there is a key”/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 → distritoawait catalogs.ivaTariffs(); // labeled IVA optionsBoth catalogs are also on an authenticated client (emitecr.catalogs.*). They
change only when Hacienda republishes a codification — cache them.
Exact invoice math
Section titled “Exact invoice math”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, …After a rejection
Section titled “After a rejection”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` });Control plane & webhooks
Section titled “Control plane & webhooks”See Provisioning tenants for @emitecr/sdk/partner and
Webhooks for verifyWebhook.