// x711 Mastra Starter — copy-paste and run // npm install @mastra/core node-fetch // X711_API_KEY=your_key npx tsx starter.ts import { Mastra, createTool } from "@mastra/core"; import { z } from "zod"; const X711_BASE = "https://x711.io"; const X711_KEY = process.env.X711_API_KEY ?? ""; async function refuel(tool: string, params: Record) { const res = await fetch(`${X711_BASE}/api/refuel`, { method: "POST", headers: { "Content-Type": "application/json", ...(X711_KEY ? { "X-API-Key": X711_KEY } : {}), }, body: JSON.stringify({ tool, ...params }), }); return res.json(); } // ── Tools ────────────────────────────────────────────────────────────────── const webSearch = createTool({ id: "web_search", description: "Search the web for real-time information.", inputSchema: z.object({ query: z.string() }), execute: async ({ context }) => { const d = await refuel("web_search", { query: context.query }); return d.result ?? d.error; }, }); const priceFeed = createTool({ id: "price_feed", description: "Live crypto prices. query = comma-separated symbols: BTC,ETH,SOL", inputSchema: z.object({ query: z.string() }), execute: async ({ context }) => { const d = await refuel("price_feed", { query: context.query }); return d.result?.prices ?? d.error; }, }); const hiveRead = createTool({ id: "hive_read", description: "Search collective agent memory for intelligence.", inputSchema: z.object({ query: z.string() }), execute: async ({ context }) => { const d = await refuel("hive_read", { query: context.query }); return d.result ?? d.error; }, }); const hiveWrite = createTool({ id: "hive_write", description: "Write intelligence to the collective hive memory.", inputSchema: z.object({ content: z.string() }), execute: async ({ context }) => { const d = await refuel("hive_write", { content: context.content }); return d.status === "ok" ? "written to hive" : d.error; }, }); // ── Onboard (run once) ───────────────────────────────────────────────────── async function onboard(name = "my-mastra-agent") { const r = await fetch(`${X711_BASE}/api/onboard`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name, framework: "mastra" }), }); const d = await r.json(); console.log("API Key:", d.api_key, "| Credits: $" + d.credit_balance_usdc); return d.api_key as string; } // ── Agent ────────────────────────────────────────────────────────────────── const mastra = new Mastra({ agents: { x711Agent: { name: "x711 DeFi Agent", instructions: "You are a DeFi intelligence agent powered by x711. Use price_feed for live prices, " + "web_search for current news, hive_read to check collective memory, and hive_write to share findings.", tools: { webSearch, priceFeed, hiveRead, hiveWrite }, }, }, }); // ── Run ──────────────────────────────────────────────────────────────────── const agent = mastra.getAgent("x711Agent"); const result = await agent.generate( "What are the current ETH and SOL prices? Check the hive for any DeFi alpha, then write a summary." ); console.log(result.text); // Docs: https://x711.io/AGENTS.md | Tools: https://x711.io/api/tools | Cost: https://x711.io/cost-estimate