Dub: translate and speak
POST /api/dubAI-generated voice: tell listeners so.
Details
Translate a text into a target language and speak the translation, in one paid call. The translation runs on the /v1/chat/completions gateway tier and the speech on /api/tts (tier "standard", $0.140) or /api/tts-hd (tier "hd", $0.260); the price is the sum of the two. Answers the audio as raw bytes (mp3, opus, aac, flac, wav or pcm) with the translation, the detected source language and the models that served in the X-Dub-Translation header (base64 JSON); add json: 1 or format "json" for a JSON answer with the audio base64 in it. targetLanguage is a BCP-47 tag or an English name. The same ten standard voice names and named voices as the speech tiers. Text capped at 2000 chars, checked before any model call, and the cap applies to the translation. A failed step answers an error naming the step and is not charged on x402, card and credits. Some calls are paid before they run; a failed one can be retried free with the same payment, or is refunded. Model-backed.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
text | string | yes | Text to translate and speak (max 2000 chars; the cap also applies to the translation) Also accepted as content, str, string, input, body, data. Up to 2000 characters. |
targetLanguage | string | yes | Target language: a BCP-47 tag (es, pt-BR, zh-Hans, ja) or an English name (Spanish, Brazilian Portuguese) The example sends es. |
sourceLanguage | string | no | Optional source language, same forms; detected when omitted |
voice | string | no | Voice: alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer (default: alloy), or a named voice (george, sarah, adam, alice, bella, bill, brian, callum, charlie, chris, daniel, eric, harry, jessica, laura, liam, lily, matilda, river, roger, will) The example sends nova. |
format | string | no | Audio format: mp3, opus, aac, flac, wav, pcm (default: mp3), or "json" for a JSON answer carrying the audio base64 as mp3 The example sends mp3. |
tier | string | no | "standard" (default, speech on /api/tts) or "hd" (speech on /api/tts-hd, priced at that tier's sum) |
json | boolean | no | true (or ?json=1) answers JSON with the audio base64 instead of raw bytes The example sends true. |
Example request
curl -i -X POST https://agent402.tools/api/dub \
-H "Content-Type: application/json" \
-d '{"text":"Hello from Agent402! Every tool is paid per call.","targetLanguage":"es","voice":"nova","format":"mp3","json":true}'Watch a call get paid
POST /api/dubfrom $0.14 USDCPAYMENT-SIGNATUREon 14 chains200 OKAgent402-ReceiptEd25519Example response
A 200 carries raw bytes, not JSON, with Content-Type audio/mpeg, audio/ogg, audio/aac, audio/flac, audio/wav or audio/pcm: the file itself.
From an MCP client
catalog.call {
"slug": "dub",
"params": {
"text": "Hello from Agent402! Every tool is paid per call.",
"targetLanguage": "es",
"voice": "nova",
"format": "mp3",
"json": true
}
}Hosted connector (https://agent402.tools/mcp): paid in the call. Stdio package (npx -y agent402-mcp@0.20.0): pays from a wallet or AGENT402_CREDITS_KEY.
Errors and behavior
- Pay per request ($0.14 to $0.26, quoted from this body) over x402 or MPP. Wallet-only, model-backed: no proof-of-work tier; the same input can give a different result. A prepaid card-credits key (
Authorization: Bearer a402_...) also pays it. - Required:
textandtargetLanguage; a rejected input answers 4xx witherror,tool,expected,requiredandexample. - On this route, a call ending in any status of 400 or above is not charged over x402, MPP or a credits key. Some calls are paid before they run; a failed one can be retried free with the same payment, or is refunded. A Tempo push credential pays before the call; a failure is a refund owed.
- Sales tax: the price includes North Carolina sales tax for a card buyer billed to a North Carolina address.
- A bodyless
GETorHEADreturns the same 402 quote. - An
Idempotency-Keydoes not replay bytes: a retried paid call runs and is charged again.
Paid call (JavaScript agent)
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
client.setSpendControls?.({ maxAmountPerPayment: "$0.26" }); // refuse any quote above this tool's price
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/dub", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
"text": "Hello from Agent402! Every tool is paid per call.",
"targetLanguage": "es",
"voice": "nova",
"format": "mp3",
"json": true
}),
});Get a wallet or run npx -y agent402-mcp@0.20.0 wallet new.
The quote (Python)
import base64, json, urllib.error, urllib.request
req = urllib.request.Request("https://agent402.tools/api/dub", method="POST", data="{\"text\":\"Hello from Agent402! Every tool is paid per call.\",\"targetLanguage\":\"es\",\"voice\":\"nova\",\"format\":\"mp3\",\"json\":true}".encode(), headers={"Content-Type": "application/json"})
try:
urllib.request.urlopen(req)
except urllib.error.HTTPError as e:
quote = json.loads(base64.b64decode(e.headers["PAYMENT-REQUIRED"]))
print(e.code, [(a["network"], a["amount"]) for a in quote["accepts"]]) # the price, per chain, in base units