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:


Getting Started

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:

ReadMe