Sample Code PHP (GET Request)

Forex Latest Prices: Find below a simple PHP example for getting exchange rate data via the API's latest endpoint.

$auth_key = "AUTH_KEY"; // Your API auth key, Signup to get API KEY
$symbol = "EUR/USD"; // FX Pairs

// Enter your API URL below
$api_url = "https://elklayer.com/api/v1/latest?symbol=".$symbol."&auth_key=".$auth_key;

// Initialize CURL:
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch); // Store the data
curl_close($ch);

$response = json_decode($json, true); // convert JSON into Array
foreach ($response['response'] as $key => $value) {
  echo "<br>Open Price Of ".$value['symbol']." is ".$value['price']; // Open Price Of EUR/USD is 1.1266
}

Sample Code PHP (POST Request)

$auth_key = "AUTH_KEY"; // Your API auth key, Signup to get API KEY
$symbol = "EUR/USD"; // FX Pairs

// Enter your API URL below
$api_url = "https://elklayer.com/api/v1/latest";
$post = "symbol=".$symbol."&auth_key=".$auth_key;

// Initialize CURL:
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$json = curl_exec($ch); // Store the data
curl_close($ch);

$response = json_decode($json, true); // convert JSON into Array
foreach ($response['response'] as $key => $value) {
  echo "<br>Open Price Of ".$value['symbol']." is ".$value['price']; // Open Price Of EUR/USD is 1.1266
}

Sample Code jQuery.ajax

Find below a simple jQuery.ajax example of getting exchange rate data via the API's latest endpoint.

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
auth_key = "AUTH_KEY"; // Your API auth key, Signup to get API KEY
id = '1,2,3,4'; // Forex ids

$.ajax({
    url:
"https://elklayer.com/api/v1/latest?id=" + id + "&auth_key=" + auth_key,
    type:
"GET", // GET or POST
    dataType:
"json",
    success: function(data) {
        console.log("Price of "+data.response[0].symbol + " is "+data.response[0].price); // Price of EUR/USD is 1.1064;
    }
});

</script>

Errors

There are multiple errors that help you to understand issue with your API response.

Code Details
200 Successfully
501 You have not supplied a valid API auth key.
502 Your account is not active, Please check your email to activate it.
503 Your account is suspend for some resons, Please contact us, to activate it.
504 Sorry, auth_key parameter missing. Or add your IP in your dashboard->profile
505 Sorry, You are not allow to access this API endpoint. Please upgrade your plan
111 Sorry you are using demo API key, that has limited id access, To get unlimited access please signup.
112 No data or an invalid value has been specified. Data not found.
113 Sorry, Something wrong, or an invalid value. Please try again or check your required parameters.
211 Your monthly API request limit exceed, Please upgrade your account, If you think this message is error, please contact our support.
212 Your account is expire, Please update your payments, If you think this message is error, please contact our support.
213 Free user has per mintue limit also per month, you can exceed your per minute limit. Please try again after 1 minute.
112 No data or an invalid value has been specified. Data not found.