NewConnect Claude, Cursor, ChatGPT, and other AI agents to API Ninjas via our MCP server

How to Get Exchange Rates in Python

Exchange Rate APIUpdated May 23, 2026
To get a currency exchange rate in Python, send a GET request to the API Ninjas Exchange Rate endpoint with a currency pair like USD_EUR. Here's a complete example:
fx.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/exchangerate',
    params={'pair': 'USD_EUR'},
    headers={'X-Api-Key': API_KEY},
)
print(response.json())

Prerequisites

Step 1: Get your API key

Sign up at api-ninjas.com/register for a free API key.

Step 2: Make the API request

The endpoint expects a pair parameter formatted as BASE_QUOTE using ISO 4217 currency codes:

fx.py
import requests

API_KEY = 'YOUR_API_KEY'
response = requests.get(
    'https://api.api-ninjas.com/v1/exchangerate',
    params={'pair': 'USD_EUR'},
    headers={'X-Api-Key': API_KEY},
)
print(response.json())

Step 3: Use the response

The API returns:

JSON
{
  "currency_pair": "USD_EUR",
  "exchange_rate": 0.9234
}

To convert an amount, multiply by exchange_rate: amount * exchange_rate.

Complete example

A reusable converter function with environment-variable key and timeouts:

fx.py
import os
import requests

API_KEY = os.environ['API_NINJAS_KEY']
URL = 'https://api.api-ninjas.com/v1/exchangerate'


def get_rate(base: str, quote: str) -> float:
    response = requests.get(
        URL,
        params={'pair': f'{base}_{quote}'},
        headers={'X-Api-Key': API_KEY},
        timeout=10,
    )
    response.raise_for_status()
    return response.json()['exchange_rate']


def convert(amount: float, base: str, quote: str) -> float:
    return amount * get_rate(base, quote)


if __name__ == '__main__':
    print(f"1 USD = {get_rate('USD', 'EUR'):.4f} EUR")
    print(f"100 USD = {convert(100, 'USD', 'JPY'):.2f} JPY")

Fetching multiple pairs

Python
pairs = [('USD', 'EUR'), ('USD', 'GBP'), ('USD', 'JPY'), ('USD', 'CAD')]

for base, quote in pairs:
    rate = get_rate(base, quote)
    print(f"{base}/{quote}: {rate:.4f}")

Error handling

Python
from requests.exceptions import HTTPError

try:
    rate = get_rate('USD', 'XYZ')
except HTTPError as e:
    if e.response.status_code == 400:
        print('Invalid currency pair')
    elif e.response.status_code == 401:
        print('Invalid or missing API key')
    elif e.response.status_code == 429:
        print('Rate limit exceeded')
    else:
        print(f'HTTP error: {e}')

For currency-to-amount conversion in a single call, see the Convert Currency API. For cryptocurrency rates use Crypto Price.

Frequently asked questions

What currencies are supported?

The API supports 150+ fiat currencies using standard ISO 4217 codes (USD, EUR, JPY, GBP, etc.).

How fresh are the exchange rates?

Rates are updated continuously from interbank foreign-exchange markets and reflect the latest published mid-market rate.

How do I format the currency pair parameter?

Use the format BASE_QUOTE — for example, USD_EUR for US dollar to euro. Both codes must be valid ISO 4217 codes.

Can I convert cryptocurrency?

For crypto-to-fiat or crypto-to-crypto rates, use the dedicated Crypto Price API instead.

Is there a free tier?

Yes. Every API Ninjas account includes a free tier — no credit card required.