Description

The ExchangeRatesAPI.convert() method is used to communicate with this endpoint as seen below:

ExchangeRatesAPI.convert(base, target, Optional[date, base_amount])

Query Parameters

Below are the query parameters for this method:

base
String
required

The base currency to be converted. It must be a currency code supported by AbstractAPI.

target
String
required

The target currency to convert a base currency into. Only one target currency is permitted per request.

date
String

An historical date that the response must refer to. This parameter should be used if you want to get past exchange rates records. If blank, the response will be based on live rates. The acceptable date format is: YYYY-MM-DD, e.g 2005-02-30

base_amount
Float

A specific amount of the base currency to be converted.

Usage

Below is a code snippet to query the ExchangeRatesAPI.convert() method:

from abstract_python import ExchangeRatesAPI

api_key = "your-exchange-rates-api-key"

api = ExchangeRatesAPI(api_key)
base = "GBP"
target = "USD"
response = api.convert(base, target)

print(response)

Below is the response object:

{
    'base': 'GBP', 
    'target': 'USD', 
    'base_amount': 1, 
    'converted_amount': 1.340148, 
    'exchange_rate': 1.340148, 
    'last_updated': 1727615700, 
    'status': 200
}

Response Fields

base
String

The base currency used for the request

target
String

The target currency that the base currency is converted into

date
String

The date the rates were pulled from.

base_amount
Float

The specific amount of base currency that was converted.

converted_amount
Float

The base amount equivalent of the base currency in the target currency.

exchange_rate
Float

The exchange rate used to convert the base amount from the base currency to the target currency.

status
integer

The status code of the response.

Using optional parameters

Below is a code snippet that uses the optional parameters:

from abstract_python import ExchangeRatesAPI

api_key = "your-exchange-rates-api-key"

api = ExchangeRatesAPI(api_key)
base = "GBP"
target = "USD"
date = "2024-02-10"
base_amount = 5.0
response = api.convert(base, target, base_amount=base_amount, date=date)

print(response)

Below is the response object:

{
    'base': 'GBP', 
    'target': 'USD', 
    'base_amount': 5, 
    'converted_amount': 6.303839, 
    'exchange_rate': 1.260768, 
    'date': '2024-02-10', 
    'status': 200}