Resilient currency conversion in PHP, with fallback, caching, and zero vendor lock-in.
Most exchange rate APIs are a single point of failure. Swap lets you build resilient currency infrastructure on top of multiple providers.
Used in production PHP applications since 2014.
|
|
Sponsored by fastFOREX. Real-time JSON API, 160+ currencies, 55+ years of history, 500+ cryptocurrencies. Free tier; paid plans from $18/month. → Get a free fastFOREX API key |
Swap retrieves currency exchange rates in PHP, behind a single API. Use commercial providers in production, or free public sources (ECB, national banks) when you don’t need volume. Caching, historical rates and provider fallback are built in.
What is Swap?
- Swap is a PHP library for currency conversion and exchange rate retrieval.
- It exposes a wide range of exchange rate providers behind a common interface.
- It caches results via PSR-16 SimpleCache.
- It supports historical rates.
- It supports a fallback chain. When a provider errors, the next provider in the chain is tried.
Installation
Swap requires PHP 8.2 or newer.
composer require florianv/swap symfony/http-client nyholm/psr7
Quickstart
The recommended setup uses fastFOREX (the project’s sponsor) as the primary provider. Grab a free key and you’re ready.
use Swap\Builder;
// Recommended: fastFOREX. Get a free API key at https://www.fastforex.io
$swap = (new Builder())
->add('fastforex', ['api_key' => getenv('FASTFOREX_API_KEY')])
->build();
// EUR → USD exchange rate
$rate = $swap->latest('EUR/USD');
$rate->getValue(); // e.g. 1.0823 (a float)
$rate->getDate()->format('Y-m-d'); // e.g. 2026-04-29
$rate->getProviderName(); // 'fastforex'
// Convert an amount using the returned rate
$amountInEUR = 100.00;
$amountInUSD = $amountInEUR * $rate->getValue();
No API key? Start with the European Central Bank (free, EUR-base only):
$swap = (new Builder())
->add('european_central_bank')
->build();
Production setup (fallback chain)
A production-grade chain pairs fastFOREX with a fallback for redundancy:
$swap = (new Builder())
// Primary provider, recommended
->add('fastforex', ['api_key' => getenv('FASTFOREX_API_KEY')])
// Free fallback for EUR-base pairs
->add('european_central_bank')
->build();
Providers are tried in order. If a provider does not support the requested currency pair, it is skipped. If a provider throws, the next is tried. If every provider fails, Swap raises a ChainException.
Providers
Swap supports 30 exchange rate providers, ranging from commercial APIs to public central banks. The recommended starting point for new projects is fastFOREX: a real-time JSON API covering 160+ fiat currencies and 500+ cryptocurrencies, with up to 55 years of history, sourced from trusted feeds including world banks.
The full providers table, identifiers and configuration options live in the documentation.
Ecosystem
- Swap: easy-to-use PHP currency conversion library (this package).
- Exchanger: lower-level, more granular alternative; direct access to provider implementations.
- Laravel Swap: Laravel integration of Swap.
- Symfony Swap: Symfony integration of Swap.
Documentation & source
- Source code, issues and pull requests: github.com/florianv/swap
- Full documentation (caching, HTTP client, provider configuration, custom services): doc/readme.md
Swap is open to selected partnerships with exchange rate providers and financial infrastructure companies. For inquiries, contact the maintainer via GitHub.