Agent templates

Start from an agent that already pays.

Four starting points that run against Unslept on Robinhood Chain mainnet. Create an agent account in the app, copy the environment from the Developers tab, and run.

TypeScript

Research agent

Discovers paid APIs in the x402 Directory and calls them, paying per request within a budget you set.

  • Node 20
  • @unslept/sdk
  • Directory

Runnpm install && npm start

Open the template
agent.ts
import { Unslept, UnsleptError } from "@unslept/sdk";
import { privateKeyToAccount } from "viem/accounts";

/**
 * Discovers paid endpoints in the x402 Directory and calls them, paying per request from the agent account.
 * Stops at BUDGET (atomic USDG) and never pays more than the listed price for a single call.
 */
const DIRECTORY = "https://www.unslept.xyz/api/directory";
const budget = BigInt(process.env.BUDGET || "50000");
const category = process.env.CATEGORY?.trim();

const unslept = new Unslept({
  agentAccount: process.env.UNSLEPT_AGENT_ACCOUNT,
  signer: privateKeyToAccount(process.env.UNSLEPT_SIGNER_KEY as `0x${string}`),
  network: "robinhood-chain",
});

type Entry = { name: string; url: string; price: string; category: string; online: boolean };
const { entries } = (await (await fetch(DIRECTORY)).json()) as { entries: Entry[] };
const targets = entries.filter((e) => e.online && (!category || e.category === category));
console.log(`Directory: ${entries.length} endpoints, ${targets.length} selected. Budget ${Number(budget) / 1e6} USDG.`);

let spent = 0n;
for (const e of targets) {
  const price = BigInt(e.price);
  if (spent + price > budget) {
    console.log(`Budget reached, skipping ${e.name}.`);
    continue;
  }
  try {
    const res = await unslept.fetch(e.url, {}, { maxAmount: price });
    spent += price;
    const body = await res.text();
    console.log(`\n${e.name} (${Number(price) / 1e6} USDG) -> ${res.status}\n${body.slice(0, 400)}`);
  } catch (err) {
    console.log(`\n${e.name} failed: ${err instanceof UnsleptError ? `${err.code}: ${err.message}` : String(err)}`);
  }
}

const b = await unslept.getBalances();
console.log(`\nSpent ${Number(spent) / 1e6} USDG. Balances:`, b);

MCP

Claude Desktop that pays

Gives Claude Desktop, or any MCP client, tools to pay x402 requests from its own agent account. Claude asks before each paid call.

  • Claude Desktop
  • @unslept/mcp
  • No code

RunMerge the config, fill in two values, restart Claude Desktop

Open the template
claude_desktop_config.json
{
  "mcpServers": {
    "unslept": {
      "command": "npx",
      "args": ["-y", "@unslept/mcp"],
      "env": {
        "UNSLEPT_AGENT_ACCOUNT": "0x...",
        "UNSLEPT_SIGNER_KEY": "0x...",
        "UNSLEPT_NETWORK": "robinhood-chain",
        "UNSLEPT_CHAIN_ID": "4663",
        "UNSLEPT_RPC_URL": "https://rpc.mainnet.chain.robinhood.com",
        "UNSLEPT_USDG": "0x5fc5360D0400a0Fd4f2af552ADD042D716F1d168",
        "UNSLEPT_ROUTER": "0x1FeAB3B8f88FB0667B686fDCB0206194e88c984b",
        "UNSLEPT_BUFFERS": "0xcD853fb30eCe3134Cf21e2f47B9e23eaf965F40D"
      }
    }
  }
}

Python

LangChain agent

A tool-calling LangChain agent with a fetch tool that pays x402 endpoints and respects the account's caps.

  • Python 3.10+
  • unslept
  • LangChain

Runpip install -r requirements.txt && python agent.py

Open the template
agent.py
"""LangChain agent with a tool that pays x402 requests from an Unslept agent account."""
import os

from eth_account import Account
from langchain.agents import create_agent

from unslept import Unslept
from unslept.langchain_tool import unslept_tool
from unslept.web3_chain import EthAccountSigner

unslept = Unslept(
    agent_account=os.environ["UNSLEPT_AGENT_ACCOUNT"],
    signer=EthAccountSigner(Account.from_key(os.environ["UNSLEPT_SIGNER_KEY"])),
    network="robinhood-chain",
)

agent = create_agent(
    model="openai:gpt-4o-mini",
    tools=[unslept_tool(unslept)],
    system_prompt=(
        "You can call paid HTTP APIs with the pay_x402_request tool. "
        "Paid endpoints are listed at https://www.unslept.xyz/api/directory. Spend as little as possible."
    ),
)

if __name__ == "__main__":
    result = agent.invoke({"messages": [{"role": "user", "content": "Get the live Unslept protocol snapshot and summarize it in two sentences."}]})
    print(result["messages"][-1].content)

For providers

Paid API endpoint

A Next.js route that charges agents 0.01 USDG per request with Unslept Pay. Deploy it and list it in the directory.

  • Next.js
  • @unslept/pay
  • Earn

Runnpm install @unslept/pay viem

Open the template
app/api/quote/route.ts
import { createPaywall } from "@unslept/pay";

const paywall = createPaywall({
  payTo: process.env.PAY_TO!,
  price: 10_000n, // 0.01 USDG
  secret: process.env.PAYWALL_SECRET!,
  description: "Example quote endpoint",
  rpcUrl: process.env.ROBINHOOD_RPC_URL,
});

export const GET = paywall.protect(async (_request, payment) => {
  return Response.json({ symbol: "EXAMPLE", price: 101.25, at: new Date().toISOString(), paidBy: payment.payer });
});

Put idle agent money to work.

Create an agent account in a few minutes, or supply the liquidity that settles payments.