// MoneyTools Transcripts: pay-per-file speech-to-text over x402 (no prepaid credits). // dTelecom-compatible API (POST /v1/transcriptions/jobs), 0.005 USDC per started audio minute on Base. // // npm i @x402/fetch @x402/evm viem // export X402_PRIVATE_KEY=0x... # Base wallet holding a little USDC (no ETH/gas needed) // npx tsx transcribe.ts meeting.mp3 # local file (multipart upload) // npx tsx transcribe.ts https://host/ep.mp3 # direct media URL (we fetch it) // MAX_USD=0.10 FORMAT=srt npx tsx transcribe.ts meeting.mp3 > meeting.srt // // Flow: POST -> 402 with the exact price for THIS file -> @x402/fetch signs a USDC authorization and // resends the same request (same Idempotency-Key => stored upload reused) -> 202 {id, job_token} -> poll. import { readFile } from "node:fs/promises"; import { basename } from "node:path"; import { wrapFetchWithPayment, x402Client } from "@x402/fetch"; import { ExactEvmScheme } from "@x402/evm"; import { privateKeyToAccount } from "viem/accounts"; const BASE = (process.env.TRANSCRIPTS_BASE ?? "https://speech.moneytools.dev").replace(/\/+$/, ""); const MAX_ATOMIC = BigInt(Math.round(Number(process.env.MAX_USD ?? "0.50") * 1e6)); // USDC 6 decimals const FORMAT = process.env.FORMAT ?? "json"; // json | srt | vtt | txt const key = process.env.X402_PRIVATE_KEY as `0x${string}` | undefined; if (!key) throw new Error("set X402_PRIVATE_KEY (a Base wallet with USDC)"); const client = new x402Client() .register("eip155:8453", new ExactEvmScheme(privateKeyToAccount(key))) .registerPolicy((_v, reqs) => reqs.filter((r) => BigInt(r.amount) <= MAX_ATOMIC)); // spend cap per file const payFetch = wrapFetchWithPayment(fetch, client); async function submit(src: string): Promise { const url = `${BASE}/v1/transcriptions/jobs`; if (src.startsWith("https://")) { return payFetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ url: src }) }); } // Multipart, exactly like dTelecom (file, language, diarize). Keep the Idempotency-Key for the paid retry. const form = new FormData(); form.append("file", new Blob([await readFile(src)]), basename(src)); return payFetch(url, { method: "POST", headers: { "Idempotency-Key": `ts-${crypto.randomUUID()}` }, body: form }); } async function main() { const src = process.argv[2]; if (!src) throw new Error("usage: transcribe.ts "); const r = await submit(src); if (r.status !== 202) { console.error(`HTTP ${r.status}:`, (await r.text()).slice(0, 500)); process.exit(1); } const job = await r.json(); console.error(`paid ${job.payment.amountUsd} USDC (tx ${job.payment.transaction}); job ${job.id}`); const auth = { Authorization: `Bearer ${job.job_token}` }; // shown once: keep it let st: any; for (;;) { st = await (await fetch(job.status_url, { headers: auth })).json(); console.error(` ${st.status}`, st.moneytools?.progress ?? ""); if (st.status === "completed" || st.status === "failed") break; await new Promise((res) => setTimeout(res, (job.poll_every_seconds ?? 15) * 1000)); } if (st.status === "failed") throw new Error(`job failed: ${st.error}`); console.log(FORMAT === "json" ? st.result.text : await (await fetch(`${job.status_url}?format=${FORMAT}`, { headers: auth })).text()); } main().catch((e) => { console.error(e?.message ?? e); process.exit(1); });