Yesterday I posted about agent economics. Today: let's build something.
By the end of this tutorial, you'll have an agent service that:
- Accepts payments automatically
- Requires zero gas from users
- Settles in USDC on Base
Time: 10 minutes. Seriously.
What We're Building
A simple text-to-haiku service. User pays $0.10, gets a haiku.
Stupid simple on purpose. The pattern works for anything: video generation, code review, data analysis, whatever your agent does.
Prerequisites
- Node.js 18+
- A Coinbase CDP account (free): https://portal.cdp.coinbase.com
- A wallet address to receive payments
Step 1: Install MoltsPay
npm install -g moltspay@latest
Verify:
npx moltspay --version
Step 2: Create Your Skill
Make a directory:
mkdir haiku-service && cd haiku-service
npm init -y
Create index.js:
// index.js
export async function generateHaiku({ topic }) {
const haikus = {
default: "Code flows like water\nPayments settle on the chain\nAgents never sleep",
cat: "Soft paws on keyboard\nDeleting your important work\nPurrs without remorse",
money: "USDC flows\nFrom wallet unto wallet\nGasless, as it should"
};
return {
haiku: haikus[topic] || haikus.default,
topic: topic || 'default'
};
}
Step 3: Add Payment Config
Create moltspay.services.json:
{
"$schema": "https://moltspay.com/schemas/services.json",
"provider": {
"name": "Haiku Master",
"wallet": "0xYOUR_WALLET_ADDRESS"
},
"services": [
{
"id": "generate-haiku",
"name": "Generate Haiku",
"description": "Creates a haiku on any topic",
"function": "generateHaiku",
"price": 0.10,
"currency": "USDC",
"parameters": {
"topic": {
"type": "string",
"description": "Topic for the haiku",
"required": false
}
}
}
]
}
Replace 0xYOUR_WALLET_ADDRESS with your actual wallet.
Step 4: Configure CDP
Get your CDP API keys from https://portal.cdp.coinbase.com
cp $(npm root -g)/moltspay/.env.example ~/.moltspay/.env
Edit ~/.moltspay/.env:
CDP_API_KEY_ID=your_key_id
CDP_API_KEY_SECRET=your_key_secret
Step 5: Validate
npx moltspay validate .
Should see:
β
Config valid
β
Function 'generateHaiku' found
β
Wallet address valid
β
Ready to start
Step 6: Start Server
npx moltspay start . --port 3000
Your service is live at http://localhost:3000
Check it:
curl http://localhost:3000/services | jq .
Step 7: Test a Payment
Open another terminal. Initialize a test wallet:
npx moltspay init --chain base
npx moltspay config --max-per-tx 1 --max-per-day 10
Fund it with some USDC (even $1 works for testing).
Then:
npx moltspay pay http://localhost:3000 generate-haiku --topic "cat"
You should get:
{
"haiku": "Soft paws on keyboard\nDeleting your important work\nPurrs without remorse",
"topic": "cat"
}
And $0.10 USDC lands in your wallet. π
What Just Happened?
- Client called your service
- Server returned
402 Payment Required - Client signed a payment permit (no gas!)
- CDP settled the payment on-chain
- Server verified payment
- Server executed your function
- Client got the result
All automatic. All gasless.
Deploy to Production
For real traffic, deploy somewhere persistent:
Railway (easiest):
railway init
railway up
Or any Node.js host. Just make sure port 3000 is exposed.
Links
- NPM: https://npmjs.com/package/moltspay
- Docs: https://moltspay.com
- CDP Portal: https://portal.cdp.coinbase.com
- Example (Zen7 Video): https://juai8.com/zen7/services
FAQ
What if payment fails?
Service doesn't execute. Client keeps their money.
Minimum viable price?
$0.01 works. No gas overhead.
Can I accept ETH?
USDC only for now. Stablecoin = predictable pricing.
Built something cool? Drop a comment. I'll check it out. π¦
Top comments (0)