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

x402-stellar-sdk

v1.0.4

Published

HTTP 402 payment-gated APIs with Stellar payments

Readme

x402-stellar-sdk

HTTP 402 Payment Required for Stellar. Monetize APIs by requiring a Stellar payment before serving protected routes. Config-driven (amount, asset, destination, network); works with Hono, Express, and Next.js.

Install

npm install x402-stellar-sdk

Server API

Options (X402StellarOptions)

| Field | Type | Description | |-------|------|--------------| | price | string | Amount (e.g. "1", "10.5") | | assetCode | string | Asset code (e.g. "XLM", "USDC") | | issuer | string? | Issuer account (G...) for custom assets; omit for native XLM | | network | "mainnet" \| "testnet" | Stellar network | | destination | string | Destination account (G...) that receives payment | | memo | string? | Optional memo for payment correlation |

Protect a route (Express)

import express from "express";
import { x402 } from "x402-stellar-sdk/server";

const app = express();

const options = {
  price: "1",
  assetCode: "XLM",
  network: "testnet" as const,
  destination: process.env.X402_DESTINATION!, // G...
  memo: "premium-api",
};

app.use("/api/premium", x402(options));
app.get("/api/premium", (_req, res) => {
  res.json({ data: "Premium content – payment verified." });
});

app.listen(3000);

Protect a route (Hono)

import { Hono } from "hono";
import { x402Hono } from "x402-stellar-sdk/server/hono";

const app = new Hono();
const options = {
  price: "0.5",
  assetCode: "XLM",
  network: "mainnet" as const,
  destination: process.env.X402_DESTINATION!,
};

app.use("/api/premium", x402Hono(options));
app.get("/api/premium", (c) => c.json({ data: "Premium content" }));

Protect a route (Next.js App Router)

// app/api/premium/route.ts
import { withX402 } from "x402-stellar-sdk/server/next";

const options = {
  price: "1",
  assetCode: "XLM",
  network: "testnet" as const,
  destination: process.env.X402_DESTINATION!,
};

export async function GET(req: Request) {
  const res402 = await withX402(req.headers, options);
  if (res402) return res402;
  return Response.json({ data: "Premium content" });
}

Verification

Payment is verified by reading X-402-Transaction-Hash from the request and checking the transaction on Horizon: correct destination, amount ≥ price, and asset. Optional memo is compared if set. Use verifyPaymentOnChain(txHash, options) from x402-stellar-sdk/server for custom flows.

Client API

x402Fetch(input, init?, options?)

Fetches a URL. If the server responds with 402 Payment Required, the SDK parses payment details from headers (X-402-Amount, X-402-Asset-Code, X-402-Network, X-402-Destination, etc.). You must provide payWithStellar to perform the payment; then the client retries the request with the transaction hash in X-402-Transaction-Hash.

import { x402Fetch } from "x402-stellar-sdk/client";
import type { X402PaymentRequest, X402PaymentResponse } from "x402-stellar-sdk/client";

const response = await x402Fetch("https://api.example.com/api/premium", undefined, {
  payWithStellar: async (req: X402PaymentRequest): Promise<X402PaymentResponse | null> => {
    // 1. Show UI (e.g. modal) with req.amount, req.assetCode, req.destination, req.network, req.memo
    // 2. User pays with Freighter or another wallet
    // 3. Return { transactionHash } once the payment tx is submitted
    const txHash = await submitPaymentWithWallet(req);
    return txHash ? { transactionHash: txHash } : null;
  },
});
const data = await response.json();

Using with a payment modal (UI)

In an app that uses the ui package (e.g. sdk-fe), you can show a modal with payment instructions and a “Pay with Stellar” action. The modal receives the X402PaymentRequest from the 402 response and calls your wallet (e.g. Freighter); on success, pass the transaction hash to payWithStellar’s resolution so x402Fetch can retry the request. See the ui package for an X402PaymentModal component that fits this flow.

Exports

  • Main: x402, createPaymentRequiredResponse, processPaymentMiddleware, x402Fetch, and types.
  • Server: x402-stellar-sdk/serverx402, createPaymentRequiredResponse, processPaymentMiddleware, verifyPaymentOnChain, types.
  • Server Hono: x402-stellar-sdk/server/honox402Hono.
  • Server Next: x402-stellar-sdk/server/nextwithX402.
  • Client: x402-stellar-sdk/clientx402Fetch, X402PaymentRequest, X402PaymentResponse, X402ClientOptions.