How To Use The Currency Conversion API with PHP, Python, and Other Popular Programming Languages
06 February 2023
The currency conversion API is a crucial tool for developers. It allows you to add real-time Forex data retrieval features to your apps and websites, thereby enhancing the user experience. This tutorial provides a range of examples demonstrating how to use our currency rate API with different programming languages. Let's begin by exploring the service and the API.
What is the Currency Conversion API Service?
Currency exchange API provides instant, up-to-date exchange rates with a simple request. Integrate them into your app or website for a seamless user experience. Developers can supercharge their apps with real-time currency conversion!
Our convert currency API offers developers a hassle-free way to integrate live exchange rates into their projects. With a quick HTTP request, you can get the data you need as a response, empowering you to keep your financial solutions updated ahead of time.
Our real time currency converter API service aims to assist developers in keeping users updated about the latest conversion rates. Our exchange rates API supports numerous currencies and thus helps developers enrich their apps and websites with excellent data retrieval features, including historical rates.
The API for currency exchange helps in many other ways. Developers can build:
1) Tables with periodical currency conversion rates,
2) Charts, and
3) Graphs to ease users for in-depth forex analysis.
We provide aggregated and curated forex data. It can be an excellent reference feed to make informed trading decisions.
Also Read: How Our Currency Conversion API Makes Your Lives Easy
What is a Free Currency Converter API?
Currency exchange APIs offer an elegant solution for enriching your applications with dynamic currency conversion features. These APIs function as powerful intermediaries, instantaneously retrieving real-time exchange rates upon request.
Currency Conversion Rate API Demystified
Our API for currency conversion free provides real-time currency data, helping developers add data retrieval features. Integration is straightforward, requiring developers to specify the base and target currencies.
The API then acts as a conduit, transmitting a request and delivering a response containing the most up-to-date conversion rate data. The API response will contain the conversion rate data. You can parse this data and display the converted amount within your app's interface.
This seamless process empowers developers to integrate valuable functionality, enhancing user experience for the users navigating international transactions.
1) You can set up websites and apps to retrieve the real-time currency conversion value. You can also get the value of a desired currency in the local currency.
2) You need to deploy geological detection of dynamic user displays in web browsers. For historical chart references, you can get the foreign exchange rate for specific times.
Also, Read our exclusive tutorial to create a real-time currency converter in Python:
Build Your First Currency Converter in Python
How to Use the API Currency Exchange Through Various Popular Programming Languages?
Our free currency exchange rate API (with a monthly limit of 1000 API requests) provides access to up-to-date, accurate, and reliable conversion rates for a wide range of currency pairs. This practical solution has a user-friendly setup process, requiring only a free API key for activation. Obtaining your key is simple—just sign up for a free API key.
To empower developers of all backgrounds, we offer a comprehensive array of code examples encompassing various popular programming languages. These examples are conveniently catalogued within our Data Documentation Page, readily accessible via the vertical menu positioned on the left-hand side, providing you with the support you need for seamless integration.
This streamlined approach ensures seamless integration of real-time currency conversion functionality into your application, enhancing the user experience for those engaged in international transactions.
Currency Converter API Integration Examples
Let us glance at the example snippets, like currency converter API PHP and currency API Python. These examples, readily available for your reference, help programmers add data retrieval features to their websites and applications:
PHP (Curl)
The PHP snippet helps developers looking to build
1) CMS scripts,
2) Custom display pages, or
3) Insert tables of currency exchange rates
Also, publishers can use the PHP code. It helps add support to the currency exchange API for web page platforms. (like WordPress, Drupal, and Jumla)
Below is an example showing converting exchange rates data using PHP (CURL):
<?php $curl = curl_init(); curl_setopt_array( $curl, array( CURLOPT_PORT => "443", CURLOPT_URL => "https://marketdata.tradermade.com/api/v1/convert?api_key=API_KEY&from=EUR&to=GBP&amount=1000", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false )); //for debug only! curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { echo $response; } ?>
Python
The Python snippets help developers working on Django, flask, and other frameworks. They can add API scripts to their code.
Below is an example to get live exchange rates data using Python:
See our tutorial on Fetch Forex API with Python and Pandas.
import requests url = "https://marketdata.tradermade.com/api/v1/convert" querystring = {"from":"EUR","to":"GBP","api_key":"API_KEY","amount":1000} response = requests.get(url, params=querystring) print(response.json())
R
Below is an example to get live exchange rates data using R:
See our detailed tutorial: Learn how to get live and historical forex data using R language.
library(httr) library(jsonlite) req <- "https://marketdata.tradermade.com/api/v1/convert?api_key=API_KEY&from=EUR&to=GBP&amount=1000" data_raw <- GET(url = req) data_text = content(data_raw, "text", encoding = "UTF-8") data_json = fromJSON(data_text, flatten=TRUE) dataframe = as.data.frame(data_json) dataframe dataframe
Go
Below is an example to get live exchange rates data using Golang:
For further explanation, see our tutorials: Get live forex data and parse REST JSON API with GoLang.
package main import ( "encoding/json" "fmt" "io/ioutil" "log" "net/http" ) type data struct { Endpoint string `'json:'endpoint'` Quotes []map[string]interface{} `'json:'quotes'` Requested_time string `'json:'requested_time'` Timestamp int32 `'json:'timestamp'` } func main(){ api_key := "API_KEY" from := "EUR" to := "GBP" amount := "10000" url := "https://marketdata.tradermade.com/api/v1/convert?from=" + from + "&to=" + to + "&amount=" + amount + "&api_key=" + api_key fmt.Println(string(url)) resp, getErr := http.Get(url) if getErr != nil { log.Fatal(getErr) } body, readErr := ioutil.ReadAll(resp.Body) if readErr != nil { log.Fatal(readErr) } fmt.Println(string(body)) data_obj := data{} jsonErr := json.Unmarshal(body, &data_obj) if jsonErr != nil { log.Fatal(jsonErr) } fmt.Println("endpoint", data_obj.Endpoint, "requested time", data_obj.Requested_time, "timestamp", data_obj.Timestamp) for key, value := range data_obj.Quotes { fmt.Println(key) fmt.Println(value) } }
C#
Below is an example to get live exchange rates data using C#:
For further explanation, see our tutorials: Get live forex data and parse REST JSON API with C#.
using System; using System.Net.Http; using Newtonsoft.Json; namespace crest { class Program { static async System.Threading.Tasks.Task Main(string[] args) { HttpClient client = new HttpClient(); var requestString="https://marketdata.tradermade.com/api/v1/convert?from=EUR&to=USD&amount=10&api_key=YOUR_API_KEY"; Console.WriteLine(" Request String: " + requestString); HttpResponseMessage response = await client.GetAsync(requestString); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync(); var result = JsonConvert.DeserializeObject<Quotes>(responseBody); Console.WriteLine("Results " + responseBody); } public class Quotes { public string base_currency { get; set; } public string endpoint { get; set; } public string quote { get; set; } public string quote_currency { get; set; } public string requested_time { get; set; } public string timestamp { get; set; } public string total { get; set; } } } }
JavaScript
Using axios
Below is an example to get historical exchange rates data using JavaScript, Axios, and NodeJS:
For further explanation, see our tutorials: Get live forex data and parse REST JSON API with JavaScript, Axios, and NodeJS.
const axios = require('axios'); axios.get('https://marketdata.tradermade.com/api/v1/convert?from=EUR&to=USD&amount=10&api_key=YOUE_API_KEY') .then(response => { console.log(response.data); console.log(response.data.quote); }) .catch(error => { console.log(error); });
JavaScript (fetch)
Below is an example to get historical exchange rates data using fetch:
var requestOptions = { method : 'GET', redirect : 'follow' }; fetch( ''https://marketdata.tradermade.com/api/v1/convert?from=EUR&to=USD&amount=10&api_key=YOUE_API_KEY'', requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log( 'error', error));
JavaScript (jQuery)
Below is an example to get historical exchange rates data using jQuery:
var settings = { "url": "'https://marketdata.tradermade.com/api/v1/convert?from=EUR&to=USD&amount=10&api_key=YOUE_API_KEY'", "method": "GET", "timeout": 0, }; $.ajax(settings).done(function (response) { console.log(response); });
The Final Thoughts
This tutorial covers various examples in popular programming languages to help you access and use our market data. Our free currency conversion API provides free currency conversion for you to start with (up to 1000 requests a month). We hope the examples covered here will help developers proficient in many programming languages extract the desired results conveniently.
If you have any technical queries or suggestions, please contact our market data experts through live chat or email.