Download received NF-e XML via API: DF-e distribution, sync and error 656
How to download the XML of received NF-e through the API and automate the flow: list, sync with the SEFAZ, webhooks, the 1-hour wait, lost NSUs and error 656.

To automate received NF-e, the official route is the SEFAZ DF-e Distribution web service (NT 2014.002): it hands your CNPJ, in NSU order, the invoices suppliers issued against it. On Stackin that read already runs for you every six hours and is stored; the API lists what was collected (GET /api/v1/received-invoices), downloads the XML (GET /api/v1/received-invoices/{access_key}/xml), sends a webhook when a new invoice arrives and asks the SEFAZ for what is new right now (POST /api/v1/received-invoices/sync). This post is for teams integrating an ERP or accounting system who need to know what the API does, what the SEFAZ imposes, and where it goes wrong.
Two things up front. First: before any manifestation the SEFAZ delivers only the summary of the invoice (resNFe); the full XML (nfeProc) arrives after a manifestation, and the events are covered in Recipient manifestation of the NF-e. Second: once the full XML is stored, the API returns the file by access key with no new charge; while only the summary exists, the answer is a 409 that points to the manifestation that releases the XML.
How to list received NF-e through the API
GET /api/v1/received-invoices only reads what was already collected, in the environment of the key you use. It never calls the SEFAZ, so you can query it freely. The response is paginated (limit up to 100, offset, has_more) with one row per access key and environment.
curl "https://api.stackin.io/api/v1/received-invoices?limit=50" \
-H "Authorization: Bearer $STACKIN_API_KEY"{
"total": 137,
"page": 1,
"per_page": 50,
"next_page": 2,
"has_more": true,
"data": [
{
"nsu": "000000000004512",
"schema": "resNFe",
"access_key": "35261112345678000195550010000012341000012345",
"issuer_name": "Fornecedor Exemplo Ltda",
"issuer_tax_id": "12345678000195",
"amount": 1840.5,
"issued_at": "2026-11-20T10:12:00-03:00",
"manifestation": null,
"final_answer_due": true,
"has_full_document": false,
"environment": "production",
"events": []
}
]
}The schema field tells you what you hold: resNFe is the summary (issuer, amount, date) and procNFe is the full NF-e. has_full_document is true once the full XML has arrived, and final_answer_due stays true while the invoice has no conclusive answer. Because a row goes from summary to full after the manifestation, use `access_key` as the key in your system and update the existing row instead of inserting a new one. Events registered against the invoice, such as a cancellation or a correction letter, come in events.
How sync works and why it answers 429 DISTRIBUTION_COOLDOWN
The scheduled read covers the production environment and already keeps the list current; with a homologation key, the list only fills through sync. So POST /api/v1/received-invoices/sync is for when you need the invoice now. It resumes from the last stored NSU and returns how many documents came in; next_sync_at is set only when the SEFAZ had nothing more to deliver.
curl -X POST https://api.stackin.io/api/v1/received-invoices/sync \
-H "Authorization: Bearer $STACKIN_API_KEY"{
"stored": 12,
"pages": 1,
"last_nsu": "000000000004512",
"max_nsu": "000000000004512",
"next_sync_at": "2026-11-22T15:04:11"
}The limit comes from the SEFAZ: after a 137 answer (no documents found), or when ultNSU equals maxNSU, the CNPJ must wait one hour before asking again. Asking sooner is rejection 656, and the NT warns that resuming before the hour is up restarts the count. Stackin refuses the request before it reaches the SEFAZ: inside the wait, sync answers 429 with code DISTRIBUTION_COOLDOWN, the time it is released in retry_at (UTC) and the Retry-After header, in seconds.
HTTP/1.1 429 Too Many Requests
Retry-After: 2312
{
"detail": "The SEFAZ allows the next read of received documents only after 2026-11-22T15:04:11Z; it requires one hour after having nothing more to deliver.",
"code": "DISTRIBUTION_COOLDOWN",
"retry_at": "2026-11-22T15:04:11Z"
}Treat that 429 as a time, not an error: store retry_at and try again only after it, with no retry loop. The Python SDK (sync_received(), received()), Node and PHP (syncReceived(), received()) and Go (SyncReceived(), Received()) have the same two methods, and the 429 becomes a RateLimitedError with code, retry_at and retry_after.
import os
from stackin import Invoice, RateLimitedError
client = Invoice(api_key=os.environ["STACKIN_API_KEY"])
try:
result = client.sync_received()
print(result.stored, "new,", "next read at", result.next_sync_at)
except RateLimitedError as error:
if error.code != "DISTRIBUTION_COOLDOWN":
raise
print("SEFAZ wait until", error.retry_at, "- retry in", error.retry_after, "s")
offset = 0
while True:
page = client.received(limit=100, offset=offset)
for row in page["data"]:
print(row["access_key"], row["schema"], row["has_full_document"])
if not page["has_more"]:
break
offset += 100What happens when an NSU is missing: gap recovery
The NSU cursor only moves when the SEFAZ answers 137 or 138. When an NSU never reaches Stackin, the range is recorded as a gap and fetched back one by one with the consNSU query, newest first. NT 2014.002 limits that query to 20 per hour per CNPJ, so Stackin asks for part of it per run and one batch an hour, always under the limit. While the gap at the front of the cursor is open, no sequential read goes out, because it could only come back as 656.
You do not call consNSU through the API and you do not see the gaps in it: the effect shows up as older invoices appearing in received-invoices after a run. And there is an honest limit. The SEFAZ keeps documents for up to 90 days after receipt; past that, the query by NSU answers 632 (request out of time) and the NF-e can no longer be recovered. When that happens, Stackin closes the gap as expired. An NSU lost more than 90 days ago, or one that could not be recovered in time at 20 per hour, is an invoice you will have to ask the supplier for.
Why rejection 656 happens and what to do when another system reads the same CNPJ
656 is excessive use, and NT 2014.002 gives three causes: asking again within the hour after a 137, asking out of the ultNSU sequence, and going past 20 queries per NSU or key in an hour. The second one catches anyone with more than one reader. The NT is explicit: if several applications of the same party query the same CNPJ by NSU, they must all follow the same ascending sequence, or they count as misuse. In practice, the ERP, the accountant's system and Stackin fight over the same queue, each with its own cursor.
When the SEFAZ answers 656 to a sync, it comes back as 503 with error.code 656 and retryable: true. Stackin's cursor does not jump to the SEFAZ's number, so it does not lose the invoices the other system took: the range becomes a gap and is recovered through consNSU while the SEFAZ still holds them. The CNPJ is blocked for an hour, and during it sync answers the 429 above. If failures continue, the dashboard notifies in the bell, at most once a day.
- Pick a single reader of the queue. Turn off automatic invoice download in the other system (ERP, accountant) and read through the Stackin API, or the other way around. Two sequential readers on the same CNPJ block each other again.
- Do not loop. After a 656, wait the whole hour; asking sooner restarts the count.
- Read the list, not the SEFAZ. The ERP should use
GET /received-invoiceswith its key and leave the distribution read to Stackin. - Check coverage. Once fixed, compare the count in the list with the other system's for recent weeks, remembering that the recovery window is 90 days.
How to download the received NF-e XML through the API
GET /api/v1/received-invoices/{access_key}/xml returns the full XML (procNFe) as the SEFAZ distributed it, with 200 and Content-Type: application/xml, in the environment of the key you use. It only reads what was already collected: it never calls the SEFAZ. Downloading is not charged: a received invoice is billed once, when it is stored.
curl "https://api.stackin.io/api/v1/received-invoices/$ACCESS_KEY/xml" \
-H "Authorization: Bearer $STACKIN_API_KEY" \
-o "$ACCESS_KEY.xml"While only the summary (resNFe) has arrived, the answer is 409 with code RECEIVED_XML_NOT_AVAILABLE and the path of the manifestation that releases the XML. An invoice past the monthly allowance with a negative credit balance answers 402 RECEIVED_PAYMENT_REQUIRED until the top-up; a key the company did not receive, 404.
HTTP/1.1 409 Conflict
{
"detail": {
"code": "RECEIVED_XML_NOT_AVAILABLE",
"message": "Only the summary (resNFe) of this NF-e has been received. The SEFAZ distributes the full XML after the recipient manifests ciência (210210) or confirmação (210200), on a later read of the distribution: the scheduled one, or POST /api/v1/received-invoices/sync.",
"manifestation": {
"method": "POST",
"path": "/api/v1/received-invoices/35261112345678000195550010000012341000012345/manifestation"
}
}
}The full XML only exists for your company after a manifestation (Ciência, Confirmação or Operação não Realizada release the nfeProc; Desconhecimento does not). Register it with POST /api/v1/received-invoices/{access_key}/manifestation, which the post Recipient manifestation of the NF-e covers. The SEFAZ delivers the complete document on a later read of the distribution, the scheduled one or a POST /api/v1/received-invoices/sync, and from then on the GET above returns the file. In the Stackin dashboard, under Notas recebidas, an invoice with has_full_document true also has the Baixar XML and Baixar PDF actions (the DANFE drawn from the stored XML).
How to get a webhook for a received NF-e
Two webhook events cover received invoices, each once per access key: received_invoice.stored, when an invoice issued against your CNPJ is stored for the first time (summary or full), and received_invoice.full_document, when the full XML arrives and can be downloaded. Subscribe to them when you register the endpoint with POST /api/v1/webhooks; an endpoint registered with no list of events receives all of them, these two included. The signature is checked like the other events, in /api#webhooks.
{
"event": "received_invoice.stored",
"company_id": "6f1c2a9e-3b7d-4c21-9a8e-2d5f0b6c7e14",
"tax_id": "11222333000181",
"environment": "production",
"data": {
"access_key": "42260831112223330001815500000012341123456789",
"schema": "resNFe",
"has_full_document": false,
"issuer": { "tax_id": "31112223000181", "name": "Fornecedor Exemplo LTDA" },
"amount": 1234.5,
"issued_at": "2026-11-17T13:05:02-03:00",
"operation_status": "1",
"environment": "production",
"payment_required": false
}
}With the webhooks you do not need to poll the list to learn about a new invoice: the notice carries the access_key, and has_full_document says whether GET /received-invoices/{access_key}/xml can already be called. The list (GET /received-invoices) remains the way to check coverage.
MCP tools and cost
In the Stackin MCP server, three tools cover this flow: list_received_invoices (read-only, reads what was already collected), sync_received_invoices (asks the SEFAZ now; inside the wait it is refused with the time to come back, and the agent should not repeat it in a loop) and manifest_received_invoice (binding and irreversible once accepted, so the agent should confirm with a person first). On an OAuth connection, listing needs the invoice:read scope, and sync and manifestation need invoice:write.
On cost: without a paid plan the first 50 received NF-e a month are included and each one after that takes R$ 0.07 from the account credit; paid plans include 100 to 1,000 a month and charge R$ 0.05 to R$ 0.02 after that, depending on the plan, once, when the invoice is stored; downloading the XML does not charge again. usage() in the SDKs shows used_received, included_received and received_free. The full route reference is at /api#received-invoices and the SDKs at /sdks#received-invoices.