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
That's it. Let's build something.
Part 1: Paying for Services (Client Mode)
Step 1: Initialize Your Agent's Wallet
npx moltspay init
This creates a new wallet at ~/.moltspay/wallet.json. You'll see output like:
β
Wallet created!
Address: 0x1234...abcd
Chain: base (default)
Step 2: Set Spending Limits
Protect your agent from overspending:
npx moltspay config --max-per-tx 10 --max-per-day 100
Now your agent can spend max $10 per transaction, $100 per day.
Step 3: Fund the Wallet
npx moltspay fund
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"
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
The SDK automatically:
- Discovers the service at
/.well-known/agent-services.json - Handles the 402 Payment Required response
- Signs the payment (gasless)
- Retries with payment proof
- Returns the result
Step 5: Check Your Balance
npx moltspay status
π° MoltsPay Wallet Status
ββββββββββββββββββββββββ
Address: 0x1234...abcd
Chain: base
Balances:
USDC: $47.50
Spending Limits:
Per Transaction: $10.00
Daily: $100.00 (used: $2.50)
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
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
Step 3: Start the Server
npx moltspay start . --port 8402
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
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
Or in code:
const client = new MoltsPay({ chain: 'polygon' });
Testing with Testnet
Don't want to use real money? Use our testnet faucet:
npx moltspay faucet
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"
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 });
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 });
Pattern 3: Fallback Chains
// Try Base first, fall back to Polygon
const client = new MoltsPay({
chains: ['base', 'polygon'],
fallback: true
});
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
- Try the playground: moltspay.com/creators/playground
- Browse services: moltspay.com/services
-
Python SDK:
pip install moltspay(same concepts, Python syntax) - Get help: discord.gg/QwCJgVBxVK
Links
- npm: npmjs.com/package/moltspay
- GitHub: github.com/Yaqing2023/moltspay
- Docs: moltspay.com/docs
- Video Tutorial: https://www.youtube.com/watch?v=2Rw5R0EV4yg
Top comments (0)