---
title: "PHP Exchange Rate API with Laravel"
description: "Fetch and cache live exchange rates with Laravel’s HTTP client, bearer authentication, bounded timeouts, and freshness metadata."
canonical: "https://exchangerate.dev/learn/php-laravel-exchange-rates"
---

> Index of every exchangerate.dev page and doc for AI readers: [llms.txt](https://exchangerate.dev/llms.txt)

# Exchange rates in PHP and Laravel

Fetch and cache live exchange rates with Laravel’s HTTP client, bearer authentication, bounded timeouts, and freshness metadata.

By the Exchangerate team · Updated 20 Sep 2026 · 6 min read

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.

- Use Laravel’s HTTP client with bearer authentication, an explicit timeout, and `throw()` for non-success responses.
- Cache the complete successful response for 60 seconds to stay well inside the Free tier’s 10 requests per minute.
- Keep the key in `config/services.php` through `EXCHANGERATE_API_KEY`, not in application code.
- Use a decimal library for calculations that require exact money arithmetic; PHP floats are binary floating point.

## 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.

```phpappservicesexchangerateservicephp title="php · app/Services/ExchangeRateService.php"
<?php

namespace App\Services;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;

final class ExchangeRateService
{
    public function latest(string $base, array $symbols): array
    {
        sort($symbols);
        $symbolList = implode(',', $symbols);
        $cacheKey = "fx:latest:{$base}:{$symbolList}";

        return Cache::remember($cacheKey, now()->addSeconds(60), function () use (
            $base,
            $symbolList,
        ): array {
            return Http::acceptJson()
                ->withToken(config('services.exchangerate.key'))
                ->timeout(5)
                ->get(
                    "https://api.exchangerate.dev/v1/latest/{$base}",
                    ['symbols' => $symbolList],
                )
                ->throw()
                ->json();
        });
    }
}
```

## Configure the key outside source control

```phpconfigservicesphpandenv title="php · config/services.php and .env"
// config/services.php
'exchangerate' => [
    'key' => env('EXCHANGERATE_API_KEY'),
],

// .env (never commit the real value)
EXCHANGERATE_API_KEY=exr_live_...
```

## Read the rate and its observation fields

Get a key from the [quickstart](https://exchangerate.dev/docs/quickstart), then 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.

**Do not cache errors as rates**

A 401 means the key or header is wrong. A 429 means the application has exceeded a request limit. Let both fail, back off where appropriate, and keep any prior successful observation only with its original update time.

- 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.

## Questions

### Does Laravel need a separate SDK to call exchangerate.dev?

No. Laravel’s built-in HTTP client can send the bearer token, query parameters, timeout, and JSON accept header. The API returns ordinary JSON that can be handled as an array.

### How long should Laravel cache an exchange-rate response?

A 60-second cache works for all pairs updating about every 60s on trading days and prevents duplicate application requests from consuming the upstream limit. Check data_updated_at because daily fallback data can still have an older observation.

### Can PHP float be used for currency conversion?

PHP float is binary floating point and can introduce representation artifacts. Use a decimal or arbitrary-precision library when exact money arithmetic matters, preserve the API rate, and round only the final display amount.

### What limits apply to the free exchangerate.dev key?

The Free tier includes 5,000 calls per month at 10 requests per minute, with no card required. Cache shared reads and avoid one upstream request per page view.

## Keep reading

- [Historical FX rates and time series in one call](https://exchangerate.dev/learn/historical-fx-time-series)
- [Reading source and market_session](https://exchangerate.dev/learn/reading-source-and-session)
- [Indicative vs executable FX rates](https://exchangerate.dev/learn/indicative-vs-executable-fx)
- [Refresh exchange rates in Excel with Power Automate](https://exchangerate.dev/learn/power-automate-exchange-rates)
- [Get exchange rates in Swift with URLSession](https://exchangerate.dev/learn/swift-exchange-rates)
- [Use live exchange rates from Gemini CLI with MCP](https://exchangerate.dev/learn/gemini-cli-exchange-rates)

Source: https://exchangerate.dev/learn/php-laravel-exchange-rates
