Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Swap Laravel Package

florianv/swap

PHP 8.2+ currency exchange rate library with a single API over 30+ providers. Supports conversion, historical rates, PSR-16 caching, and provider fallback. Works with PSR-18 HTTP clients and PSR-17 factories for flexible integrations.

View on GitHub
Deep Wiki
Context7

PHP currency conversion library for retrieving exchange rates from 30 providers, with caching and fallback. Maintained since 2014.

Frequently asked questions about Swap
How do I install Swap in a Laravel project?
Run `composer require florianv/laravel-swap` to install the Laravel-specific package. The package includes a Service Provider and Facade, so no additional setup is needed beyond configuring your API keys in `.env`. For the core library, use `composer require florianv/swap` and its dependencies like `php-http/curl-client`.
Which Laravel versions does Swap support?
Swap officially supports Laravel 8.x and 9.x. The package leverages Laravel’s service container and configuration system, so it integrates seamlessly with these versions. For older versions (7.x), you may need to manually adapt the Service Provider binding.
Can I use Swap with MoneyPHP for currency conversions?
Yes, Swap is designed to work with MoneyPHP. The library returns rate objects with methods like `getValue()` and `getDate()`, which can be used to convert amounts between currencies using MoneyPHP’s `Money` class. The Laravel package simplifies this integration further.
How do I configure fallback providers in Swap?
Use the `Builder` class to chain providers. Start with your primary provider (e.g., Fixer) and add fallbacks in order of preference. For example, `->add('apilayer_fixer', ['api_key' => env('FIXER_API_KEY')])->add('apilayer_currency_data', ['api_key' => env('CURRENCYLAYER_API_KEY')])`. Swap will automatically try each provider until it gets a successful response.
What caching options are available for Swap in Laravel?
Swap supports PSR-16 (Simple Cache) and PSR-6 (Cache) adapters, which Laravel already uses. You can cache rates using Redis, database, or file drivers. Configure the cache driver in Laravel’s `config/cache.php` and bind it to Swap’s `CacheService` in your `AppServiceProvider`.
How do I handle API key management securely in Laravel?
Store API keys in Laravel’s `.env` file (e.g., `FIXER_API_KEY=your_key`). Use Laravel’s `env()` helper or the `config()` method to retrieve keys in your code. For production, consider using a secrets manager like HashiCorp Vault or Laravel Forge to avoid hardcoding sensitive data.
Does Swap support historical exchange rates?
Yes, Swap supports historical rates via the `historical()` method. Pass a `DateTime` object to fetch rates for a specific date. Not all providers support historical data, so check the [documentation](https://github.com/florianv/swap/blob/master/doc/readme.md) for provider-specific limitations.
How do I test Swap’s fallback mechanism during development?
Use HTTP mocking tools like VCR or Mockery to simulate API failures. For example, with VCR, record a successful request and then modify the response to simulate errors. This helps test how Swap handles fallbacks without hitting rate limits or incurring costs.
Are there any alternatives to Swap for Laravel?
Alternatives include `spatie/currency-converter` (simpler, single-provider focus) and `mollie/api-library` (if you’re using Mollie’s payment services). However, Swap stands out for its multi-provider fallback system, caching, and MoneyPHP integration, making it ideal for complex financial applications.
How do I optimize Swap for high-traffic Laravel applications?
Offload rate fetching to Laravel queues (e.g., `swap:fetch-rates` jobs) to avoid blocking requests. Use Redis for caching to reduce API calls, and implement a cache warming strategy (e.g., cron jobs) to preload rates. Monitor API usage with Laravel Horizon or Prometheus to avoid hitting rate limits.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope