x402 in TypeScript & Node.js: call a paid API with @2sio/sdk

The @2sio/sdk TypeScript client wraps the x402 probe → sign → retry loop behind a typed method call. Works in Node and any modern JS runtime.

Install and call

Install the SDK, set an EVM wallet key, and call any endpoint. The result includes the data, the USDC cost, and the settlement tx:

TypeScript — @2sio/sdktypescript
// npm i @2sio/sdk
import { TwoS } from '@2sio/sdk'

// Fund any EVM wallet with a little USDC on Base. No signup, no API key.
const client = new TwoS({ privateKey: process.env.EVM_PRIVATE_KEY as `0x${string}` })

const res = await client.weather.zip({ zip: '94103' })
console.log(res.data)          // real National Weather Service forecast
console.log(res.costUsd)       // what this call cost, in USDC
console.log(res.settlement?.txHash)  // on-chain settlement tx

Try before you pay

Construct the client in trial mode for a free real call (one per endpoint per hour) — no wallet needed:

TypeScript — trial modetypescript
import { TwoS } from '@2sio/sdk'

const client = new TwoS({ trial: true })   // no key needed
const res = await client.validate.iban({ iban: 'GB82WEST12345698765432' })
console.log(res.data)  // real result, marked meta.trial

Prefer raw fetch?

You do not have to use the SDK. The wire flow is a plain fetch that gets a 402, a signed payment header, and a retry — the same shape the SDK automates. See the cURL guide for the raw envelope, or the facilitator guide for how settlement works.

Call a live x402 API right now — no signup.

Browse the full endpoint directory, grab the quickstart, or read the llms.txt manifest. Every endpoint is keyless and pay-per-call.

FAQ

How do I call an x402 API in TypeScript or Node.js?
Install @2sio/sdk, construct new TwoS({ privateKey }) with an EVM wallet key, and call any endpoint method (e.g. client.weather.zip({ zip: "94103" })). The SDK performs the 402 payment flow automatically.
Can I use plain fetch instead of the SDK?
Yes. The flow is: fetch the URL, receive 402 with the PaymentRequirements, build a PAYMENT-SIGNATURE from the accepts entry using a wallet library, and refetch with that header. The SDK just does this for you.
Does it work with axios / other HTTP clients?
The payment is just an HTTP header, so any client works. The SDK uses fetch under the hood; you can replicate the same probe-and-retry with axios if you build the PAYMENT-SIGNATURE yourself.

Related

Topics: x402 typescript · x402 node.js · x402 nodejs example · x402 javascript · x402 fetch · x402 axios · x402 client example