Query Price Data

Overview

The /price endpoint lists token prices in fiat currencies. It is particularly useful when showing a lot of pricing information to the user as it allows querying of prices for multiple tokens at once.

📘

Note:

Unknown token addresses will be ignored. Make sure you check if the price for a give token is returned to avoid null-pointer exceptions in your code.

Code Example

https://github.com/Blockdaemon/defi-api-examples/blob/main/src/main/scripts/get-prices.ts contains example code for using the /price endpoint programmatically.

Please refer to Set Up the Development Environment for the basic setup of the project. To run the script use the command: npx ts-node src/main/scripts/get-prices.ts

Let’s go through the relevant code sections step by step:

  1. We initialise the object PriceApi with our configuration object (see: Set Up the Development Environment) which includes the API key. The TokensApi object can then be used to interact programmatically with the /tokens endpoint.

    const api = new PriceApi(apiConfig);
    
  2. We configure the GetPriceRequest object which allows us to configure which tokens we want to query prices for. In this example we query the prices of Ether and USDC on Ethereum Mainnet in USD. For a list of all possible parameters, please refer to the API specification at Price.

    const request: PriceRequest = {
      chainID: "1",
      tokens: ["0x0000000000000000000000000000000000000000", "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"],
      currency: "USD" as CurrencyCode,
    };
    
    const priceParameters: GetPriceRequest = {
      priceRequest: request,
    };
    
  3. Finally we query the API using the PriceApi object together with the the parameter object and log the returned data.

    const prices = await api.getPrice(priceParameters);
    log.info("Got prices");
    log.info(JSON.stringify(prices, null, 2));
    

👋 Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.