x402 Mesh Inference — integration in three steps ================================================ # 1. See what each model costs. Free, no wallet, no key. curl -s https://x402-mesh-gateway.fly.dev/v1/pricing # 2. See the payment challenge for a request you have not paid for. curl -si -X POST https://x402-mesh-gateway.fly.dev/v1/chat/completions \ -H "content-type: application/json" \ -d '{"model":"llama-3.3-70b-versatile","messages":[{"role":"user","content":"hi"}]}' \ | grep -i payment-required # 3. Install the client and pay it. npm i @x402/core @x402/avm ------------------------------------------------ // pay.mjs — a complete paid request against https://x402-mesh-gateway.fly.dev // Run: AVM_PRIVATE_KEY= node pay.mjs // // The paying account needs a little ALGO for its minimum balance and must be OPTED IN to the // USDC ASA. The facilitator sponsors the transaction fee, so you only sign the asset transfer. import { toClientAvmSigner } from "@x402/avm"; import { ExactAvmScheme } from "@x402/avm/exact/client"; import { x402Client, x402HTTPClient } from "@x402/core/client"; import { decodePaymentRequiredHeader } from "@x402/core/http"; const ENDPOINT = "https://x402-mesh-gateway.fly.dev/v1/chat/completions"; const body = { // Any model a healthy node advertises at https://x402-mesh-gateway.fly.dev/v1/nodes. model: "llama-3.3-70b-versatile", messages: [{ role: "user", content: "Reply with one word." }], }; // Register the WILDCARD, not a concrete network id. A client matches the challenge's network // verbatim, and this server advertises the facilitator's full-genesis-hash form rather than // the truncated constant `@x402/avm` exports. Registering the constant here fails with // "No network/scheme registered for x402 version: 2". const signer = toClientAvmSigner(process.env.AVM_PRIVATE_KEY); const http = new x402HTTPClient( new x402Client().register("algorand:*", new ExactAvmScheme(signer)), ); const post = (headers) => fetch(ENDPOINT, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body), }); const unpaid = await post({}); if (unpaid.status !== 402) throw new Error(`expected 402, got ${unpaid.status}`); // The challenge is in the HEADER. The JSON body is a human-readable preview with no // `accepts[]` — decoding that instead yields a challenge with nothing to pay against. const challenge = decodePaymentRequiredHeader(unpaid.headers.get("payment-required")); console.log("price:", challenge.accepts[0].amount, "atomic USDC"); // Spread the header MAP the SDK returns. The name is part of the protocol: x402 v2 expects // `PAYMENT-SIGNATURE`, and renaming it to `X-PAYMENT` makes the server answer 402 again. const paid = await post(http.encodePaymentSignatureHeader(await http.createPaymentPayload(challenge))); if (!paid.ok) throw new Error(`${paid.status}: ${await paid.text()}`); const completion = await paid.json(); console.log(completion.choices[0].message.content); console.log("settled:", paid.headers.get("payment-response") ? "yes" : "pending"); ------------------------------------------------ Prices: https://x402-mesh-gateway.fly.dev/v1/pricing Models: https://x402-mesh-gateway.fly.dev/v1/nodes Manifest: https://x402-mesh-gateway.fly.dev/.well-known/x402 Agent docs: https://x402-mesh-gateway.fly.dev/llms.txt