npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@phantom/phantom-api-client

v1.2.1

Published

Shared HTTP client for api.phantom.app with 402/429 payment gating support

Readme

@phantom/phantom-api-client

Shared HTTP client for api.phantom.app with automatic 402 payment gating and 429 rate-limit handling.

Installation

yarn add @phantom/phantom-api-client

Usage

Basic setup

import { PhantomApiClient } from "@phantom/phantom-api-client";

const client = new PhantomApiClient({
  baseUrl: "https://api.phantom.app",
});

Setting static headers

Call setHeaders() once after authentication to attach headers to every subsequent request. Typical use: app ID and wallet address.

client.setHeaders({
  "x-api-key": appId,
  "X-App-Id": appId,
  "X-Wallet-Address": solanaAddress,
});

Headers set via setHeaders() are merged — calling it again adds or overrides individual keys without clearing the rest.

Making requests

// GET with optional query params
const data = await client.get<MyResponse>("/swap/v2/quotes", {
  params: { inputMint: "So11....", outputMint: "EPjF..." },
});

// POST
const result = await client.post<MyResponse>("/swap/v2/quotes", {
  sellToken: "SOL",
  buyToken: "USDC",
  sellAmount: "1000000000",
});

402 Payment Required

When the API returns a 402, a PaymentRequiredError is thrown. You can handle it manually or wire in an automatic payment handler that signs and submits the payment transaction, after which the original request is retried automatically.

import { PaymentRequiredError } from "@phantom/phantom-api-client";

// Option A — manual catch
try {
  const data = await client.get("/swap/v2/quotes");
} catch (err) {
  if (err instanceof PaymentRequiredError) {
    console.log(err.payment); // { network, token, amount, preparedTx, description }
  }
}

// Option B — automatic handler (wired once after login)
client.setPaymentHandler(async payment => {
  // Sign and broadcast the prepared transaction, return the signature
  const signature = await wallet.signAndSend(payment.preparedTx);
  return signature;
});

// Requests now auto-pay on 402 and retry transparently
const data = await client.get("/swap/v2/quotes");

The payment signature is stored automatically via setPaymentSignature() and sent as the X-Payment header on all subsequent requests.

429 Rate Limiting

A 429 response throws RateLimitError with a retryAfterMs field:

import { RateLimitError } from "@phantom/phantom-api-client";

try {
  const data = await client.get("/swap/v2/quotes");
} catch (err) {
  if (err instanceof RateLimitError) {
    console.log(`Retry after ${err.retryAfterMs}ms`);
  }
}

API

new PhantomApiClient(options)

| Option | Type | Description | | ------------------- | ---------------- | ------------------------------------------------------------------------ | | baseUrl | string | Base URL for all requests | | logger | Logger | Optional logger (debug, info, warn, error) | | onPaymentRequired | PaymentHandler | Optional payment handler (can also be set later via setPaymentHandler) |

Methods

| Method | Description | | ------------------------------- | ---------------------------------------------------------------------------------------------------- | | get<T>(path, options?) | GET request. options.params appended as query string. options.headers merged for this call only. | | post<T>(path, body, options?) | POST request with JSON body. | | rpc<T>(path, method, params) | JSON-RPC 2.0 wrapper around post. Unwraps result or throws on error. | | setHeaders(headers) | Merges headers into every future request (e.g. X-Wallet-Address, x-api-key). | | setPaymentHandler(handler) | Wires a payment handler for automatic 402 handling. | | setPaymentSignature(sig) | Stores a payment signature sent as X-Payment on all subsequent requests. |