Developer Hub

Subscribe to Prices

Learn how to authenticate, configure, and subscribe to Pyth Pro price streams

This guide explains how to subscribe to prices from Pyth Pro. This guide will also explain various properties and configuration options to customize the prices. The return data also includes verified payloads that can be verified on the target blockchain.

Subscribing to prices is a three-step process:

  1. Acquire an access token.
  2. Configure subscription parameters.
  3. Subscribe to the prices via websocket API.

The websocket servers are available at:

  • wss://pyth-lazer-0.dourolabs.app/v1/stream
  • wss://pyth-lazer-1.dourolabs.app/v1/stream

Redundancy Required

For redundancy and to avoid interruptions during deployments, you must connect to both endpoints. During deployments, a single endpoint will briefly go down, so maintaining open connections to both endpoints ensures continuous service availability.

1. Acquire an access token

Please fill out this form to get in touch with our authorized distribution partners (Pyth Data Distributors) and obtain an access token.

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

Security Warning: Never expose your access token in frontend applications or client-side code. Access tokens should only be used in secure backend environments. Exposing tokens in frontend code makes them publicly accessible and is a violation of our terms of service.

2. Configure subscription parameters

Pyth Pro supports several request/subscription parameters to customize the received prices. 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 price data for. It will also include the verified payloads for the price feeds. 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 for more details.

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

3. Subscribe to the prices

Complete Payload Reference

For understanding all fields and data types of Lazer payloads, see our Payload Reference page.

To subscribe to the prices, send a request to the websocket server. The server will respond with a signed payload.

  1. Pyth Lazer provides an SDK 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 object using both endpoint URLs and the access token requested from our authorized distribution partners (Pyth Data Distributors) in the first step:
import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";

const client = await PythLazerClient.create(
  [
    "wss://pyth-lazer-0.dourolabs.app/v1/stream",
    "wss://pyth-lazer-1.dourolabs.app/v1/stream",
  ],
  "YOUR_ACCESS_TOKEN",
);
  1. After the client is created, subscribe to prices (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 and verified payloads to the client:
client.addMessageListener((message) => {
  console.log(message);
});

By default, verified payloads contain the parsed field that one can use to easily interpret the price data 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 prices from Pyth Pro.

Price Feed IDs

Pyth Pro 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 is a simple example for subscribing to the Pyth Pro websocket.