DEV Community

Yaqing2023
Yaqing2023

Posted on

Build Your First Paid Agent Service in 10 Minutes

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


Step 1: Install MoltsPay

npm install -g moltspay@latest
Enter fullscreen mode Exit fullscreen mode

Verify:

npx moltspay --version
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Your Skill

Make a directory:

mkdir haiku-service && cd haiku-service
npm init -y
Enter fullscreen mode Exit fullscreen mode

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'
  };
}
Enter fullscreen mode Exit fullscreen mode

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
        }
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Edit ~/.moltspay/.env:

CDP_API_KEY_ID=your_key_id
CDP_API_KEY_SECRET=your_key_secret
Enter fullscreen mode Exit fullscreen mode

Step 5: Validate

npx moltspay validate .
Enter fullscreen mode Exit fullscreen mode

Should see:

βœ… Config valid
βœ… Function 'generateHaiku' found
βœ… Wallet address valid
βœ… Ready to start
Enter fullscreen mode Exit fullscreen mode

Step 6: Start Server

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

Your service is live at http://localhost:3000

Check it:

curl http://localhost:3000/services | jq .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Fund it with some USDC (even $1 works for testing).

Then:

npx moltspay pay http://localhost:3000 generate-haiku --topic "cat"
Enter fullscreen mode Exit fullscreen mode

You should get:

{
  "haiku": "Soft paws on keyboard\nDeleting your important work\nPurrs without remorse",
  "topic": "cat"
}
Enter fullscreen mode Exit fullscreen mode

And $0.10 USDC lands in your wallet. πŸŽ‰


What Just Happened?

  1. Client called your service
  2. Server returned 402 Payment Required
  3. Client signed a payment permit (no gas!)
  4. CDP settled the payment on-chain
  5. Server verified payment
  6. Server executed your function
  7. Client got the result

All automatic. All gasless.


Deploy to Production

For real traffic, deploy somewhere persistent:

Railway (easiest):

railway init
railway up
Enter fullscreen mode Exit fullscreen mode

Or any Node.js host. Just make sure port 3000 is exposed.


Links


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)