Skip to main content

Code Examples

Choose your programming language to see working examples for the OpenFXRates API.

Available Languages

  • PHP - Examples using cURL and Guzzle
  • JavaScript - Fetch API and Node.js examples
  • Python - Requests library examples
  • Java - Examples using Java HTTP Client and OkHttp
  • Rust - Examples using reqwest and async patterns
  • Go - Examples using standard library and net/http
  • Dart - Examples using http package, suitable for Flutter

Generic Examples

Get Latest Rates

Fetch the latest exchange rates for a base currency:

curl -X GET "https://api.openfxrates.com/latest_rates?base=USD&targets=EUR,GBP" \
-H "X-API-Key: your-api-key"

Get Historical Rates

Fetch exchange rates for a specific date:

curl -X GET "https://api.openfxrates.com/historical_rates?base=USD&date=2024-01-15&targets=EUR" \
-H "X-API-Key: your-api-key"

Convert Currency

Convert an amount from one currency to multiple currencies:

curl -X GET "https://api.openfxrates.com/convert?from=USD&to=EUR,GBP&amount=100" \
-H "X-API-Key: your-api-key"

List All Currencies

Get all available currencies:

curl -X GET "https://api.openfxrates.com/currencies" \
-H "X-API-Key: your-api-key"

Language-Specific Guides

For detailed examples with error handling and best practices, see the language-specific guides:

Choosing a Language

LanguageBest ForLibrary
PHPWeb backends, Laravel, SymfonycURL or Guzzle
JavaScriptWeb frontends, Node.js serversFetch API or Axios
PythonData science, automation, scriptsRequests or httpx

Common Patterns

1. Error Handling

Always handle errors gracefully:

curl -i -X GET "https://api.openfxrates.com/latest_rates?base=INVALID" \
-H "X-API-Key: your-api-key"

Check the HTTP status code and parse error messages.

2. Rate Limiting

Monitor rate limit headers:

X-RateLimit-Limit: 10
X-RateLimit-Remaining: 8
X-RateLimit-Reset: 1640000060

If you receive a 429 response, wait before retrying.

3. Environment Variables

Store your API key in an environment variable:

export OPENFXRATES_API_KEY="sk_test_abc123"
curl -X GET "https://api.openfxrates.com/latest_rates?base=USD" \
-H "X-API-Key: $OPENFXRATES_API_KEY"

4. Response Parsing

All responses are JSON. Parse them appropriately:

{
"base": "USD",
"date": "2024-12-25",
"rates": {
"EUR": 0.92,
"GBP": 0.79
}
}

Next Steps