A client to interact with the Forex API for fetching currency rates and conversion data

Hierarchy (view full)

Constructors

Properties

timeout: number
headers: HeadersInit

Methods

  • Fetches the list of available currencies

    Parameters

    • Optionaldate: Date | "latest" = 'latest'

      The date for fetching currencies, or "latest" for the most recent

    • Optionaloptions: RequestInit = {}

      Additional request options

    Returns Promise<HttpResponse<AvailableCurrency[]>>

    A list of available currencies

    // => {
    // code: 200,
    // message: 'OK',
    // data: [
    // { code: 'eur', name: 'Euro' },
    // { code: 'usd', name: 'US Dollar' },
    // { code: 'cny', name: 'Chinese Yuan Renminbi' },
    // ... More items
    // ]
    // }
    new ForexClient().getCurrencies('latest');
  • Fetches the exchange rates for a specific currency

    Parameters

    • Optionalcode: string = ...

      The currency code or locale code to get rates for

    • Optionaldate: Date | "latest" = 'latest'

      The date for the rates, or 'latest' for the most recent

    • Optionaloptions: RequestInit = {}

      Additional request options

    Returns Promise<HttpResponse<ExchangeRate[]>>

    A list of exchange rates

    // => {
    // code: 200,
    // message: 'OK',
    // data: [
    // { code: 'eur', rate: 100_000 },
    // { code: 'usd', rate: 100_000 },
    // { code: 'cny', rate: 100_000 },
    // ... More items
    // ]
    // }
    new ForexClient().getRates('BTC');
  • Fetches the exchange rate between two currencies

    Parameters

    • OptionalbaseCode: string = ...

      The base currency code or locale code

    • OptionaldestCode: string

      The destination currency code or locale code

    • Optionaldate: Date | "latest" = 'latest'

      The date for the rate, or 'latest' for the most recent

    • Optionaloptions: RequestInit = {}

      Additional request options

    Returns Promise<HttpResponse<number>>

    The exchange rate

    // => {
    // code: 200,
    // message: 'OK',
    // data: 100_000
    // }
    new ForexClient().getRate('BTC', 'USD');
  • Get a valid currency code based on locale

    Parameters

    • localeCode: string

      The locale code to get currency code for

    Returns string

    The corresponding currency code

    // => 'USD'
    new ForexClient().getCode('US');
    // => 'CNH'
    new ForexClient().getCode('HK');
    // => 'CNY'
    new ForexClient().getCode('RMB');
  • Converts an amount from one currency to another

    Parameters

    • OptionalbaseCode: string = ...

      The base currency code or locale code

    • OptionaldestCode: string

      The destination currency code or locale code

    • Optionalamount: string | number = 0

      The amount to convert

    • Optionaldate: Date | "latest" = 'latest'

      The date for the conversion rate, or 'latest' for the most recent

    • Optionaloptions: RequestInit = {}

      Additional request options

    Returns Promise<HttpResponse<number>>

    The converted amount

    // => {
    // code: 200,
    // message: 'OK',
    // data: 100_000
    // }
    new ForexClient().convert('BTC', 'USD', 1);
    // => {
    // code: 200,
    // message: 'OK',
    // data: 7.27
    // }
    new ForexClient().convert('US', 'HK', 1);