# `Brasilapi.Exchange.API`
[🔗](https://github.com/stlucasgarcia/brasilapi-ex/blob/main/lib/brasilapi/exchange/api.ex#L1)

Client for BrasilAPI Exchange Rate endpoints.

Provides functions to fetch currency information and exchange rates
between Real and other currencies from the Central Bank of Brazil.

# `get_exchange_currencies`

```elixir
@spec get_exchange_currencies() ::
  {:ok, [Brasilapi.Exchange.Currency.t()]} | {:error, map()}
```

Fetches all available currencies that can be used for exchange rate queries.

## Examples

    iex> Brasilapi.Exchange.API.get_exchange_currencies()
    {:ok, [%Brasilapi.Exchange.Currency{simbolo: "USD", nome: "Dólar dos Estados Unidos", tipo_moeda: "A"}]}

    iex> Brasilapi.Exchange.API.get_exchange_currencies()
    {:error, %{status: 500, message: "Internal server error"}}

## API Reference
  https://brasilapi.com.br/docs#tag/CAMBIO/paths/~1cambio~1v1~1moedas/get

# `get_exchange_rate`

```elixir
@spec get_exchange_rate(
  String.t(),
  String.t() | Date.t() | DateTime.t() | NaiveDateTime.t()
) ::
  {:ok, Brasilapi.Exchange.DailyExchangeRate.t()} | {:error, map()}
```

Fetches the exchange rate between Real and another currency for a specific date.

For weekends and holidays, the returned date will be the last available business day.
Data is available from November 28, 1984 onwards.

## Parameters

  * `currency` - The target currency symbol (string). Available: AUD, CAD, CHF, DKK, EUR, GBP, JPY, SEK, USD
  * `date` - The desired date. Accepts Date/DateTime/NaiveDateTime structs or strings in YYYY-MM-DD format

## Examples

    iex> Brasilapi.Exchange.API.get_exchange_rate("USD", "2025-02-13")
    {:ok, %Brasilapi.Exchange.DailyExchangeRate{moeda: "USD", data: "2025-02-13", cotacoes: [...]}}

    iex> Brasilapi.Exchange.API.get_exchange_rate("USD", ~D[2025-02-13])
    {:ok, %Brasilapi.Exchange.DailyExchangeRate{moeda: "USD", data: "2025-02-13", cotacoes: [...]}}

    iex> Brasilapi.Exchange.API.get_exchange_rate("USD", ~U[2025-02-13 14:30:00Z])
    {:ok, %Brasilapi.Exchange.DailyExchangeRate{moeda: "USD", data: "2025-02-13", cotacoes: [...]}}

    iex> Brasilapi.Exchange.API.get_exchange_rate("INVALID", "2025-02-13")
    {:error, %{status: 404, message: "Not found"}}

## API Reference
  https://brasilapi.com.br/docs#tag/CAMBIO/paths/~1cambio~1v1~1cotacao~1%7Bmoeda%7D~1%7Bdata%7D/get

---

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