SDK - JavaScript

JavaScript SDK (@paynodelabs/sdk-js)

The JavaScript SDK provides both client-side tools for AI Agents (Payer) and server-side middleware for Merchants (Receiver).

📦 Installation

npm install @paynodelabs/sdk-js ethers

🤖 For AI Agents (Client)

The PayNodeClient is a wrapper that automatically handles 402 responses, executes USDC payments, and retries requests.

import { PayNodeClient } from '@paynodelabs/sdk-js';
 
// 1. Initialize with the Agent's private key
// Ensure this wallet has Base ETH (for gas) and Base USDC (for payment)
const client = new PayNodeClient(process.env.AGENT_PRIVATE_KEY);
 
async function fetchData() {
    try {
        // 2. Make a request. The SDK handles the 402 handshake internally.
        const response = await client.request('https://api.merchant.com/data', {
            method: 'GET'
        });
 
        const data = await response.json();
        console.log("Success:", data);
        
    } catch (error) {
        console.error("Payment or Request Failed:", error);
    }
}

Mainnet Hardening

The JS SDK automatically performs Infinite Approvals for the Router to save gas on subsequent calls, and overrides gasLimit to prevent estimateGas flakiness during network congestion.


🏪 For Merchants (Express Middleware)

The createPayNodeMiddleware protects your routes by enforcing a stateless USDC payment via the X-POM-V1 header.

import express from 'express';
import { createPayNodeMiddleware } from '@paynodelabs/sdk-js';
 
const app = express();
 
// 1. Define the cost and your receiving wallet
const requirePayment = createPayNodeMiddleware({
    price: "2.50", // Cost in USDC
    merchantWallet: "0xYourWalletAddress...",
    rpcUrl: process.env.BASE_RPC_URL || "https://mainnet.base.org"
});
 
// 2. Protect the route
app.get('/api/ai-inference', requirePayment, (req, res) => {
    // This code ONLY runs if a valid, un-replayed TxHash was provided
    res.json({
        result: "The meaning of life is 42.",
        paid: true
    });
});
 
app.listen(3000, () => console.log('Merchant Server running on port 3000'));