Sign inGet started
Back to blog
Integration

Python SDK

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

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

How to install

Copy
pip install stackin-python-sdk

It requires Python 3.10 or later. In code, the package comes in with import stackin.

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(), sync_received(), manifest(), usage(), readiness(), classify() and companies().

Each line item is a typed Product (stackin.br). Brazil-specific tax fields (NCM/CFOP/CEST...) are required only where the authorizer really needs them.

Beside Invoice sit:

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

These are the same methods and the same contract as the Node, Go and PHP SDKs. Once you know one of them, a Python integration reads the same way.

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 environment.

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 argument.

How to handle errors

Errors show up as distinct types, not as one generic failure:

  • APIError: carries the status code and the detail of the response itself. 422 when the authorizer refused the data, 503 to try later, 502 when it could not be reached.
  • ConnectionFailedError: a network, DNS or timeout failure before any response came back.
  • ValueError: a client-side validation problem, such as empty items or a missing ncm/cfop on an NF-e item. It shows up before the request is even sent.

APIError and ConnectionFailedError inherit from InvoiceError, so you can catch both at once.

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 SDK never repeats a call on its own and never generates an idempotency key.

Issuing is the call you must not repeat blindly. If the response was lost, the invoice may have been authorized.

Pass idempotency_key to issue() or reissue(). Repeating with the same key and the same body returns the first response, with no second document and no new credit.

See the SDKs section of the API reference for install commands, or the code on GitHub.