Exchange rates in PHP and Laravel
Fetch and cache live exchange rates with Laravel’s HTTP client, bearer authentication, bounded timeouts, and freshness metadata.
A small Laravel service can call exchangerate.dev, reject failed HTTP responses, and cache a complete FX observation for 60 seconds. Keep the API key in configuration and return source, market_session, and data_updated_at with the rates.
throw() for non-success responses.config/services.php through EXCHANGERATE_API_KEY, not in application code.Build one cached FX service
The service below creates a stable cache key from the base and symbol list. The callback stores only a successful JSON response because throw() raises before Laravel writes a failed request into the cache.
Configure the key outside source control
Read the rate and its observation fields
Call $service->latest("USD", ["EUR", "GBP"]), then read $result["rates"]["EUR"]. Keep $result["source"], $result["market_session"], and $result["data_updated_at"] in the view model or stored record so the value never loses its context.
- Sort symbols before building the cache key.
- Set a bounded HTTP timeout.
- Call throw before reading and caching JSON.
- Round the final amount for display, not the API rate.