You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> _The easy-to-use PHP currency conversion library. Retrieve exchange rates from 30 providers, with caching and fallback. Maintained since 2014._
9
9
10
-
Swap is a mature PHP **currency conversion library** for retrieving and working with exchange rates. It provides a single, easy-to-use API on top of multiple exchange rate providers, ranging from public sources (the European Central Bank, several national banks, exchangerate.host) to commercial **exchange rate APIs** that require an API key. Caching, historical rates, and a fallback chain are built in. Used in real-world PHP applications since 2014.
<strong>Sponsored by <a href="https://fastforex.io" target="_blank" rel="noopener">fastFOREX</a>.</strong> Real-time JSON API, 160+ currencies, 500+ cryptocurrencies, 21 ms average response. <strong>Free tier</strong>; paid plans from $18/month.
19
+
<a href="https://fastforex.io" target="_blank" rel="noopener"><strong>→ Get a free fastFOREX API key</strong></a>
20
+
</td>
21
+
</tr>
22
+
</table>
23
+
24
+
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. Maintained since 2014.
11
25
12
26
## 💡 What is Swap?
13
27
@@ -17,27 +31,6 @@ Swap is a mature PHP **currency conversion library** for retrieving and working
17
31
- It supports historical rates.
18
32
- It supports a fallback chain. When a provider errors, the next provider in the chain is tried.
19
33
20
-
## 🎯 When should you use Swap?
21
-
22
-
- Use Swap when you need to retrieve exchange rates in a PHP application: currency conversion workflows, multi-currency pricing, invoice totals, reconciliation, or historical FX data.
23
-
- Use the lower-level [Exchanger](https://github.com/florianv/exchanger) library when Swap's defaults are too opinionated and you want finer control over chain composition, caching, or HTTP plumbing.
24
-
25
-
## Why not call an exchange rate API directly?
26
-
27
-
You can integrate a single exchange rate API directly in your application.
28
-
29
-
Swap is useful when you need more than a single provider:
30
-
31
-
-**Provider abstraction** — switch providers without rewriting your code
32
-
-**Fallback support** — if one provider fails, another can be used automatically
33
-
-**Unified interface** — all providers share the same API
34
-
-**Caching** — reduce API calls and improve performance
35
-
-**Flexibility** — combine public and commercial providers
36
-
37
-
For simple use cases, calling a single API may be enough.
38
-
39
-
Swap becomes valuable when you need reliability, flexibility, or long-term maintainability.
The recommended setup uses **[fastFOREX](https://fastforex.io)** (the project's sponsor) as the primary provider. [Grab a free key](https://fastforex.io) and you're ready.
47
+
53
48
```php
54
49
use Swap\Builder;
55
50
56
-
// Build Swap with the European Central Bank (free, no API key required).
51
+
// Recommended: fastFOREX. Get a freeAPI key at https://fastforex.io
->add('european_central_bank') // free fallback for EUR-base pairs
75
+
->add('european_central_bank')
85
76
->build();
77
+
78
+
$rate = $swap->latest('EUR/USD');
86
79
```
87
80
88
-
Providers are tried in order. If a provider does not support the requested currency pair, it is skipped silently. If a provider throws an error, the next provider is tried. If every provider fails, a `ChainException` is thrown with all collected errors.
81
+
The European Central Bank publishes EUR-base rates with daily granularity. For non-EUR base pairs, more frequent updates, or a wider currency list, switch to fastFOREX or another commercial provider.
The Swap ecosystem is a layered toolkit for currency conversion in PHP:
93
+
// Free fallback for EUR-base pairs
94
+
->add('european_central_bank')
95
+
->build();
96
+
```
101
97
102
-
-**Swap.** The easy-to-use, high-level API (this package).
103
-
-**Exchanger.** Lower-level, more granular alternative; direct access to the 30 provider implementations and the `ExchangeRateService` interface.
104
-
-**Laravel Swap.** Laravel application of Swap.
105
-
-**Symfony Swap.** Symfony integration of Swap.
98
+
Providers are tried in order. If a provider does not support the requested currency pair, it is skipped silently. If a provider throws an error, the next provider is tried. If every provider fails, a `ChainException` is thrown with all collected errors.
106
99
107
-
All four packages are MIT-licensed and require PHP 8.2 or newer.
100
+
For amount conversion (including the [moneyphp/money](https://github.com/moneyphp/money) integration via `SwapExchange`), see [Converting amounts](doc/readme.md#converting-amounts) in the documentation.
108
101
109
102
## 📊 Providers
110
103
111
-
Start with a single provider (for example the European Central Bank), then add others as needed.
104
+
Swap supports 30 exchange rate providers. Pass the **identifier** to `Builder::add()`.
105
+
106
+
### Commercial providers (require an API key)
112
107
113
-
Swap supports 30 exchange rate providers via [Exchanger](https://github.com/florianv/exchanger). Pass the **identifier** to `Builder::add()`.
108
+
| Service | Identifier | Base | Quote | Historical |
You can also add your own provider by implementing the `Exchanger\Contract\ExchangeRateService` interface and passing the instance to `Builder::addExchangeRateService()`.
154
146
155
-
## ⚙ Caching, HTTP client, and error handling
156
-
157
-
-**Caching.** Swap uses PSR-16 `SimpleCache`. Configure once on the builder:
158
-
159
-
```php
160
-
$swap = (new Builder())->useSimpleCache($psr16Cache)->add('european_central_bank')->build();
161
-
```
162
-
163
-
Disable caching for a single query: `$swap->latest('EUR/USD', ['cache' => false])`.
164
-
Override the TTL for a single query: `$swap->latest('EUR/USD', ['cache_ttl' => 3600])`.
165
-
166
-
-**HTTP client.** Any PSR-18 client (`symfony/http-client`, `php-http/guzzle7-adapter`, etc.) is supported and auto-discovered via `php-http/discovery`. To pass an explicit instance, use `Builder::useHttpClient()`.
167
-
168
-
-**Errors.** When every configured provider has either skipped (unsupported pair) or thrown, Swap raises an `Exchanger\Exception\ChainException` containing all collected exceptions.
169
-
170
-
## 📚 Documentation
147
+
## 🎯 When should you use Swap?
171
148
172
-
The full documentation is in [`doc/readme.md`](doc/readme.md), and is also published at [florianv.github.io/swap](https://florianv.github.io/swap/).
149
+
- Use Swap when you need to retrieve exchange rates in a PHP application: currency conversion workflows, multi-currency pricing, invoice totals, reconciliation, or historical FX data.
150
+
- Use the lower-level [Exchanger](https://github.com/florianv/exchanger) library when Swap's defaults are too opinionated and you want finer control over chain composition, caching, or HTTP plumbing.
173
151
174
-
## 🧩 Related packages
152
+
## 🛠 Common use cases
175
153
176
-
The Swap ecosystem:
154
+
- Display localized prices in multi-currency storefronts.
155
+
- Compute invoice totals across currencies.
156
+
- Reconcile multi-currency ledgers using historical rates.
157
+
- Power internal FX dashboards with rate history.
158
+
- Build currency conversion infrastructure for fintech and ERP applications.
-[**Exchanger**](https://github.com/florianv/exchanger): lower-level, more granular alternative; direct access to provider implementations.
180
-
-[**Laravel Swap**](https://github.com/florianv/laravel-swap): Laravel application of Swap.
181
-
-[**Symfony Swap**](https://github.com/florianv/symfony-swap): Symfony integration of Swap.
160
+
## 🧭 Which package should I use?
182
161
183
-
## 🤝 Sponsorship
162
+
The Swap ecosystem is a layered toolkit for currency conversion in PHP:
184
163
185
-
The Swap ecosystem is open to selected sponsorships from exchange rate API providers and financial infrastructure companies.
164
+
-[**Swap**](https://github.com/florianv/swap). The easy-to-use, high-level API (this package).
165
+
-[**Exchanger**](https://github.com/florianv/exchanger). Lower-level, more granular alternative; direct access to the 30 provider implementations and the `ExchangeRateService` interface.
166
+
-[**Laravel Swap**](https://github.com/florianv/laravel-swap). Laravel application of Swap.
167
+
-[**Symfony Swap**](https://github.com/florianv/symfony-swap). Symfony integration of Swap.
186
168
187
-
Sponsorship can include:
169
+
All four packages are MIT-licensed and require PHP 8.2 or newer.
188
170
189
-
- Documentation visibility
190
-
- Integration examples
191
-
- Ecosystem-level visibility across Swap, Exchanger, Laravel Swap, and Symfony Swap
171
+
## 📚 Documentation
192
172
193
-
For inquiries, contact the maintainer via [GitHub](https://github.com/florianv).
173
+
Caching (PSR-16), HTTP client selection (PSR-18 / Guzzle / `useHttpClient`), error handling (`ChainException`), per-query options and the full provider configuration reference live in [`doc/readme.md`](doc/readme.md). The same content is also published at [florianv.github.io/swap](https://florianv.github.io/swap/).
0 commit comments