Prices API Quickstart

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:

EndpointHow It WorksWhen to Use
Token Prices By SymbolCombines 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 AddressCombines 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 AddressFetches past price data by symbol or address.Use this when you require historical price data for creating charts or performing analysis.

Getting Started

Via HTTP Requests

Modern web development uses fetch for HTTP requests. The Prices API can be easily accessed using native fetch or any HTTP client library.

No Additional Installation Required

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-in

Usage

Create 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.

1// prices-fetch-script.js
2
3// Replace with your Alchemy API key:
4const apiKey = "demo";
5
6// Define the symbols you want to fetch prices for.
7const symbols = ["ETH", "BTC", "USDT"];
8
9async function getTokenPricesBySymbol() {
10 try {
11 const symbolsParam = symbols.join(',');
12 const response = await fetch(`https://api.g.alchemy.com/prices/v1/tokens/by-symbol?symbols=${symbolsParam}`, {
13 headers: {
14 'Authorization': `Bearer ${apiKey}`
15 }
16 });
17
18 const data = await response.json();
19 console.log("Token Prices By Symbol:");
20 console.log(JSON.stringify(data, null, 2));
21 } catch (error) {
22 console.error("Error:", error);
23 }
24}
25
26getTokenPricesBySymbol();

Running the Script

Execute the script from your command line:

bash
$node prices-fetch-script.js

Expected Output:

json
1{
2 "data": [
3 {
4 "symbol": "ETH",
5 "prices": [
6 {
7 "currency": "USD",
8 "value": "3000.00",
9 "lastUpdatedAt": "2024-04-27T12:34:56Z"
10 }
11 ],
12 "error": null
13 },
14 {
15 "symbol": "BTC",
16 "prices": [
17 {
18 "currency": "USD",
19 "value": "45000.00",
20 "lastUpdatedAt": "2024-04-27T12:34:56Z"
21 }
22 ],
23 "error": null
24 },
25 {
26 "symbol": "USDT",
27 "prices": [
28 {
29 "currency": "USD",
30 "value": "1.00",
31 "lastUpdatedAt": "2024-04-27T12:34:56Z"
32 }
33 ],
34 "error": null
35 }
36 ]
37}

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

Usage

Create a new JavaScript file (e.g., prices-fetch-script.js) and add the following code.

1// prices-fetch-script.js
2
3
4// Replace with your Alchemy API key:
5const apiKey = "YOUR_ALCHEMY_API_KEY";
6const fetchURL = `https://api.g.alchemy.com/prices/v1/${apiKey}/tokens/by-symbol`;
7
8// Define the symbols you want to fetch prices for.
9const symbols = ["ETH", "BTC", "USDT"];
10
11const params = new URLSearchParams();
12symbols.forEach(symbol => params.append('symbols', symbol));
13
14const urlWithParams = `${fetchURL}?${params.toString()}`;
15
16const requestOptions = {
17 method: 'GET',
18 headers: {
19 'Content-Type': 'application/json',
20 },
21};
22
23fetch(urlWithParams, requestOptions)
24 .then(response => response.json())
25 .then(data => {
26 console.log("Token Prices By Symbol:");
27 console.log(JSON.stringify(data, null, 2));
28 })
29 .catch(error => console.error('Error:', error));

Running the Script

Execute the script from your command line:

bash
$node prices-fetch-script.js

Expected Output:

json
1{
2 "data": [
3 {
4 "symbol": "ETH",
5 "prices": [
6 {
7 "currency": "USD",
8 "value": "3000.00",
9 "lastUpdatedAt": "2024-04-27T12:34:56Z"
10 }
11 ],
12 "error": null},
13 {
14 "symbol": "BTC",
15 "prices": [
16 {
17 "currency": "USD",
18 "value": "45000.00",
19 "lastUpdatedAt": "2024-04-27T12:34:56Z"
20 }
21 ],
22 "error": null},
23 {
24 "symbol": "USDT",
25 "prices": [
26 {
27 "currency": "USD",
28 "value": "1.00",
29 "lastUpdatedAt": "2024-04-27T12:34:56Z"
30 }
31 ],
32 "error": null}
33 ]
34}