Skip to content

Latest commit

 

History

History
198 lines (157 loc) · 6.25 KB

File metadata and controls

198 lines (157 loc) · 6.25 KB
title Prices API Quickstart
description A new developer's guide to fetching current and historical token prices via the Prices API.
subtitle A new developer's guide to fetching current and historical token prices via the Prices API.
slug reference/prices-api-quickstart

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:

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.

Supported Networks

For Token Prices By Address and Historical Prices endpoints, pass the network slug in the network field of your request body.

Mainnets:

Network Slug
apechain-mainnet arb-mainnet arbnova-mainnet
avax-mainnet base-mainnet blast-mainnet
bnb-mainnet eth-mainnet fantom-mainnet
gnosis-mainnet linea-mainnet metis-mainnet
opbnb-mainnet opt-mainnet polygon-mainnet
polygonzkevm-mainnet scroll-mainnet solana-mainnet
worldchain-mainnet zetachain-mainnet zksync-mainnet
Check the [Chains](https://dashboard.alchemy.com/chains) page for the most up-to-date product and chain support!

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:

```shell Node.js 18+ (Built-in) # No installation needed - fetch is built-in ```
npm install node-fetch

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.

```js By Symbol // 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();


```js By Address
// prices-fetch-script.js

// Replace with your Alchemy API key:
const apiKey = "demo";

// 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
  }
];

async function getTokenPricesByAddress() {
  try {
    const response = await fetch('https://api.g.alchemy.com/prices/v1/tokens/by-address', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`
      },
      body: JSON.stringify({ addresses })
    });

    const data = await response.json();
    console.log("Token Prices By Address:");
    console.log(JSON.stringify(data, null, 2));
  } catch (error) {
    console.error("Error:", error);
  }
}

getTokenPricesByAddress();

Running the Script

Execute the script from your command line:

```bash bash node prices-fetch-script.js ```

Expected Output:

```json json { "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 } ] } ```