DEV Community

Cover image for MoltsPay Quick Start Guide
Zen7
Zen7

Posted on

MoltsPay Quick Start Guide

MoltsPay Node.js SDK: Quick Start Guide

Get your AI agent making crypto payments in under 5 minutes


What is MoltsPay?

MoltsPay is a universal payment infrastructure built for AI agents, enabling gasless agent-to-agent commerce via the x402 protocol. Your agent can:

  • Pay for external services autonomously
  • Accept payments for its own services
  • Work across 8 blockchains without managing gas
npm install moltspay
Enter fullscreen mode Exit fullscreen mode

That's it. Let's build something.


Part 1: Paying for Services (Client Mode)

Step 1: Initialize Your Agent's Wallet

npx moltspay init
Enter fullscreen mode Exit fullscreen mode

This creates a new wallet at ~/.moltspay/wallet.json. You'll see output like:

βœ… Wallet created!
Address: 0x1234...abcd
Chain: base (default)
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Spending Limits

Protect your agent from overspending:

npx moltspay config --max-per-tx 10 --max-per-day 100
Enter fullscreen mode Exit fullscreen mode

Now your agent can spend max $10 per transaction, $100 per day.

Step 3: Fund the Wallet

npx moltspay fund
Enter fullscreen mode Exit fullscreen mode

Option 1: Onchain transfer through other wallet or exchange. Gas needed.
Option 2: This opens Coinbase Onramp. Scan the code to send USDC to your agent's address. No ETH neededβ€”MoltsPay handles gas.

Step 4: Make a Payment

CLI:

npx moltspay pay https://juai8.com/zen7 text-to-video \
  --prompt "a cat playing piano"
Enter fullscreen mode Exit fullscreen mode

Or in code:

import { MoltsPay } from 'moltspay';

const client = new MoltsPay();

// Pay for a video generation service
const result = await client.pay('https://juai8.com/zen7', 'text-to-video', {
  prompt: 'a cat playing piano'
});

console.log(result.video_url);
// https://cdn.example.com/video_abc123.mp4
Enter fullscreen mode Exit fullscreen mode

The SDK automatically:

  1. Discovers the service at /.well-known/agent-services.json
  2. Handles the 402 Payment Required response
  3. Signs the payment (gasless)
  4. Retries with payment proof
  5. Returns the result

Step 5: Check Your Balance

npx moltspay status
Enter fullscreen mode Exit fullscreen mode
πŸ’° MoltsPay Wallet Status
────────────────────────
Address: 0x1234...abcd
Chain: base

Balances:
  USDC: $47.50

Spending Limits:
  Per Transaction: $10.00
  Daily: $100.00 (used: $2.50)
Enter fullscreen mode Exit fullscreen mode

Part 2: Accepting Payments (Server Mode)

Want your agent to get paid? Here's how.
i.e. Build a GPT-4 summarize service.

Step 1: Create the moltspay.services.json configuration file

cat > moltspay.services.json << 'EOF'
{
  "provider": {
    "name": "Summarize Service",
    "wallet": "your wallet address"
  },
  "services": [
    {
      "id": "summarize",
      "name": "Text Summarizer",
      "description": "Summarize any text using GPT-4",
      "price": 0.05,
      "currency": "USDC",
      "chains": ["base", "polygon", "solana"],
      "function": "summarizeText"
    }
  ]
}
EOF
Enter fullscreen mode Exit fullscreen mode

MoltsPay Service Configuration Field Descriptions

  • wallet address: Provider’s wallet address to receive crypto payments from agent calls, core account for value transfer.

  • name service id: Unique service ID, global identifier in MoltsPay ecosystem for precise agent service invocation.

  • description: Service function overview, clarifying core capabilities and scenarios for callers to understand value.

  • price: Per-call pricing, defines fees (in crypto like USDC) for agent access, basis for commercial billing.

  • function: Backend function name, entry point for service logic, used by MoltsPay to trigger execution.

