Lazer
Subscribe to Price Updates

How to Subscribe to Price Updates from Pyth Lazer

This guide explains how to subscribe to price updates from Pyth Lazer. This guide will also explain various properties and configuration options to customize the price updates.

Subscribing to price updates is a three-step process:

  1. Acquire an access token.
  2. Configure subscription parameters.
  3. Subscribe to the price updates via websocket API (opens in a new tab).

The websocket server is available at wss://pyth-lazer.dourolabs.app/v1/stream for production and wss://pyth-lazer-staging.dourolabs.app/v1/stream for staging.

1. Acquire an access token

Please fill out this form (opens in a new tab) to contact the Pyth team and get the access token.

Use the access token to authenticate the websocket connection by passing it as an Authorization header with the value Bearer {token}.

2. Configure subscription parameters

Lazer supports several request/subscription parameters to customize the received price updates. These parameters are configured by sending a subscription message to the webservice. A sample request (using the Lazer SDK client -- see step 3) is shown below:

client.send({
  type: "subscribe",
  subscriptionId: 1,
  priceFeedIds: [1, 2],
  properties: ["price"],
  chains: ["solana"],
  channel: "fixed_rate@200ms",
});

The most significant parameters are:

  • subscriptionId is an arbitrary numeric identifier for a subscription. It will be returned back in response by the server. It does not affect the signed payload.
  • priceFeedIds is the list of price feeds to receive updates for. Data for all price feeds will be present in the signed price updates generated. Refer to the Price Feed IDs list for the supported price feeds.
  • properties is the list of properties to retrieve, such as price, bestBidPrice, bestAskPrice, etc.
  • chains is the list of chains to receive a signed payload for, such as evm, solana, etc.
  • channel determines the update rate: updates in the real_time channel are sent as frequently as possible, while fixed_rate@200ms and fixed_rate@50ms channels are updated at fixed rates.

There are also a few other configuration parameters -- see the API documentation (opens in a new tab) for more details.

Determine the most suitable values for your application -- they will be used in the next step.

3. Subscribe to the price updates

To subscribe to the price updates, send a request to the websocket server. The server will respond with a signed price update.

  1. Pyth Lazer provides an SDK (opens in a new tab) to seamlessly integrate the websocket API into your application. Install it using the following command:
npm install --save @pythnetwork/pyth-lazer-sdk
  1. Then create a PythLazerClient (opens in a new tab) object using the URL and the access token requested from the Pyth team in the first step:
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";
 
const client = await PythLazerClient.create(
  ["wss://pyth-lazer.dourolabs.app/v1/stream"],
  "YOUR_ACCESS_TOKEN"
);
  1. After the client is created, subscribe to updates (using the configuration parameters from step 2):
client.subscribe({
  type: "subscribe",
  subscriptionId: 1,
  priceFeedIds: [1, 2],
  properties: ["price"],
  chains: ["solana"],
  channel: "fixed_rate@200ms",
});
  1. Once the connection is established, the server will start sending the price updates to the client:
client.addMessageListener((message) => {
  console.log(message);
});

By default, price updates contain the parsed field that one can use to easily interpret the price update in their backend or frontend, as well as evm and/or solana fields that contain data that one should include in the on-chain transaction:

{
  "type": "streamUpdated",
  "subscriptionId": 1,
  "parsed": {
    "timestampUs": "1730986152400000",
    "priceFeeds": [
      {
        "priceFeedId": 1,
        "price": "1006900000000"
      },
      {
        "priceFeedId": 2,
        "price": "2006900000000"
      }
    ]
  },
  "solana": {
    "encoding": "hex",
    "data": "b9011a82d239c094c52016990d6ca2b261dbb1157ad503cbd3ea0679493316150cf3457624d19ec3f6e0a0e94373ab0971e39d939beda15cc02eb3c5454eb700f1f7310df65210bee4fcf5b1cee1e537fabcfd95010297653b94af04d454fc473e94834f2a0075d3c7938094b99e52260600030201000000010000b5ea6fea00000002000000010000c58f44d3010000"
  }
}

Additional Resources

You may find these additional resources helpful for subscribing to price updates from Pyth Lazer.

Price Feed IDs

Pyth Lazer supports a wide range of price feeds. Consult the Price Feed IDs page for a complete list of supported price feeds.

Examples

pyth-lazer-example-js (opens in a new tab) is a simple example for subscribing to the Pyth Lazer websocket.

Last updated on