@libertai/x402
v0.1.0
Published
x402 utils to use LibertAI services
Readme
@libertai/x402
x402 utilities that handle payments automatically for LibertAI services
Install
npm install @libertai/x402Usage with OpenAI SDK
import { wrapFetchWithPayment } from "@libertai/x402";
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.libertai.io/v1",
apiKey: "x402",
fetch: wrapFetchWithPayment(process.env.PRIVATE_KEY as `0x${string}`),
});
const response = await client.chat.completions.create({
model: "qwen3-coder-next",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0].message.content);Set apiKey to "x402" — the wrapper strips this dummy value before sending requests. Real API keys (any other value) are preserved.
Usage with plain fetch
import { wrapFetchWithPayment } from "@libertai/x402";
const fetchWithPayment = wrapFetchWithPayment("0x...");
const response = await fetchWithPayment(
"https://api.libertai.io/v1/chat/completions",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
model: "qwen3-coder-next",
messages: [{ role: "user", content: "Hello!" }],
}),
},
);How it works
- Makes the request normally
- If the server responds with
402, readsPaymentRequirementsfrom the response body - Signs an EIP-712 typed data message (TransferWithAuthorization on Base) using the provided private key
- Retries the request with the signed payment in the
X-PAYMENTheader
License
MIT
