Node.js x402 guide

Pay BMM Wallet Snapshot from Node.js

This guide shows the smallest practical BMM payment flow: call Wallet Snapshot without payment, receive the x402 challenge, verify the public receiver, sign with a dedicated buyer wallet, and retry with the signed payment header.

1

POST to Wallet Snapshot

Send public wallet input to https://api.blockchainmoneymap.com/api/x402/wallet-snapshot without a payment header.

2

Receive the 402 challenge

The response tells the buyer agent the exact price, receiver, network, and protected resource.

3

Sign payment

Use a dedicated buyer wallet to sign the USDC payment on base; never send private keys to BMM.

4

Retry the same request

Keep the request body unchanged and retry with the signed x402 payment header.

5

Receive wallet intelligence

The paid response returns the Wallet Snapshot result; outputs are research summaries, not financial or compliance advice.

Install client packages

Run this in the buyer agent project. Keep the buyer private key in the local runtime environment and use a dedicated wallet for test purchases.

npm install @x402/fetch @x402/evm viem

Wallet Snapshot example

The first request should return HTTP 402. The paid retry should return the Wallet Snapshot result. Verify the challenge receiver before signing; stop if the receiver is missing or unexpected.

Do not paste private keys, seed phrases, wallet passwords, or raw signed payment headers into websites, support chats, public logs, or shared documents.
import { x402Client, x402HTTPClient } from "@x402/fetch";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const endpoint = "https://api.blockchainmoneymap.com/api/x402/wallet-snapshot";
const body = {
  address: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e",
  chain: "base"
};

const signer = privateKeyToAccount(process.env.BUYER_PRIVATE_KEY as `0x${string}`);
const client = new x402Client();
registerExactEvmScheme(client, { signer, networks: ["eip155:8453"] });
const httpClient = new x402HTTPClient(client);

const first = await fetch(endpoint, {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify(body)
});

if (first.status !== 402) {
  throw new Error(`Expected 402 payment challenge, got ${first.status}`);
}

const challengeBody = await first.json();
const paymentRequired = httpClient.getPaymentRequiredResponse(
  (name) => first.headers.get(name),
  challengeBody
);

const expectedPayTo = "0x2211E12e6Bc8Da27B48B9DD66A55f5C3388D713a".toLowerCase();
const payTo = paymentRequired.accepts?.[0]?.payTo?.toLowerCase();
if (!payTo || payTo !== expectedPayTo) {
  throw new Error(`Unexpected x402 receiver: ${payTo ?? "missing"}`);
}

const paymentPayload = await httpClient.createPaymentPayload(paymentRequired);
const paymentHeaders = httpClient.encodePaymentSignatureHeader(paymentPayload);

const paid = await fetch(endpoint, {
  method: "POST",
  headers: { "content-type": "application/json", ...paymentHeaders },
  body: JSON.stringify(body)
});

console.log(paid.status, await paid.json());