Rate providers
A provider fetches rates from one API and returns them against its own base currency. The Currency facade uses one provider at a time: default_provider from config, or whatever you switched to.
Built-in providers
Section titled “Built-in providers”| Alias | Source | API key | Currencies | Buy/sell | Historical |
|---|---|---|---|---|---|
monobank | Monobank public API | no | 15 against UAH | real | no |
privatbank | PrivatBank public API | no | EUR, USD | real | yes |
nbu | National Bank of Ukraine | no | all NBU official rates | equal (official rate) | yes |
jsdelivr | @fawazahmed0/currency-api via jsDelivr CDN | no | 150+ | synthetic ±0.5% | yes, from 2024-03-06 |
exchangeratesapi | exchangeratesapi.io, or frankfurter.dev without a key | optional | ECB list without a key | equal | yes |
currencyapi | currencyapi.com | required | per API | equal | yes |
fixer | fixer.io | required | per API | equal | yes |
Base currency of every built-in provider is UAH. Per-provider details, URLs and limitations — Providers reference.
Switching provider
Section titled “Switching provider”Currency::useProvider('nbu'); // alias from config('currency.providers')Currency::setRateProvider('nbu'); // alias,Currency::setRateProvider(NbuRateProvider::class); // class name,Currency::setRateProvider(new NbuRateProvider()); // or an instance
Currency::useProvider('nbu')->getRate('USD'); // both return the Currency instanceuseProvider() accepts only configured aliases (InvalidArgumentException: Provider 'x' is not configured); setRateProvider() also takes a class name or an instance. A class that doesn’t implement RateProvider throws InvalidArgumentException.
Currency::getProvider(); // current RateProvider instance (alias: getRateProvider())Currency::getAvailableProviders(); // config('currency.providers')Using a provider directly
Section titled “Using a provider directly”The CurrencyProvider facade (currency.manager, a ProviderManager) builds provider instances without touching the shared Currency instance. It is not auto-aliased — import it by class name:
use Fomvasss\Currency\Facades\CurrencyProvider;
$nbu = CurrencyProvider::provider('nbu'); // new NbuRateProvider instance$nbu->getRates(); // raw ['USD' => ['buy' => .., 'sell' => ..], ...] against UAH$nbu->getRate('USD'); // ['buy' => .., 'sell' => ..] or null
CurrencyProvider::provider(); // default_provider (alias only, not a class name)CurrencyProvider::getRates(); // unknown methods are forwarded to a new default providerRaw provider rates are not recalculated to your default base currency and not reduced by rate type. See Contracts, events & manager.
Constructor arguments
Section titled “Constructor arguments”Providers are built through the service container ($container->make($class)). ExchangeRatesApiProvider, CurrencyApiProvider and FixerProvider accept (?string $apiKey = null, string $baseCurrency = 'UAH'); the API key falls back to config. To change the base currency, bind the class in a service provider:
use Fomvasss\Currency\RateProviders\ExchangeRatesApiProvider;
public function register(): void{ $this->app->bind(ExchangeRatesApiProvider::class, fn () => new ExchangeRatesApiProvider(null, 'EUR'));}Set default in config/currency.php to the same currency ('EUR'): when the two differ, Currency needs the provider to return a rate for your default currency to recalculate, and frankfurter has none for UAH.
monobank, privatbank, nbu and jsdelivr have no constructor; their base currency is the $baseCurrency property (UAH), which a subclass can override — but only if the API really returns rates against that currency.
Choosing a provider
Section titled “Choosing a provider”- Ukrainian shop, bank rates —
monobank(buy/sell, many currencies) orprivatbank(USD/EUR only) - Official rate, accounting, historical data —
nbu - Many currencies, no key —
jsdelivr(daily mid rates; the spread is synthetic) - Fallback between providers isn’t built in — when the API fails, the provider serves its own fallback cache. For a chain of providers, write a multi-source provider.