Pyth Playground
Configure subscription parameters, generate code, and test real-time price streams.
pyth-lazer-example.tsimport { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk";
// Read your API key from the environment (never hard-code it)
const token = process.env.LAZER_TOKEN;
if (!token) throw new Error("Set LAZER_TOKEN in your environment");
// Create the Pyth Lazer client with WebSocket pool configuration
const client = await PythLazerClient.create({
token,
webSocketPoolConfig: {
urls: [
"wss://pyth-lazer-0.dourolabs.app/v1/stream",
"wss://pyth-lazer-1.dourolabs.app/v1/stream",
"wss://pyth-lazer-2.dourolabs.app/v1/stream",
],
},
});
// Subscribe to price feeds
client.subscribe({
type: "subscribe",
subscriptionId: 1,
priceFeedIds: [1, 2],
properties: ["price"],
formats: ["solana"],
channel: "fixed_rate@200ms",
deliveryFormat: "json",
jsonBinaryEncoding: "hex",
parsed: true,
// ignoreInvalidFeeds: true, // Ignore invalid feed IDs instead of failing
});
// Listen for price updates
client.addMessageListener((message) => {
if (message.type === "json") {
console.log("Received JSON update:", JSON.stringify(message.value, null, 2));
} else {
console.log("Received binary update:", {
subscriptionId: message.value.subscriptionId,
parsed: message.value.parsed,
});
}
});
// Handle connection errors
client.addAllConnectionsDownListener(() => {
console.error("All WebSocket connections are down!");
});
// To unsubscribe later:
// client.unsubscribe(1);
// To shutdown the client:
// client.shutdown();
Ready