# @pythnetwork/client

> Client for consuming Pyth price data

Latest version **2.22.1** (published 2025-02-06) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @pythnetwork/client
pnpm add @pythnetwork/client
yarn add @pythnetwork/client
bun add @pythnetwork/client
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.22.1 |
| Published | 2025-02-06 |
| First published | 2021-05-15 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 138.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 106 |
| Maintainers | marc.pyth, jayantk2, pythrelease, cprussin |
| Keywords | pyth, solana, oracle |

## Links

- npm: https://www.npmjs.com/package/@pythnetwork/client
- Repository: https://github.com/pyth-network/pyth-client-js
- Homepage: https://pyth.network
- Issues: https://github.com/pyth-network/pyth-client-js/issues
- npm.io page: https://npm.io/package/@pythnetwork/client

## Dependencies (3)

- [buffer](https://npm.io/package/buffer.md) ^6.0.1
- [@coral-xyz/borsh](https://npm.io/package/@coral-xyz/borsh.md) ^0.28.0
- [@coral-xyz/anchor](https://npm.io/package/@coral-xyz/anchor.md) ^0.29.0

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 2.22.1 (latest) — 2025-02-06
- 2.22.0 — 2024-08-28
- 2.21.1 — 2024-06-24
- 2.21.0 — 2024-02-29
- 2.20.0 — 2024-02-28
- 2.19.0 — 2023-06-22
- 2.18.0 — 2023-06-21
- 2.17.0 — 2023-04-06
- 2.16.0 — 2023-04-05
- 2.15.0 — 2023-02-13
- 2.14.0 — 2023-02-06
- 2.13.1 — 2023-02-04
- 2.13.0 — 2023-02-03
- 2.12.0 — 2023-01-26
- 2.11.0 — 2023-01-20
- … 29 more at https://npm.io/package/@pythnetwork/client/versions

## README

# @pythnetwork/client

## A library for reading Pyth accounts on Pythnet

[Pyth](https://pyth.network/) is building a way to deliver a decentralized, cross-chain market of verifiable data from high-quality nodes to any smart contract, anywhere.

This library reads Pythnet (Pyth's app-specific SVM blockchain) data using [@solana/web3.js](https://www.npmjs.com/package/@solana/web3.js) and returns JavaScript-friendly objects.

> ⚠️ **Important Warning**: For most use cases, it is recommended and more user-friendly to use Pyth's off-chain API ([Hermes](https://hermes.pyth.network/docs/)) via the [@pythnetwork/hermes-client](https://www.npmjs.com/package/@pythnetwork/hermes-client) package to get the most up-to-date Pyth prices. Using `@pythnetwork/client` requires a Pythnet RPC connection and exposes many low-level details.

## Installation

### npm

```
$ npm install --save @pythnetwork/client
```

### Yarn

```
$ yarn add @pythnetwork/client
```

## Example Usage

This library lets you consume prices in two different ways: you can either get continuously-streaming price updates via a websocket connection, or send one-off requests every time you want the current price.  

### Streaming updates

The websocket connection provides a subscription model for consuming price updates:

```typescript
const pythConnection = new PythConnection(pythnetWeb3Connection, getPythProgramKeyForCluster(pythnetClusterName))
pythConnection.onPriceChange((product, price) => {
  // sample output:
  // Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
  console.log(`${product.symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
})

// Start listening for price change events.
pythConnection.start()
```

The `onPriceChange` callback will be invoked every time a Pyth price gets updated.
This callback gets two arguments:
* `price` contains the official Pyth price and confidence, along with the component prices that were combined to produce this result.
* `product` contains metadata about the price feed, such as the symbol (e.g., "BTC/USD") and the number of decimal points.

See `src/example_ws_usage.ts` for a runnable example of the above usage.
You can run this example with `npm run ws_example`.

You may also register to specific account updates using `connection.onAccountChange` in the solana web3 API, then
use the methods in `index.ts` to parse the on-chain data structures into Javascript-friendly objects.

### Request an update

The request model allows you to send one-off HTTP requests to get the current price without subscribing to ongoing updates:

```typescript
const pythClient = new PythHttpClient(connection, pythPublicKey);
const data = await pythClient.getData();

for (let symbol of data.symbols) {
  const price = data.productPrice.get(symbol)!;
  // Sample output:
  // Crypto.SRM/USD: $8.68725 ±$0.0131 Status: Trading
  console.log(`${symbol}: $${price.price} \xB1$${price.confidence} Status: ${PriceStatus[price.status]}`)
}
```

The `getData` function will fetch all information about every product listed on Pyth.
This includes the current price as well as metadata, such as the base and quote currencies.
See `src/example_http_usage.ts` for a runnable example of the above usage.
You can run this example with `npm run http_example`.

## Releases

In order to release a new version of this library and publish it to npm, follow these steps: 
1. Update `CHANGELOG.md` with a description of the changes in this version.
2. Run `npm version <new version number>`. This command will update the version of the package, tag the branch in git, and push your changes to github.
3. Once your change is merged into `main`, create a release, and a github action will automatically publish a new version of the package to npm.

---
_Source: https://npm.io/package/@pythnetwork/client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
