sefaz_nfe

Elixir library for SEFAZ NF-e communication (modelo 55): sign, authorize, consult, DistDFe.

It does not calculate taxes. The ERP (or Nexus Pro) builds the XML; this library talks to SEFAZ.

Status: transport live and the whole NF-e lifecycle verified in homologação with a real ICP-Brasil A1: authorization (cStat 100, nfeProc assembled), CCe and cancellation (135), protocol consultation (100), and inutilização (102), all against SEFAZ BA.

Requires OTP 29.1 or later. Earlier OTP 29 aborts the TLS handshake on 20 UFs (malformed CA names in CertificateRequest, fixed by OTP-20327). On OTP 29.1+ with the ICP-Brasil v10 root configured, service_status/1 answers cStat 107 on 26 of 27 UFs. The exception is MA, which routes to SVAN and gets HTTP 403 from the server. The Ambiente Nacional and SVAN homologação endpoints also answer 403 ("You do not have permission to view this directory or page"), identically with curl/OpenSSL and the same certificate, so that is server-side. Ambiente Nacional production DistDFe answers normally.

Package shell

Public API is in SefazNfe (authorize/1, authorization_result/1, service_status/1, dist_dfe/1, consult_protocol/1, cancel/1, cce/1, manifest/2, void_numbers/1, start_dist_dfe_poller/1).

Input is validated and the UF URL is resolved from priv/endpoints/nfe_4.00.json. All services run live over mTLS, with documents signed by XMLDSig. mix test never opens a socket — config/test.exs swaps the transport for a stub.

TLS trust anchors

Nearly every UF serves certificates chained to Autoridade Certificadora Raiz Brasileira v10, which no OS bundle carries (measured: 24 of 27 UFs fail with {:tls, :unknown_ca} without it; only GO and MG use commercial roots, as does the Ambiente Nacional). Supply the roots rather than weakening verification:

config :sefaz_nfe, :cacerts, "/etc/ssl/icp-brasil.pem"

Fetch them from the ITI repository and check the fingerprints before installing; this library deliberately ships no trust anchors of its own (AD-009). The v10 root's SHA-256 fingerprint is:

6E:0B:FF:06:9A:26:99:4C:15:DE:2C:48:88:CC:54:AF:84:88:2E:54:95:B7:FB:F6:6B:E9:CC:FF:EC:74:89:F6

Without them the call fails as {:error, {:tls, :unknown_ca}}, named so the cause is obvious.

A1 (PKCS#12) is real. OTP 29 ships no PKCS#12 support and Hex has no package for it, so SefazNfe.Certificate.PKCS12 reads the PFX itself (RFC 7292), verified byte-for-byte against a real ICP-Brasil A1 and against openssl. pbeWithSHAAnd3-KeyTripleDES-CBC with a SHA-1 MAC — what ICP-Brasil issues today — is supported; a PBES2/AES-256 file is refused by name, never as a misleading "invalid password" (AD-007).

OTP (Elixir 1.20 / OTP 29): Task.Supervisor isolates SOAP, Registry + DistDFe poller per tax_id (Process.set_label/1), timeouts via Duration + to_timeout/1, JSON stdlib.

# Decodes the PKCS#12: a wrong password fails here, on the MAC, not at SOAP.
{:ok, cert} = SefazNfe.Certificate.load(pfx_binary, "senha")
SefazNfe.service_status(%{cert: cert, uf: "SP", environment: :homologation})
# => {:ok, %SefazNfe.Result{c_stat: 107, x_motivo: "Serviço em Operação"}}

{:ok, url} = SefazNfe.Endpoints.url("SP", :homologation, :nfe_autorizacao)

DistDFe poller

One labelled process per tax_id (CNPJ or CPF). The library never persists the cursor — the required :handler receives each page and answers with the cursor the host committed, so delivery is at-least-once:

SefazNfe.start_dist_dfe_poller(
  tax_id: "00000000000191",
  cert: cert,
  ult_nsu: MyApp.load_cursor!(),
  handler: fn %SefazNfe.DistDFe{} = page ->
    :ok = MyApp.store(page.documents)
    {:ok, MyApp.commit_cursor!(page.ult_nsu)}
  end
)

What this is

ERP / Nexus  →  XML (cálculo ICMS, IBS/CBS, totais)
sefaz_nfe    →  A1, XMLDSig, SOAP 4.00, cStat, DistDFe
SEFAZ        →  autorização / rejeição / documentos distribuídos

Same split as Focus/ACBr: they guarantee transport, not fiscal arithmetic.

Operating

Docs

Official sources

Endpoint table and schemas are owned by the RFB / ENCAT, not by this repo:

License

Apache-2.0