
This guide will help you fetch token prices via the Prices API.
Whether you're building a DeFi protocol, portfolio tracker, or analytics tool, the Prices API provides simple endpoints for current and historical prices.
The Prices API includes the following REST endpoints:
| Endpoint | How It Works | When to Use |
|---|---|---|
| Token Prices By Symbol | Combines prices from centralized (CEX) and decentralized (DEX) exchanges for each symbol. | Use this when you need a current price overview of a token across all supported chains and exchanges. |
| Token Prices By Address | Combines prices from DEXes for each contract address and network. | Use this when you need the current price of a specific token on a particular blockchain network. |
| Historical Prices By Symbol Or Address | Fetches past price data by symbol or address. | Use this when you require historical price data for creating charts or performing analysis. |
Modern web development uses fetch for HTTP requests. The Prices API can be easily accessed using native fetch or any HTTP client library.
The fetch API is available in all modern browsers and Node.js (18+). For older Node.js versions, you can install node-fetch:
# No installation needed - fetch is built-inCreate a new JavaScript file (e.g., prices-fetch-script.js) and add one of the following snippets depending on which endpoint you want to call.
// prices-fetch-script.js
// Replace with your Alchemy API key:
const apiKey = "demo";
// Define the symbols you want to fetch prices for.
const symbols = ["ETH", "BTC", "USDT"];
async function getTokenPricesBySymbol() {
try {
const symbolsParam = symbols.join(',');
const response = await fetch(`https://api.g.alchemy.com/prices/v1/tokens/by-symbol?symbols=${symbolsParam}`, {
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
const data = await response.json();
console.log("Token Prices By Symbol:");
console.log(JSON.stringify(data, null, 2));
} catch (error) {
console.error("Error:", error);
}
}
getTokenPricesBySymbol();Execute the script from your command line:
node prices-fetch-script.jsExpected Output:
{
"data": [
{
"symbol": "ETH",
"prices": [
{
"currency": "USD",
"value": "3000.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null
},
{
"symbol": "BTC",
"prices": [
{
"currency": "USD",
"value": "45000.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null
},
{
"symbol": "USDT",
"prices": [
{
"currency": "USD",
"value": "1.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null
}
]
}node-fetch is a lightweight option for making HTTP requests with Javascript.
Install the node-fetch package using npm or yarn:
npm install node-fetchCreate a new JavaScript file (e.g., prices-fetch-script.js) and add the following code.
// prices-fetch-script.js
// Replace with your Alchemy API key:
const apiKey = "YOUR_ALCHEMY_API_KEY";
const fetchURL = `https://api.g.alchemy.com/prices/v1/${apiKey}/tokens/by-symbol`;
// Define the symbols you want to fetch prices for.
const symbols = ["ETH", "BTC", "USDT"];
const params = new URLSearchParams();
symbols.forEach(symbol => params.append('symbols', symbol));
const urlWithParams = `${fetchURL}?${params.toString()}`;
const requestOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
fetch(urlWithParams, requestOptions)
.then(response => response.json())
.then(data => {
console.log("Token Prices By Symbol:");
console.log(JSON.stringify(data, null, 2));
})
.catch(error => console.error('Error:', error));Execute the script from your command line:
node prices-fetch-script.jsExpected Output:
{
"data": [
{
"symbol": "ETH",
"prices": [
{
"currency": "USD",
"value": "3000.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null},
{
"symbol": "BTC",
"prices": [
{
"currency": "USD",
"value": "45000.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null},
{
"symbol": "USDT",
"prices": [
{
"currency": "USD",
"value": "1.00",
"lastUpdatedAt": "2024-04-27T12:34:56Z"
}
],
"error": null}
]
}