|
| 1 | +import { Callout, Steps } from "nextra/components"; |
| 2 | + |
1 | 3 | # Getting Started with Pyth Lazer
|
2 | 4 |
|
3 |
| -Please refer to the how-to guides to get started. |
| 5 | +Pyth Lazer is a high-performance, low-latency service that provides real-time financial market data. |
| 6 | +This guide will walk you through setting up and running a basic JavaScript example to subscribe to Lazer price feeds. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +Before getting started, make sure you have the following installed: |
| 11 | + |
| 12 | +- **Node.js** (version 18 or higher) |
| 13 | +- **pnpm** package manager |
| 14 | +- **Git** for cloning the examples repository |
| 15 | +- A **Lazer Access Token** - see [How to Acquire an Access Token](./acquire-access-token) if you don't have one |
| 16 | + |
| 17 | +<Steps> |
| 18 | + |
| 19 | +### Clone the Examples Repository |
| 20 | + |
| 21 | +First, clone the Pyth examples repository which contains the JavaScript SDK example: |
| 22 | + |
| 23 | +```bash copy |
| 24 | +git clone https://github.com/pyth-network/pyth-examples.git |
| 25 | +cd pyth-examples/lazer/js |
| 26 | +``` |
| 27 | + |
| 28 | +### Install Dependencies |
| 29 | + |
| 30 | +Install the required dependencies using pnpm: |
| 31 | + |
| 32 | +```bash copy |
| 33 | +pnpm install |
| 34 | +``` |
| 35 | + |
| 36 | +This will install `@pythnetwork/pyth-lazer-sdk`, which is the main Lazer SDK. |
| 37 | + |
| 38 | +### Configure Your Access Token |
| 39 | + |
| 40 | +Set your Lazer access token as an environment variable: |
| 41 | + |
| 42 | +```bash copy |
| 43 | +export ACCESS_TOKEN=your_actual_token_here |
| 44 | +``` |
| 45 | + |
| 46 | +<Callout type="warning" emoji="⚠️"> |
| 47 | + Replace `your_actual_token_here` with your actual Lazer access token. If you |
| 48 | + don't have one, follow the [access token guide](./acquire-access-token) to |
| 49 | + obtain it. |
| 50 | +</Callout> |
| 51 | + |
| 52 | +### Run the Basic WebSocket Example |
| 53 | + |
| 54 | +Now you can run the basic example that demonstrates connecting to Lazer and receiving price updates: |
| 55 | + |
| 56 | +```bash copy |
| 57 | +pnpm run start |
| 58 | +``` |
| 59 | + |
| 60 | +This command will subscribe to Pyth Lazer updates for two price feeds, receiving streaming updates. |
| 61 | +Each update is then printed to the terminal on receipt. |
| 62 | +You should see output similar to the following: |
| 63 | + |
| 64 | +``` |
| 65 | +got message: { |
| 66 | + type: 'json', |
| 67 | + value: { |
| 68 | + type: 'streamUpdated', |
| 69 | + subscriptionId: 1, |
| 70 | + parsed: { timestampUs: '1758034015200000', priceFeeds: [Array] }, |
| 71 | + solana: { |
| 72 | + encoding: 'hex', |
| 73 | + data: 'b9011a82036df6ced259a33949ab9b2c48a61a2d3b0b9436cba24c3ef8a600b72767927d14a459fcc3abce280b3f8194e16e8b32f9322ac0b84a9c0b792e19857962a60180efc1f480c5615af3fb673d42287e993da9fbc3506b6e41dfa32950820c2e6c2a0075d3c79300a3fa30ec3e060003020100000001009053802f790a00000200000001004234106d67000000' |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +stream updated for subscription 1 : [ |
| 78 | + { priceFeedId: 1, price: '11515604259728' }, |
| 79 | + { priceFeedId: 2, price: '444211409986' } |
| 80 | +] |
| 81 | +got message: { |
| 82 | + type: 'json', |
| 83 | + value: { |
| 84 | + type: 'streamUpdated', |
| 85 | + subscriptionId: 1, |
| 86 | + parsed: { timestampUs: '1758034015400000', priceFeeds: [Array] }, |
| 87 | + solana: { |
| 88 | + encoding: 'hex', |
| 89 | + data: 'b9011a826f5ff7e25ac4056c4ec3a08c428baf38e7b78c46014296ccbcfd5395c38c9f7bc23865a048401c66788e791f0edc3a6701b0ea4a5399f50ec8b1795757854f0180efc1f480c5615af3fb673d42287e993da9fbc3506b6e41dfa32950820c2e6c2a0075d3c79340b0fd30ec3e060003020100000001005821a32f790a00000200000001004334106d67000000' |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +stream updated for subscription 1 : [ |
| 94 | + { priceFeedId: 1, price: '11515606540632' }, |
| 95 | + { priceFeedId: 2, price: '444211409987' } |
| 96 | +] |
| 97 | +``` |
| 98 | + |
| 99 | +### Understanding the Code |
| 100 | + |
| 101 | +The main example code in `src/index.ts` demonstrates the core Lazer integration pattern: |
| 102 | + |
| 103 | +```typescript |
| 104 | +import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk"; |
| 105 | + |
| 106 | +const client = await PythLazerClient.create({ |
| 107 | + urls: ["wss://pyth-lazer.dourolabs.app/v1/stream"], |
| 108 | + token: process.env.ACCESS_TOKEN!, |
| 109 | +}); |
| 110 | + |
| 111 | +// The message listener is called every time a new message is received. |
| 112 | +client.addMessageListener((message) => { |
| 113 | + // Add your logic to consume messages here |
| 114 | + console.log("got message:", message); |
| 115 | +}); |
| 116 | + |
| 117 | +// Subscribe to price feeds |
| 118 | +client.subscribe({ |
| 119 | + type: "subscribe", |
| 120 | + subscriptionId: 1, |
| 121 | + priceFeedIds: [1, 2], |
| 122 | + properties: ["price"], |
| 123 | + formats: ["solana"], |
| 124 | + deliveryFormat: "json", |
| 125 | + channel: "fixed_rate@200ms", |
| 126 | + jsonBinaryEncoding: "hex", |
| 127 | +}); |
| 128 | +``` |
| 129 | + |
| 130 | +</Steps> |
| 131 | + |
| 132 | +## What's Next? |
| 133 | + |
| 134 | +Now that you've successfully run the basic Lazer example, you can explore more advanced integration patterns: |
| 135 | + |
| 136 | +### More Information |
| 137 | + |
| 138 | +Explore additional Lazer capabilities: |
| 139 | + |
| 140 | +- **[Subscribe to Price Updates](./subscribe-price-updates)** - Detailed guide on WebSocket subscriptions and message handling |
| 141 | +- **[Price Feed IDs](./price-feed-ids)** - Complete list of available price feeds and their identifiers |
| 142 | + |
| 143 | +### Blockchain Integration |
| 144 | + |
| 145 | +Learn how to integrate Lazer price feeds into your smart contracts: |
| 146 | + |
| 147 | +- **[Solana Integration](./integrate-as-consumer/svm)** - Build SVM smart contracts that consume Lazer price feeds with cryptographic verification |
| 148 | +- **[EVM Integration](./integrate-as-consumer/evm)** - Integrate Lazer into Ethereum and other EVM-compatible chains |
| 149 | + |
| 150 | +### Example Applications |
| 151 | + |
| 152 | +Check out more comprehensive examples in the [pyth-examples repository](https://github.com/pyth-network/pyth-examples/tree/main/lazer): |
| 153 | + |
| 154 | +- **Solana Examples** - Post price data to Solana smart contracts with Ed25519 and ECDSA verification |
| 155 | +- **EVM Examples** - Smart contract integration for Ethereum-compatible chains |
0 commit comments