# `SefazNfe.Endpoints`
[🔗](https://github.com/devaction-labs/sefaz_nfe/blob/v0.5.0/lib/sefaz_nfe/endpoints.ex#L1)

Resolves `{uf, environment, service}` to a SOAP URL, per document model.

Snapshots in `priv/endpoints/nfe_4.00.json` (model 55) and
`priv/endpoints/nfce_4.00.json` (model 65), both nfephp mirrors of the RFB
tables. Source of truth remains
https://www.nfe.fazenda.gov.br/portal/webServices.aspx

The two tables share a schema: `authorizers` per authorizer and environment,
`uf_authorizer` per UF. They differ in who serves a UF: for model 65 only
AM, GO, MG, MS, MT, PR, RS and SP run their own, and SVRS serves every other
UF (there is no SVAN for model 65), so BA resolves to a different host per
model. The model 65 table also carries `qr_code` and `url_chave`, the two
per-UF addresses printed on a DANFCE and written into `infNFeSupl`.

# `environment`

```elixir
@type environment() :: :homologation | :production
```

# `model`

```elixir
@type model() :: 55 | 65 | 57 | 67 | 58
```

# `service`

```elixir
@type service() ::
  :nfe_autorizacao
  | :nfe_ret_autorizacao
  | :nfe_consulta_protocolo
  | :nfe_status_servico
  | :nfe_recepcao_evento
  | :nfe_inutilizacao
  | :nfe_distribuicao_dfe
  | :nfe_consulta_cadastro
  | :cte_recepcao_sinc
  | :cte_recepcao_os
  | :cte_consulta
  | :cte_status_servico
  | :cte_recepcao_evento
  | :mdfe_recepcao_sinc
  | :mdfe_consulta
  | :mdfe_status_servico
  | :mdfe_recepcao_evento
  | :mdfe_consulta_nao_enc
```

# `contingency_for`

```elixir
@spec contingency_for(String.t() | atom(), model()) ::
  {:ok, :svc_an | :svc_rs | :svc_sp}
  | {:error, {:unknown_uf, String.t()}}
  | {:error, {:unsupported_for_model, model()}}
  | {:error, {:unsupported_model, term()}}
```

The SVC (Sefaz Virtual de Contingência) that serves `uf`.

Which one is data (`uf_contingency` in the snapshot). Model 55 (the default)
has SVC-RS for AM, BA, GO, MA, MS, MT, PE and PR and SVC-AN for every other
UF, from the MOC. Models 57 and 67 have SVC-SP for the UFs authorized by SVRS,
MG, PR and RS and SVC-RS for those authorized by SVSP, MT, MS and SP, from the
CT-e portal. NFC-e and MDF-e have none (`{:unsupported_for_model, model}`;
MDF-e contingency is off-line). Nothing here decides to use it; contingency
is the caller's choice (AD-005).

# `contingency_url`

```elixir
@spec contingency_url(:svc_an | :svc_rs | :svc_sp, environment(), service(), model()) ::
  {:ok, String.t()} | {:error, term()}
```

The URL of `service` on the SVC `svc`: `:svc_an` or `:svc_rs` for model 55
(the default), `:svc_sp` or `:svc_rs` for models 57 and 67.

It does not check that `svc` serves any particular UF; see `contingency_for/2`.
For CT-e, SVC-SP is served by the SVSP endpoints and SVC-RS by the SVRS ones.

# `cte_qr_code_url`

```elixir
@spec cte_qr_code_url(String.t() | atom(), environment()) ::
  {:ok, String.t()} | {:error, term()}
```

The CT-e QR code base address (`infCTeSupl/qrCodCTe`) for `uf`.

It is the portal of the authorizer that serves the emitting UF (SVRS for BA,
SVSP for PE, the UF's own for MG, MS, MT, PR, RS and SP), per environment, as
NFePHP does; the same one is used when the document is sent through an SVC.
Returned as published: the case of the path differs between portals and the
MOC says it is not validated.

# `mdfe_qr_code_url`

```elixir
@spec mdfe_qr_code_url(environment()) :: {:ok, String.t()} | {:error, term()}
```

The QR code base URL of an MDF-e (model 58).

One SVRS portal address for every UF; the query string the QR code carries
(`chMDFe`, `tpAmb`, and `sign` in contingency) is `SefazNfe.MDFe.QRCode`'s.

# `operation`

```elixir
@spec operation(String.t() | atom(), service(), model()) :: String.t() | nil
```

The SOAP operation `service` is called with on the authorizer serving `uf`,
or `nil` for a service whose operation is fixed (see `SefazNfe.SOAP.Envelope`).

`NFeConsultaCadastro` is the exception: its authorizers disagree on the
operation name (`consultaCadastro` on BA, GO, MS, MT and SP;
`consultaCadastro4` elsewhere), so the snapshot carries it as data under
`"operations"`, keyed by service and then authorizer, with a `"default"`.

# `qr_code_url`

```elixir
@spec qr_code_url(String.t() | atom(), environment()) ::
  {:ok, String.t()} | {:error, term()}
```

The QR code base URL of `uf`'s own SEFAZ, for model 65.

Always the emitting UF's, even where SVRS authorizes the document: SVRS has
no QR code address of its own.

# `snapshot_date`

```elixir
@spec snapshot_date() :: String.t()
```

# `uf_code`

```elixir
@spec uf_code(String.t() | atom()) ::
  {:ok, pos_integer()} | {:error, {:unknown_uf, String.t()}}
```

IBGE code for `uf`, which the 4.00 envelopes carry as `cUF`.

The Ambiente Nacional is 91.

# `url`

```elixir
@spec url(String.t() | atom(), environment(), service(), model()) ::
  {:ok, String.t()}
  | {:error, {:unknown_endpoint, String.t(), service()}}
  | {:error, {:invalid_environment, term()}}
  | {:error, {:unknown_service, term()}}
  | {:error, {:unsupported_model, term()}}
```

Resolves the SOAP URL for `{uf, environment, service}` under `model`
(55 by default; 65 for NFC-e, 57 and 67 for CT-e and CT-e OS, 58 for MDF-e).

The failures are distinct on purpose: `{:unknown_endpoint, uf, service}`
is a UF this snapshot cannot resolve, while `{:invalid_environment, _}` and
`{:unknown_service, _}` mean the caller passed something that is not an
environment or not a 4.00 service at all, and `{:unsupported_model, _}` a
model this library has no table for. A service that belongs to another model
(`:nfe_autorizacao` under 57) is also `{:unknown_service, _}`.

# `url_chave`

```elixir
@spec url_chave(String.t() | atom(), environment()) ::
  {:ok, String.t()} | {:error, term()}
```

The "consulta pela chave" address of `uf` for model 65, which `urlChave` and
the DANFCE carry.

Returned exactly as published: some entries have no scheme and some are
plain http, and that is what SEFAZ expects to read back.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
