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

@winsznx/lend402

v0.1.6

Published

Lend402 AI Agent SDK — x402 V2 JIT micro-lending interceptor for Stacks

Readme

@winsznx/lend402

Lend402 AI Agent SDK — x402 V2 JIT micro-lending interceptor for AI agents on Stacks.

When a paywalled API responds with HTTP 402, the interceptor automatically:

  1. Parses the x402 V2 payment challenge
  2. Calls simulate-borrow on the lend402-vault (read-only pre-flight)
  3. Builds a borrow-and-pay Stacks contract-call transaction
  4. Signs it with the agent's private key (PostConditionMode.Deny)
  5. Base64-encodes the signed payload into the payment-signature header
  6. Retries the original request — transparently, in a single await

Install

npm install @winsznx/lend402

Usage

import { withPaymentInterceptor, mainnetConfig } from "@winsznx/lend402";
import { StacksMainnet } from "@stacks/network";

const agent = withPaymentInterceptor({
  ...mainnetConfig(),
  privateKey: process.env.AGENT_PRIVATE_KEY!,
  agentAddress: "SP1AGENT...",
  vaultContractAddress: "SP3VAULT...",
  vaultContractName: "lend402-vault",
  onEvent: (event) => console.log(event.type, event.data),
});

// Automatically pays any HTTP 402 via JIT borrow
const { data } = await agent.get("https://api.dataprovider.com/premium");

API

withPaymentInterceptor(config, axiosConfig?)

Creates a pre-configured Axios instance with the Lend402 interceptor attached.

mainnetConfig()

Returns network-specific defaults for Stacks mainnet (stacks:1):

  • network: StacksMainnet
  • caip2Network: "stacks:1"
  • sbtcContractAddress / sbtcContractName
  • usdcxContractAddress / usdcxContractName

Spread into your AgentClientConfig and provide privateKey, agentAddress, and vault contract details.

testnetConfig()

Same as mainnetConfig() but for Stacks testnet (stacks:2147483648).

AgentClientConfig

| Field | Type | Description | |---|---|---| | privateKey | string | Agent's Stacks private key (hex, 32 bytes) | | agentAddress | string | Agent's Stacks address | | network | StacksNetwork | Stacks network instance | | caip2Network | Caip2NetworkId | "stacks:1" or "stacks:2147483648" | | vaultContractAddress | string | Deployed lend402-vault contract address | | vaultContractName | string | lend402-vault contract name | | sbtcContractAddress | string | sBTC SIP-010 contract address | | sbtcContractName | string | sBTC token contract name | | usdcxContractAddress | string | USDCx SIP-010 contract address | | usdcxContractName | string | USDCx token contract name | | timeoutMs? | number | Axios timeout in ms (default: 30000) | | maxPaymentRetries? | number | Max 402 retries (default: 1) | | onEvent? | (event: AgentEvent) => void | Real-time event callback |

Error handling

The interceptor throws if the borrow or signing fails. The vault's PostConditionMode.Deny guarantee means no funds move on a failed transaction — the agent's treasury is unchanged.

import { withPaymentInterceptor, mainnetConfig } from "@winsznx/lend402";

const agent = withPaymentInterceptor({
  ...mainnetConfig(),
  privateKey: process.env.AGENT_PRIVATE_KEY!,
  agentAddress: process.env.AGENT_ADDRESS!,
  vaultContractAddress: "SP3VAULT...",
  vaultContractName: "lend402-vault",
  onEvent: (event) => {
    if (event.type === "PAYMENT_CONFIRMED") {
      console.log("Settled:", event.data.txid);
    }
    if (event.type === "ERROR") {
      console.error("Agent error:", event.data.message);
    }
  },
});

try {
  const { data } = await agent.get("https://api.dataprovider.com/premium");
} catch (err) {
  // Payment failed — either insufficient sBTC collateral for the required
  // ratio, or the transaction was aborted on-chain by PostConditionMode.DENY.
  // The agent's treasury is unchanged. Inspect err.message for the stage
  // that failed: simulate-borrow, tx build, or the retried request itself.
  console.error(err);
}

Event lifecycle

| Event type | When it fires | |---|---| | REQUEST_SENT | Every outbound request | | PAYMENT_REQUIRED_RECEIVED | 402 intercepted, challenge parsed | | SIMULATE_BORROW_OK | Collateral requirement confirmed | | TX_BUILT | borrow-and-pay tx built and signed | | TX_SIGNED | Serialized, txid computed | | PAYMENT_HEADER_ATTACHED | payment-signature header encoded | | REQUEST_RETRIED | Original request retried with payment | | PAYMENT_CONFIRMED | payment-response header received | | DATA_RETRIEVED | Origin response body forwarded to caller | | ERROR | Any stage failure — treasury unchanged |

License

MIT