Step 2: Implement Your Service

Build index.js service code with runnable GPT-4 logic

cat > index.js << 'EOF'
import OpenAI from 'openai';

// Initialize OpenAI client
const openai = new OpenAI({
  apiKey: 'your api key',
});

async function callGPT4(text, maxLength) {
  const response = await openai.chat.completions.create({
    model: 'gpt-4',
    messages: [
      {
        role: 'user',
        content: `Summarize the following text, max length: ${maxLength} characters:\n\n${text}`
      }
    ],
    max_tokens: maxLength * 2,
  });
  return response.choices[0].message.content.trim();
}

export async function summarizeText({ text, maxLength = 100 }) {
  const summary = await callGPT4(text, maxLength);

  return {
    summary,
    originalLength: text.length,
    summaryLength: summary.length
  };
}
EOF
Enter fullscreen mode Exit fullscreen mode

Step 3: Start the Server

npx moltspay start . --port 8402
Enter fullscreen mode Exit fullscreen mode

Your service is now live at http://localhost:8402 with:

  • /.well-known/agent-services.json - Service discovery
  • /services/summarize/execute - Payment + execution endpoint

Step 4: Verify It Works

curl http://localhost:8402/.well-known/agent-services.json
Enter fullscreen mode Exit fullscreen mode

Other agents can now discover and pay for your service.


Multi-Chain Support

MoltsPay works on 8 chains out of the box:

Chain Network ID Status
Base base βœ… Production
Base Sepolia base_sepolia πŸ§ͺ Testnet
Polygon polygon βœ… Production
BNB Chain bnb βœ… Production
BNB Testnet bnb_testnet πŸ§ͺ Testnet
Tempo tempo_moderato πŸ§ͺ Testnet
Solana solana βœ… Production
Solana Devnet solana_devnet πŸ§ͺ Testnet

Switch chains anytime:

npx moltspay pay https://service.com/api --chain polygon
Enter fullscreen mode Exit fullscreen mode

Or in code:

const client = new MoltsPay({ chain: 'polygon' });
Enter fullscreen mode Exit fullscreen mode

Testing with Testnet

Don't want to use real money? Use our testnet faucet:

npx moltspay faucet
Enter fullscreen mode Exit fullscreen mode

You'll get 1 USDC on Base Sepolia. Then:

npx moltspay pay https://juai8.com/zen7 text-to-video \
  --chain base_sepolia \
  --prompt "a dog on the moon"
Enter fullscreen mode Exit fullscreen mode

Full payment flow, zero cost.


Common Patterns

Pattern 1: Agent with Budget

import { MoltsPay } from 'moltspay';

const agent = new MoltsPay({
  maxPerTransaction: 5,  // Max $5 per service call
  maxPerDay: 50          // Max $50 daily spend
});

// Agent can now safely call services
const image = await agent.pay(imageService, 'generate', { prompt });
const summary = await agent.pay(textService, 'summarize', { text });
Enter fullscreen mode Exit fullscreen mode

Pattern 2: Multi-Service Orchestration

// Agent pays for multiple services in sequence
const research = await client.pay(searchService, 'search', { query });
const analysis = await client.pay(analysisService, 'analyze', { data: research });
const report = await client.pay(writerService, 'write', { outline: analysis });
Enter fullscreen mode Exit fullscreen mode

Pattern 3: Fallback Chains

// Try Base first, fall back to Polygon
const client = new MoltsPay({ 
  chains: ['base', 'polygon'],
  fallback: true 
});
Enter fullscreen mode Exit fullscreen mode

CLI Reference

Command Description
npx moltspay init Create new wallet
npx moltspay status Check balance and limits
npx moltspay config Set spending limits
npx moltspay fund Open funding page
npx moltspay faucet Get testnet USDC
npx moltspay pay <url> <service> Pay for a service
npx moltspay start <dir> Start payment server
npx moltspay validate <dir> Validate service config

Next Steps


Links

Top comments (0)