Price Feeds
in Off-Chain Applications

How to Use Real-Time Data in Off-Chain Applications

This guide explains how to use the typescript SDK client (opens in a new tab) to fetch the latest prices and subscribe to real-time price updates in off-chain applications.

Installation

The Pyth SDK can be installed using npm or yarn:

npm

npm install --save @pythnetwork/price-service-client

Yarn

yarn add @pythnetwork/price-service-client

Usage

Typical usage of the Pyth SDK involves creating a PriceServiceConnection instance, by passing the URL of the Hermes (opens in a new tab) service to the constructor. You can then use the getLatestPriceFeeds method to fetch the latest prices of the specified price feeds:

// Get the Stable Hermes service URL from https://docs.pyth.network/price-feeds/api-instances-and-providers/hermes
const connection = new PriceServiceConnection("https://hermes.pyth.network");
 
const priceIds = [
  "0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43", // BTC/USD price id
  "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace", // ETH/USD price id
];
 
// Get the latest values of the price feeds as JSON objects.
const currentPrices = await connection.getLatestPriceFeeds(priceIds);
console.log(currentPrices);

The getLatestPriceFeeds method returns an array of PriceFeed objects. Each PriceFeed object contains the latest price, the Exponential Moving Average (EMA) price, and other metadata:

[
  PriceFeed {
    emaPrice: Price {
      conf: '3450227100',
      expo: -8,
      price: '6610104500000',
      publishTime: 1715870606
    },
    id: 'e62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43',
    metadata: undefined,
    vaa: undefined,
    price: Price {
      conf: '3315255511',
      expo: -8,
      price: '6619626406527',
      publishTime: 1715870606
    }
  },
  PriceFeed {
    emaPrice: Price {
      conf: '266713535',
      expo: -8,
      price: '297876550000',
      publishTime: 1715870606
    },
    id: 'ff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace',
    metadata: undefined,
    vaa: undefined,
    price: Price {
      conf: '296893358',
      expo: -8,
      price: '296499502000',
      publishTime: 1715870606
    }
  }
]

You can also subscribe to real-time price updates over a WebSocket connection using the subscribePriceFeedUpdates method:

// priceIds here is the one declared in the above code snippet.
const priceFeeds = await connection.getLatestPriceFeeds(priceIds);
 
connection.subscribePriceFeedUpdates(priceIds, (priceFeed) => {
  // priceFeed here is the same as returned by getLatestPriceFeeds above.
  console.log(
    `Received an update for ${priceFeed.id}: ${priceFeed.getPriceNoOlderThan(
      60
    )}`
  );
});
 
// When using the subscription, make sure to close the WebSocket upon termination to finish the process gracefully.
setTimeout(() => {
  connection.closeWebSocket();
}, 60000);

Additional Resources

You may find these additional resources helpful.

Examples

This example (opens in a new tab) demonstrates how to use the Pyth SDK to fetch prices.

Hermes API Reference

The Hermes API (opens in a new tab) reference lets you interactively explore the complete API of the Pyth Hermes service.

Price Feed IDs

The Price Feed IDs (opens in a new tab) lists the price feeds available on the Pyth network.

Last updated on