-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPricePerfomance.js
56 lines (45 loc) · 1.45 KB
/
getPricePerfomance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function GETPRICEPERFORMANCE(symbol, name) {
// Ensure the symbol and name parameters are provided
if (!symbol || !name) {
return 'Error: Coin symbol and name are required';
}
var url = "https://pro-api.coinmarketcap.com/v2/cryptocurrency/price-performance-stats/latest";
var apiKey = 'APIKEY'; // Replace with your API key
var headers = {
"X-CMC_PRO_API_KEY": apiKey,
"Accept": "application/json",
"muteHttpExceptions": true
};
// Construct query parameters
var params = {
symbol: symbol
};
var queryString = Object.keys(params).map(key => key + '=' + encodeURIComponent(params[key])).join('&');
var fullUrl = url + '?' + queryString;
var options = {
'headers': headers
};
try {
var response = UrlFetchApp.fetch(fullUrl, options);
var json = JSON.parse(response.getContentText());
// Check if the response contains the data
if (json.status.error_code !== 0) {
return 'Error: ' + json.status.error_message;
}
// Find the specific coin data by name
var matchingElement = null;
for (var key in json.data) {
if (json.data[key].name === name) {
matchingElement = json.data[key];
break;
}
}
if (!matchingElement) {
return 'Error: No coin data found matching the provided name';
}
var pricePerformance = matchingElement.quote.USD.price_performance;
return pricePerformance;
} catch (e) {
return 'Error: ' + e.message;
}
}