Skip to content

xratesapi/python-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

xratesapi

PyPI version Python versions PyPI downloads license

Official Python SDK for the XRates exchange rate API.

Python 3.9+, built on httpx.

Install

pip install xratesapi

Quick start

from xratesapi import Client

client = Client("YOUR_API_KEY")

print(client.latest(base="USD", symbols=["EUR", "GBP"]))
print(client.convert("USD", "EUR", 100))

The client is also a context manager:

with Client("YOUR_API_KEY") as client:
    rates = client.latest()

Methods

Method Endpoint
latest(base="USD", symbols=None) GET /api/v1/latest
historical(date, base="USD", symbols=None) GET /api/v1/{YYYY-MM-DD}
convert(from_, to, amount, date=None) GET /api/v1/convert
timeseries(start_date, end_date, base="USD", symbols=None) GET /api/v1/timeseries
fluctuation(start_date, end_date, base="USD", symbols=None) GET /api/v1/fluctuation
currencies() GET /api/v1/currencies
status() GET /api/v1/status

Error handling

from xratesapi import (
    Client,
    ApiError,
    AuthenticationError,
    RateLimitError,
    ValidationError,
)

try:
    client.latest(base="XXX")
except AuthenticationError:
    ...  # 401 / 403
except RateLimitError:
    ...  # 429 — back off
except ValidationError as e:
    print(e.payload)  # raw response body
except ApiError as e:
    print(e.status, e)

Configuration

import httpx
from xratesapi import Client

# Custom timeout / base URL
client = Client("KEY", base_url="https://xratesapi.com", timeout=30.0)

# Or inject your own httpx.Client (retries, proxies, transport, ...)
http = httpx.Client(timeout=30.0, transport=httpx.HTTPTransport(retries=3))
client = Client("KEY", http_client=http)

License

MIT

About

Official Python SDK for the XRates exchange rate API

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages