Nov, 19, 2024
Jack Isherwood
The cryptocurrency market is fast-paced, and to keep up, you need quick insights on trend shifts. With Guavy’s Recent Trends API, you can access trend change data for various cryptocurrencies within a specific time frame.
In this tutorial, we’ll show you how to connect to the Recent Trends API and build a JavaScript program that fetches, displays, and works with trend data.
The Recent Trends API lets you retrieve trend changes for multiple cryptocurrencies within a specified number of days. For example, you could retrieve trend changes from the last 7 days, giving you actionable insights on market directions.
Ensure you have Node.js installed if you plan to run this program from a local environment. You’ll also need an API key from Guavy for authentication.
Let’s start by making a simple API request to get trend changes from the last 7 days.
Set up your API URL
Guavy’s API endpoint for retrieving recent trend changes looks like this:
Replace <DAYS-AGO>
with the number of days you’re interested in. For example, /recent-trend-change/7
retrieves trends from the last 7 days.
Add Your API Key
You’ll need to include your Bearer token in the request header for authentication.
const url = "https://api.guavy.com/api/v2/market/recent-trend-change/7";
const options = {
method: "GET",
headers: {
Authorization: "Bearer YOUR-API-KEY",
},
};
Fetch the Data
Now, let’s fetch the data using JavaScript’s fetch
function.
fetch(url, options)
.then((response) => response.json())
.then((data) => console.log("Trend Data:", data))
.catch((error) => console.error("Error fetching data:", error));
When executed, this code will print the trend data to the console. The response will look something like this:
{
"Success": [
{
"date": 1727337600000.0,
"trend": "up",
"coin": "SOL",
"trend_started_on": "2024-09-26T08:00:00.000Z",
"price_on_trend_start": 151.74378288210804,
"duration_in_days": 1
}
]
}
To make this data useful, let’s parse the response and display specific trend details, like the cryptocurrency name, trend direction, and start date.
Add this code after the fetch
call to display each trend change in a readable format.
fetch(url, options)
.then((response) => response.json())
.then((data) => {
if (data.Success) {
data.Success.forEach((trend) => {
console.log(`Coin: ${trend.coin}`);
console.log(`Trend: ${trend.trend}`);
console.log(`Started On: ${new Date(trend.trend_started_on)}`);
console.log(`Price at Start: $${trend.price_on_trend_start}`);
console.log(`Duration: ${trend.duration_in_days} days\n`);
});
}
})
.catch((error) => console.error("Error fetching data:", error));
This code extracts each trend and displays key details. Formatting dates with JavaScript’s Date
object makes the information more readable.
With just a few lines of JavaScript, we’ve successfully used the Guavy Recent Trends API to retrieve and display trend data for cryptocurrencies. Now you can leverage this API to keep tabs on the latest market movements, helping you make informed trading decisions.
Happy coding, and good luck navigating the crypto markets with Guavy!