
Exchange rates play an important role for businesses, especially those companies that import raw materials and sell products or services to other countries. For example, if a company buys raw materials from a specific country and its currency depreciates, the company can purchase raw materials at a cheaper rate. However, if the currency appreciates and the exchange rate increases, the company would have to pay higher prices. Hence, businesses must track exchange rates and make decisions accordingly. Additionally, businesses also need to know historical exchange rates to analyze trends.
To date, an easy way to get accurate current and historical foreign exchange rates is to use a reliable exchange rate API using ajax that will help convert any values from one currency to another.
To help you get started with Fixer API, the most advanced exchange rate API, we’ve put together a step-by-step guide that will show you how you can use it to get real-time or historical exchange rates.
Table of Contents
What is Fixer API?
Fixer is a powerful exchange rate API that provides real-time exchange rates for as many as 170 world currencies. The API uses more than 15 exchange rate data sources, including European Central Bank, and updates its currency data every 60 seconds to ensure you’re getting the most accurate exchange rates. Additionally, the API is also capable of providing historical exchange rate data. You can use the API to get the historical data back to 1st January 1999. The Fixer exchange rate API also has a currency conversion endpoint that you can use to convert one currency to another.
The API returns data in lightweight standard JSON format and responds within milliseconds. Additionally, it comes with extensive and easy-to-understand documentation with lots of code examples to help you get started quickly.
Users love the Fixer APIs is its free subscription plan that provides 100 API calls per month, hourly updates, and historical data.
How to Get Latest Exchange Rates with Fixer API?
The Fixer API comes with the latest rate endpoint that you can use to get real-time exchange rate data for any currency. The endpoint can deliver data for all available currencies, or specify a set of currencies. The endpoint returns exchange rate data updated every 60 minutes, 10 minutes, or 60 seconds, depending on your subscription plan. And it also returns a timestamp, which shows the exact time the currencies were collected. The API delivers exchange rates relative to EUR by default.
Below is an example code to get real-time exchange rate data:
The above code will provide the following results:
How to Get Historical Exchange Rates with Fixer?
The Fixer exchange rate API can provide historical exchange rates for most currencies all the way back to 1999. You can also specify a set of output currencies and set your preferred base currency. You need to append a date (format YYYY-MM-DD) to the base URL to get historical exchange rates.
Below is an example code to get historical data:
The above code will provide the following results:
Does Fixer Exchange Rate API Support Currency Conversion?
The API comes with a currency conversion endpoint that you can convert any amount from one currency to another. To convert currencies, you need to use the “convert” endpoint and set your preferred target and base currencies. You can also convert currencies using historical exchange rate data by using the “date parameter” and setting it to your preferred date (format YYYY-MM-DD).
Below is an example code to convert one currency to another:
The code above will return the following results:
Can I Get Information About Currency Fluctuation with Fixer?
The Fixer API also has a fluctuation data endpoint that shows you how your specified currencies fluctuate on a day-to-day basis. You need to use the “fluctuation” endpoint and provide start and end dates. You can get fluctuation data for all available currencies, or specify a set of currencies.
Below is an example code to get fluctuation data:
The above code will provide the following results:
What is Fixer API’s Time-Series Endpoint?
With the “time series” endpoint, you can get daily historical exchange rates between two dates. You can specify the dates with a maximum time period of 365 days. You can get the time-series data for all available currencies, or specify a set of currencies.
Below is an example code to get historical exchange rates between two dates using the time-series endpoint:
The above code will provide the following results:
How to Get Exchange Rates with Fixer Using PHP?
Below is a PHP example code for getting the latest exchange rates with Fixer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<span class="com">// set API Endpoint and API key </span><span class="pln"> $endpoint </span><span class="pun">=</span> <span class="str">'latest'</span><span class="pun">;</span><span class="pln"> $access_key </span><span class="pun">=</span> <span class="str">'API_KEY'</span><span class="pun">;</span> <span class="com">// Initialize CURL:</span><span class="pln"> $ch </span><span class="pun">=</span><span class="pln"> curl_init</span><span class="pun">(</span><span class="str">'https://data.fixer.io/api/'</span><span class="pun">.</span><span class="pln">$endpoint</span><span class="pun">.</span><span class="str">'?access_key='</span><span class="pun">.</span><span class="pln">$access_key</span><span class="pun">.</span><span class="str">''</span><span class="pun">);</span><span class="pln"> curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_RETURNTRANSFER</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span> <span class="com">// Store the data:</span><span class="pln"> $json </span><span class="pun">=</span><span class="pln"> curl_exec</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span><span class="pln"> curl_close</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span> <span class="com">// Decode JSON response:</span><span class="pln"> $exchangeRates </span><span class="pun">=</span><span class="pln"> json_decode</span><span class="pun">(</span><span class="pln">$json</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span> <span class="com">// Access the exchange rate values, e.g. GBP:</span><span class="pln"> echo $exchangeRates</span><span class="pun">[</span><span class="str">'rates'</span><span class="pun">][</span><span class="str">'GBP'</span><span class="pun">];</span> |
Below is how you can convert currencies with Fixer using PHP cURL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<span class="com">// set API Endpoint, access key, required parameters</span><span class="pln"> $endpoint </span><span class="pun">=</span> <span class="str">'convert'</span><span class="pun">;</span><span class="pln"> $access_key </span><span class="pun">=</span> <span class="str">'API_KEY'</span><span class="pun">;</span><span class="pln"> $from </span><span class="pun">=</span> <span class="str">'USD'</span><span class="pun">;</span><span class="pln"> $to </span><span class="pun">=</span> <span class="str">'EUR'</span><span class="pun">;</span><span class="pln"> $amount </span><span class="pun">=</span> <span class="lit">10</span><span class="pun">;</span> <span class="com">// initialize CURL:</span><span class="pln"> $ch </span><span class="pun">=</span><span class="pln"> curl_init</span><span class="pun">(</span><span class="str">'https://data.fixer.io/api/'</span><span class="pun">.</span><span class="pln">$endpoint</span><span class="pun">.</span><span class="str">'?access_key='</span><span class="pun">.</span><span class="pln">$access_key</span><span class="pun">.</span><span class="str">'&from='</span><span class="pun">.</span><span class="pln">$from</span><span class="pun">.</span><span class="str">'&to='</span><span class="pun">.</span><span class="pln">$to</span><span class="pun">.</span><span class="str">'&amount='</span><span class="pun">.</span><span class="pln">$amount</span><span class="pun">.</span><span class="str">''</span><span class="pun">);</span><span class="pln"> curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_RETURNTRANSFER</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span> <span class="com">// get the JSON data:</span><span class="pln"> $json </span><span class="pun">=</span><span class="pln"> curl_exec</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span><span class="pln"> curl_close</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span> <span class="com">// Decode JSON response:</span><span class="pln"> $conversionResult </span><span class="pun">=</span><span class="pln"> json_decode</span><span class="pun">(</span><span class="pln">$json</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span> <span class="com">// access the conversion result</span><span class="pln"> echo $conversionResult</span><span class="pun">[</span><span class="str">'result'</span><span class="pun">];</span> |
Ready to get accurate real-time or historical exchange rates?
Head over to Fixer and choose a subscription plan that suits your needs.
You may also check the other APIs and app backends on our API marketplace for detail.