### Currencylayer API JSONP Callback Example
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This example demonstrates how to enable JSONP callbacks for the Currencylayer API. By appending `callback=CALLBACK_FUNCTION` to the API endpoint, the response is wrapped within the specified JavaScript function, allowing for cross-domain data retrieval.
```javascript
https://api.currencylayer.com/live
? access_key = YOUR_ACCESS_KEY
& callback = CALLBACK_FUNCTION
```
```javascript
CALLBACK_FUNCTION ({
"success": true,
"terms": "https://currencylayer.com/terms",
"privacy": "https://currencylayer.com/privacy",
"timestamp": 1432480209,
"source": "USD",
"quotes": {
"USDAED": 3.67315,
"USDAFN": 60.790001,
"USDALL": 126.194504,
"USDAMD": 477.359985,
"USDANG": 1.790403,
[...]
}
})
```
--------------------------------
### Currencylayer API JSON Formatting Example
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This example shows how to enable pretty-printing for Currencylayer API responses by appending `format=1` to the API URL. This feature is useful for debugging as it makes the JSON response more human-readable, though it may increase response size and is not guaranteed to be valid JSON.
```url
https://api.currencylayer.com/live
? access_key = YOUR_ACCESS_KEY
& format = 1
```
--------------------------------
### Currencylayer API Example Response
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This is an example of a standard API response from the Currencylayer API. It includes success status, terms, privacy policy, timestamp, source currency, and exchange rate quotes.
```json
{
"success": true,
"terms": "https://currencylayer.com/terms",
"privacy": "https://currencylayer.com/privacy",
"timestamp": 1430401802,
"source": "USD",
"quotes": {
"USDAED": 3.672982,
"USDAFN": 57.8936,
"USDALL": 126.1652,
"USDAMD": 475.306,
"USDANG": 1.78952,
"USDAOA": 109.216875,
"USDARS": 8.901966,
"USDAUD": 1.269072,
"USDAWG": 1.792375,
"USDAZN": 1.04945,
"USDBAM": 1.757305,
[...]
}
}
```
--------------------------------
### Request List of Supported Currencies
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This example demonstrates how to request a list of all supported currencies from the Currencylayer API. Append your access key to the list endpoint.
```bash
https://api.currencylayer.com/list
? access_key = YOUR_ACCESS_KEY
```
--------------------------------
### GET /live
Source: https://docs.apilayer.com/currencylayer/docs/api-documentation
Retrieves the most recent exchange rate data for all available currencies.
```APIDOC
## GET /live
### Description
Fetches real-time exchange rates for 168 world currencies.
### Method
GET
### Endpoint
/live
### Parameters
#### Query Parameters
- **access_key** (string) - Required - Your API access key.
- **source** (string) - Optional - The source currency code (default: USD).
- **currencies** (string) - Optional - Comma-separated list of currency codes to limit the output.
### Request Example
GET /live?access_key=YOUR_KEY¤cies=EUR,GBP,JPY
### Response
#### Success Response (200)
- **quotes** (object) - Map of currency pairs and their exchange rates.
#### Response Example
{
"success": true,
"quotes": {
"USDEUR": 0.92,
"USDGBP": 0.78
}
}
```
--------------------------------
### Authenticate with Currencylayer API
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This example shows how to append your API access key to the live endpoint URL for authentication. Replace 'YOUR_ACCESS_KEY' with your actual key.
```bash
https://api.currencylayer.com/live
? access_key = YOUR_ACCESS_KEY
```
--------------------------------
### GET /live
Source: https://docs.apilayer.com/currencylayer/docs/currencylayer-api-v-1-0-0
Retrieves the most recent exchange rate data for all available currencies.
```APIDOC
## GET /live
### Description
Returns the latest exchange rates for all supported currencies relative to a base currency (default USD).
### Method
GET
### Endpoint
https://api.currencylayer.com/live
### Parameters
#### Query Parameters
- **access_key** (string) - Required - Your API access key.
- **source** (string) - Optional - The currency code to use as the source (e.g., EUR).
- **currencies** (string) - Optional - Comma-separated list of currency codes to limit the output.
### Request Example
GET https://api.currencylayer.com/live?access_key=YOUR_KEY
### Response
#### Success Response (200)
- **success** (boolean) - Indicates if the request was successful.
- **quotes** (object) - Key-value pairs of currency pairs and their rates.
#### Response Example
{
"success": true,
"quotes": {
"USDAED": 3.6725,
"USDAFN": 88.412
}
}
```
--------------------------------
### Secure Connection with HTTPS
Source: https://docs.apilayer.com/currencylayer/docs/getting-started
This example shows how to establish a secure HTTPS connection to the Currencylayer API by adding an 's' to the HTTP protocol. This ensures data is transmitted securely.
```bash
https://api.currencylayer.com/live?access_key=YOUR_ACCESS_KEY
```
--------------------------------
### GET /convert
Source: https://docs.apilayer.com/currencylayer/docs/api-documentation
Converts a specific amount from one currency to another.
```APIDOC
## GET /convert
### Description
Performs currency conversion between two specified currencies.
### Method
GET
### Endpoint
/convert
### Parameters
#### Query Parameters
- **access_key** (string) - Required - Your API access key.
- **from** (string) - Required - The currency code to convert from.
- **to** (string) - Required - The currency code to convert to.
- **amount** (number) - Required - The amount to be converted.
- **date** (string) - Optional - Specific date for historical conversion (YYYY-MM-DD).
### Request Example
GET /convert?access_key=YOUR_KEY&from=USD&to=EUR&amount=100
### Response
#### Success Response (200)
- **result** (number) - The converted amount.
#### Response Example
{
"success": true,
"result": 92.00
}
```
--------------------------------
### GET /list/html/table
Source: https://docs.apilayer.com/currencylayer/docs/downloads
Retrieves a list of all supported currencies formatted as a standard HTML table.
```APIDOC
## GET /list/html/table
### Description
Returns a clean HTML
structure containing currency codes and their corresponding names.
### Method
GET
### Endpoint
/list/html/table
### Response
#### Success Response (200)
- **body** (string) - HTML table element containing currency data.
#### Response Example
Code
Name
AED
United Arab Emirates Dirham
```
--------------------------------
### Retrieve Live Exchange Rates in Java
Source: https://docs.apilayer.com/currencylayer/docs/code-examples
This Java code demonstrates how to use the Apache HttpClient library to send a GET request to the Currencylayer live endpoint. It parses the JSON response to extract and print the timestamp and specific currency conversion rates.
```java
package org.json.poc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
public class LiveResponseDemo{
public static final String ACCESS_KEY = "YOUR_ACCESS_KEY";
public static final String BASE_URL = "http://api.currencylayer.com/";
public static final String ENDPOINT = "live";
static CloseableHttpClient httpClient = HttpClients.createDefault();
public static void sendLiveRequest(){
HttpGet get = new HttpGet(BASE_URL + ENDPOINT + "?access_key=" + ACCESS_KEY);
try {
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
JSONObject exchangeRates = new JSONObject(EntityUtils.toString(entity));
System.out.println("Live Currency Exchange Rates");
Date timeStampDate = new Date((long)(exchangeRates.getLong("timestamp")*1000));
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
String formattedDate = dateFormat.format(timeStampDate);
System.out.println("1 " + exchangeRates.getString("source") + " in GBP : " + exchangeRates.getJSONObject("quotes").getDouble("USDGBP") + " (Date: " + formattedDate + ")");
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException{
sendLiveRequest();
httpClient.close();
}
}
```
--------------------------------
### GET /convert
Source: https://docs.apilayer.com/currencylayer/docs/currencylayer-api-v-1-0-0
Performs a currency conversion between two specific currencies based on the latest rates.
```APIDOC
## GET /convert
### Description
Converts a specific amount from one currency to another.
### Method
GET
### Endpoint
https://api.currencylayer.com/convert
### Parameters
#### Query Parameters
- **access_key** (string) - Required - Your API access key.
- **from** (string) - Required - The currency code to convert from.
- **to** (string) - Required - The currency code to convert to.
- **amount** (number) - Required - The amount to be converted.
### Request Example
GET https://api.currencylayer.com/convert?access_key=YOUR_KEY&from=USD&to=EUR&amount=100
### Response
#### Success Response (200)
- **result** (number) - The converted amount.
#### Response Example
{
"success": true,
"result": 92.45
}
```
--------------------------------
### GET /list/html/select
Source: https://docs.apilayer.com/currencylayer/docs/downloads
Retrieves a list of all supported currencies wrapped in HTML select-option tags for easy integration into web forms.
```APIDOC
## GET /list/html/select
### Description
Returns a pre-formatted HTML string containing