A new developer's guide to fetching current and historical token prices via the Prices API.
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.
Endpoints
The Prices API includes the following REST endpoints:
- Token Prices By Symbol: Retrieves the latest price data for multiple tokens using their symbols.
- Token Prices By Address: Retrieves the latest price data for multiple tokens using their contract addresses and network identifiers.
- Historical Prices By Symbol Or Address: Retrieves historical price data for a single token within a specified time range.
Getting Started
Via Alchemy SDK
The Alchemy SDK provides a convenient and fully featured way to interact with Alchemy’s APIs, including the Prices API.
Installation
Install the alchemy-sdk
package using npm
or yarn
:
npm install alchemy-sdk
yarn add alchemy-sdk
Usage
Create a new JavaScript file (e.g., prices-alchemy-sdk-script.js
) and add one of the following snippets depending on which endpoint you want to call.
// prices-alchemy-sdk-script.js
import { Alchemy } from "alchemy-sdk";
// Replace with your Alchemy API key:
const apiKey = "demo";
const alchemy = new Alchemy({ apiKey });
// Define the symbols you want to fetch prices for.
const symbols = ["ETH", "BTC", "USDT"];
alchemy.prices.getTokenPriceBySymbol(symbols)
.then(data => {
console.log("Token Prices By Symbol:");
console.log(JSON.stringify(data, null, 2));
})
.catch(error => console.error("Error:", error));
// prices-alchemy-sdk-script.js
import { Alchemy } from "alchemy-sdk";
// Replace with your Alchemy API key:
const apiKey = "demo";
const alchemy = new Alchemy({ apiKey });
// Define the network and contract addresses you want to fetch prices for.
const addresses = [
{
network: "eth-mainnet",
address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" // USDC
},
{
network: "eth-mainnet",
address: "0xdac17f958d2ee523a2206206994597c13d831ec7" // USDT
}
];
alchemy.prices.getTokenPriceByAddress(addresses)
.then(data => {
console.log("Token Prices By Address:");
console.log(JSON.stringify(data, null, 2));
})
.catch(error => console.error("Error:", error));
Running the Script
Execute the script from your command line:
node prices-alchemy-sdk-script.js
Expected 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
}
]
}
Via Node Fetch
node-fetch
is a lightweight option for making HTTP requests with Javascript.
Installation
Install the node-fetch
package using npm
or yarn
:
npm install node-fetch
yarn add node-fetch
Usage
Create a new JavaScript file (e.g., prices-fetch-script.js
) and add the following code.
// prices-fetch-script.js
import fetch from 'node-fetch';
// 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));
// prices-fetch-script.js
import fetch from 'node-fetch';
// Replace with your Alchemy API key:
const apiKey = "YOUR_ALCHEMY_API_KEY";
const fetchURL = `https://api.g.alchemy.com/prices/v1/${apiKey}/tokens/by-address`;
// Define the network and contract addresses you want to fetch prices for.
const requestBody = {
addresses: [
{
network: "eth-mainnet",
address: "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48" // USDC
},
{
network: "eth-mainnet",
address: "0xdac17f958d2ee523a2206206994597c13d831ec7" // USDT
}
]
};
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify(requestBody),
};
fetch(fetchURL, requestOptions)
.then(response => response.json())
.then(data => {
console.log("Token Prices By Address:");
console.log(JSON.stringify(data, null, 2));
})
.catch(error => console.error('Error:', error));
Running the Script
Execute the script from your command line:
node prices-fetch-script.js
Expected 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}
]
}
API Reference
For more details on the available Prices API methods, check out the docs: