Brasilapi.Exchange.API (BrasilAPI v0.1.5)

View Source

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.

Summary

Functions

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

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

Functions

get_currencies()

@spec get_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_currencies()
{:ok, [%Brasilapi.Exchange.Currency{simbolo: "USD", nome: "Dólar dos Estados Unidos", tipo_moeda: "A"}]}

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

get_exchange_rate(currency, date)

@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"}}