Sign inGet started
Back to blog
Integration

Node SDK

One `Invoice` class to issue, consult, cancel, reissue and correct invoices, plus clients, catalog and webhooks — typed in TypeScript.

The official Node/TypeScript SDK wraps the REST API in one Invoice class. With it you issue, consult, cancel, reissue and correct invoices, and also manage clients, catalog and webhooks.

How to install

Copy
npm install @stackin-io/stackin-node-sdk

It requires Node 18 or later. The SDK uses the platform's fetch instead of bundling an HTTP client.

Every request honors a configurable timeout: timeoutMs, 30 seconds by default. The fetch implementation itself is injectable. That is how the test suite runs without a network.

What the SDK does

The Invoice class groups these methods:

  • Invoice lifecycle: issue(), consult(), cancel() and reissue().
  • Correction and invalidation: correct() (correction letter, NF-e only), invalidate() and invalidations() (numbering invalidation).
  • Lookups and other operations: pdf(), history(), submissions(), rejection(), stats(), received(), syncReceived(), manifest(), usage(), readiness(), classify() and companies().

Each one returns the API's own response, not a parallel model of it.

Beside Invoice sit:

  • Clients: saved clients.
  • Items: the catalog of products and services, which a line references by itemId.
  • Webhooks and Company.
  • FiscalReference: tax codes and municipalities.
  • Taxpayer: CNPJ and CPF lookup.

The SDK ships its own type declarations. DocumentType.NFE is a real enum member. A misspelled document type is a compile error, not a 422 from the API.

The same goes for br.Product and Address: the fields an NF-e requires are in the type. A missing ncm or an incomplete recipient address is caught by tsc or by local validation, before any request leaves.

How to authenticate

Use your API key as a Bearer token, the same as the raw API. It resolves the issuing company entirely: CNPJ, state, certificate and fiscal environment. Nothing about the issuer goes in the call.

A key with the sdk context is valid on sdk.stackin.io, the SDK's default host. One with the api context is valid on api.stackin.io, the raw API's host. Using one on the other's host returns 401.

An OAuth credential can cover several companies. In that case, each call names the one it acts on with the company option.

How to handle errors

Errors arrive as distinct classes, not as one generic failure:

  • ValidationError: what the SDK refused locally.
  • APIError: carries the API's own statusCode and detail.
  • ConnectionFailedError: the request got no response.

All three extend InvoiceError. A single catch still works when you don't need to tell them apart.

Known refusals are subclasses of APIError, such as PaymentRequiredError (402, no allowance or credit left) and RateLimitedError (429).

How to repeat an issuance safely

The argument of reissue() is not an access key. A failed submission never received one, so it takes the invoice's local id.

In production, a reissue is charged like a new issuance. That is why the SDK has no automatic retry loop around it.

To safely repeat an issuance or reissue whose response was lost, pass idempotencyKey. The same key with the same body returns the first response, with no second document.

See the SDKs section of the API reference for install commands and side-by-side examples, or the code on GitHub.