Skip to Content
SelloDeveloper docs
Guides

Async issuance

The ind-estado lifecycle, polling, and the SDK waiter.

Issuance is synchronous through signing, asynchronous through Hacienda. POST /v1/documents validates, builds the v4.4 XML, signs, and persists the document on the request path, returning 201 Created (a 200 OK on an idempotent replay) with the document already signed. Sello never calls Hacienda on the request path — only the Hacienda submission is enqueued, and it runs out of band on a transactional outbox where Hacienda itself validates asynchronously. Delivery is at-least-once + reconcile (not exactly-once).

Two state fields

There are two distinct notions of "state", and it's important to keep them apart:

  • state — Sello's engine state machine: created → signed → submitted → accepted | rejected. error is the recoverable bucket; dead_letter is the parked terminal after bounded retries.
  • ind-estado — Hacienda's verbatim verdict (a hyphenated field mirroring Hacienda's wire format): procesando while in flight, then one of the terminal values aceptado, rechazado, or error.

When you wait for an outcome, you're waiting for ind-estado to become terminal.

Polling

Poll GET /v1/documents/{clave} until ind-estado is terminal:

{
  "clave": "50601...",
  "fecha": "2026-06-15T10:00:00Z",
  "ind-estado": "procesando"
}

Once terminal, the response also carries respuesta-xml — the base64-encoded Hacienda response XML — and, when that response is a MensajeHacienda Sello can read, mensajeHacienda: Hacienda's own verdict code and DetalleMensaje, so you do not have to decode the XML to learn why a document was rejected.

{
  "clave": "50601...",
  "fecha": "2026-06-15T10:00:00Z",
  "ind-estado": "rechazado",
  "respuesta-xml": "PD94bWwg...",
  "mensajeHacienda": {
    "mensaje": "3",
    "detalleMensaje": "El consecutivo ya se encuentra registrado"
  }
}

mensaje is "1" (aceptado) or "3" (rechazado), and either value may be null. mensajeHacienda is absent when the response is missing or not recognised; then ind-estado: rechazado is the only signal.

Be polite: back off between polls, and honor a Retry-After header if the server sends one.

The SDK waiter

Rather than hand-rolling a poll loop, use the built-in waiter. It polls getStatus with backoff (honoring Retry-After) until ind-estado reaches a terminal state:

import { isAcceptedIndEstado, SelloWaitTimeoutError } from '@magiios/sdk';

try {
  // create → poll-until-terminal in one call:
  const status = await sello.documents.createAndWait(
    { tipo: 'FE', /* … */ },
    // The Idempotency-Key is required: derive it from the order, not the content.
    { idempotencyKey: `order-${order.id}:FE`, intervalMs: 2000, timeoutMs: 120_000 },
  );

  // …or wait on an already-issued clave:
  // const status = await sello.documents.waitForStatus(clave, { timeoutMs: 60_000 });

  // The generated model exposes the hyphenated wire field ind-estado as indEstado.
  if (isAcceptedIndEstado(status.indEstado)) {
    console.log('Aceptado por Hacienda');
  } else {
    console.warn('Terminal but not accepted:', status.indEstado); // rechazado | error
  }
} catch (err) {
  if (err instanceof SelloWaitTimeoutError) {
    // Still in flight past the budget — reconcile later (err.lastStatus, err.attempts).
  }
}

Key semantics:

  • Both acceptance and rejection resolve — inspect ind-estado to branch. procesando keeps polling.
  • Only a timeout or abort rejects (SelloWaitTimeoutError, which carries lastStatus and attempts so you can reconcile later).
  • The waiter accepts an injectable sleep/now (for tests) and an abort signal.

Prefer webhooks — they cover every outcome a poller sees

Polling is simple but wasteful at volume. For production, register a webhook endpoint and let Sello push each outcome to you: document.accepted and document.rejected for a verdict, and document.failed when a document is parked in error or dead_letter without one — see Webhooks.

Keep a low-rate sweep of your non-terminal documents as a backstop. Delivery is at-least-once, not guaranteed: an endpoint can be disabled, or a delivery can exhaust its retries and be dead-lettered.

One difference to keep in mind: a document.failed with state: "error" is recoverable — Sello may still move that document to a verdict, after an operator redrive or on its own — even though ind-estado: error ends the SDK waiter above. Do not treat the end of the wait as the end of the document.

On this page