|
| 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 price feed service that provides real-time financial market data to blockchain applications. This guide will walk you through setting up and running a basic JavaScript example to subscribe to Lazer price feeds. |
| 6 | + |
| 7 | +## What You'll Learn |
| 8 | + |
| 9 | +In this guide, you'll learn how to: |
| 10 | +- Set up the Pyth Lazer JavaScript SDK |
| 11 | +- Connect to the Lazer WebSocket stream |
| 12 | +- Subscribe to real-time price feeds |
| 13 | +- Handle both JSON and binary message formats |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +Before getting started, make sure you have the following installed: |
| 18 | + |
| 19 | +- **Node.js** (version 18 or higher) |
| 20 | +- **pnpm** package manager |
| 21 | +- **Git** for cloning the examples repository |
| 22 | +- A **Lazer Access Token** - see [How to Acquire an Access Token](./acquire-access-token) if you don't have one |
| 23 | + |
| 24 | +<Steps> |
| 25 | + |
| 26 | +### Clone the Examples Repository |
| 27 | + |
| 28 | +First, clone the Pyth examples repository which contains the JavaScript SDK example: |
| 29 | + |
| 30 | +```bash copy |
| 31 | +git clone https://github.com/pyth-network/pyth-examples.git |
| 32 | +cd pyth-examples/lazer/js |
| 33 | +``` |
| 34 | + |
| 35 | +### Install Dependencies |
| 36 | + |
| 37 | +Install the required dependencies using pnpm: |
| 38 | + |
| 39 | +```bash copy |
| 40 | +pnpm install |
| 41 | +``` |
| 42 | + |
| 43 | +This will install the following key packages: |
| 44 | +- `@pythnetwork/pyth-lazer-sdk` - The main Lazer SDK |
| 45 | +- `@solana/web3.js` - For Solana blockchain interactions |
| 46 | +- Other supporting dependencies |
| 47 | + |
| 48 | +### Configure Your Access Token |
| 49 | + |
| 50 | +Set your Lazer access token as an environment variable: |
| 51 | + |
| 52 | +```bash copy |
| 53 | +export ACCESS_TOKEN=your_actual_token_here |
| 54 | +``` |
| 55 | + |
| 56 | +<Callout type="warning" emoji="⚠️"> |
| 57 | + Replace `your_actual_token_here` with your actual Lazer access token. If you don't have one, follow the [access token guide](./acquire-access-token) to obtain it. |
| 58 | +</Callout> |
| 59 | + |
| 60 | +### Run the Basic WebSocket Example |
| 61 | + |
| 62 | +Now you can run the basic example that demonstrates connecting to Lazer and receiving price updates: |
| 63 | + |
| 64 | +```bash copy |
| 65 | +pnpm run start |
| 66 | +``` |
| 67 | + |
| 68 | +This will execute the main example script that: |
| 69 | +- Connects to the Pyth Lazer WebSocket server |
| 70 | +- Subscribes to price feeds (IDs 1 and 2) |
| 71 | +- Listens for both JSON and binary messages |
| 72 | +- Displays received price data and connection status |
| 73 | +- Automatically shuts down after 10 seconds |
| 74 | + |
| 75 | +### Understanding the Code |
| 76 | + |
| 77 | +The main example code in `src/index.ts` demonstrates the core Lazer integration pattern: |
| 78 | + |
| 79 | +```typescript |
| 80 | +import { PythLazerClient } from "@pythnetwork/pyth-lazer-sdk"; |
| 81 | + |
| 82 | +const client = await PythLazerClient.create({ |
| 83 | + urls: ["wss://pyth-lazer.dourolabs.app/v1/stream"], |
| 84 | + token: process.env.ACCESS_TOKEN!, |
| 85 | +}); |
| 86 | + |
| 87 | +// Subscribe to price feeds |
| 88 | +client.subscribe({ |
| 89 | + type: "subscribe", |
| 90 | + subscriptionId: 1, |
| 91 | + priceFeedIds: [1, 2], |
| 92 | + properties: ["price"], |
| 93 | + formats: ["solana"], |
| 94 | + deliveryFormat: "json", |
| 95 | + channel: "fixed_rate@200ms", |
| 96 | + jsonBinaryEncoding: "hex", |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +Key concepts: |
| 101 | +- **PythLazerClient**: The main client for connecting to Lazer |
| 102 | +- **Subscription**: Defines which price feeds and properties you want to receive |
| 103 | +- **Message Listeners**: Handle incoming price updates and connection events |
| 104 | +- **Channels**: Control update frequency (e.g., "fixed_rate@200ms") |
| 105 | + |
| 106 | +</Steps> |
| 107 | + |
| 108 | +## What's Next? |
| 109 | + |
| 110 | +Now that you've successfully run the basic Lazer example, you can explore more advanced integration patterns: |
| 111 | + |
| 112 | +### Blockchain Integration |
| 113 | + |
| 114 | +Learn how to integrate Lazer price feeds into your smart contracts: |
| 115 | + |
| 116 | +- **[Solana Integration](./integrate-as-consumer/svm)** - Build SVM smart contracts that consume Lazer price feeds with cryptographic verification |
| 117 | +- **[EVM Integration](./integrate-as-consumer/evm)** - Integrate Lazer into Ethereum and other EVM-compatible chains |
| 118 | + |
| 119 | +### Advanced Features |
| 120 | + |
| 121 | +Explore additional Lazer capabilities: |
| 122 | + |
| 123 | +- **[Subscribe to Price Updates](./subscribe-price-updates)** - Detailed guide on WebSocket subscriptions and message handling |
| 124 | +- **[Price Feed IDs](./price-feed-ids)** - Complete list of available price feeds and their identifiers |
| 125 | +- **[Acquire Access Token](./acquire-access-token)** - How to obtain and manage your Lazer access credentials |
| 126 | + |
| 127 | +### Example Applications |
| 128 | + |
| 129 | +Check out more comprehensive examples in the [pyth-examples repository](https://github.com/pyth-network/pyth-examples/tree/main/lazer): |
| 130 | + |
| 131 | +- **Solana Examples** - Post price data to Solana smart contracts with Ed25519 and ECDSA verification |
| 132 | +- **EVM Examples** - Smart contract integration for Ethereum-compatible chains |
| 133 | +- **Publisher Examples** - Learn how to publish your own price data to Lazer |
0 commit comments