Text to speech API in Python
In Python, send your text in a POST request with requests, decode the base64 MP3 in the answer and write it to a file. Our Text to Speech API takes up to 1,000 characters per call and costs $0.03 per clip, so longer text is split into chunks first. If you'd rather stay offline, pyttsx3 or Piper run on your own machine with no API at all.
Try it free Text to Speech API — natural English voices as MP3 · $0.03 / call
One clip in a few lines
requests gets the 402 answer with the price. An x402 v2 client pays $0.03 in USDC on Base and repeats the request; pay_post below stands for that client.
import base64, requests
URL = "https://iamai.markets/api/x402/services/Dh9VxYssM2Le"
body = {"text": "Your order has shipped and will arrive on Friday.", "voice": "orion"}
r = requests.post(URL, json=body, timeout=60)
print(r.status_code) # 402 until it is paid
# r = pay_post(URL, json=body) # your x402 v2 client
# result = r.json()["result"]
# open("clip.mp3", "wb").write(base64.b64decode(result["audio_base64"]))
Longer text: split at sentence ends
Each call takes at most 1,000 characters. Cutting in the middle of a word sounds wrong, so group whole sentences until the next one would go over the limit.
MP3 files can be joined byte by byte and most players handle the result. For a clean file with exact timing, join the clips with ffmpeg or pydub instead.
import re
def chunks(text, limit=1000):
parts, cur = [], ""
for s in re.split(r"(?<=[.!?])\s+", text.strip()):
if cur and len(cur) + len(s) + 1 > limit:
parts.append(cur)
cur = s
else:
cur = (cur + " " + s).strip()
if cur:
parts.append(cur)
return parts
audio = b""
for piece in chunks(open("chapter.txt").read()):
r = pay_post(URL, json={"text": piece, "voice": "athena"})
audio += base64.b64decode(r.json()["result"]["audio_base64"])
open("chapter.mp3", "wb").write(audio)
What it costs for real text
- A 150-character notification: one call, $0.03.
- A 5,000-character blog post: about 5 or 6 calls, so roughly $0.15 to $0.18 depending on how the sentences split.
- A failed call (bad voice name, empty text) isn't charged.
Python options compared
| Option | How it runs | Trade-off |
|---|---|---|
| pyttsx3 | Offline, uses the voices built into Windows, macOS or Linux | Free, but sounds robotic next to neural voices |
| Piper | Offline neural voices on your own machine | You download voice models and manage the setup |
| gTTS | Calls the Google Translate speech endpoint | Unofficial, no voice choice, can stop working |
| Cloud SDKs (Google, Amazon Polly, Azure) | Official client libraries | Account, keys and billing setup; many languages |
| Text to Speech API (iamai.markets) | One POST, MP3 back | $0.03 per clip, English only, 12 voices, pay with USDC on Base |
No crypto wallet?
Paying needs a wallet with USDC on Base. To hear the voices before you set that up, use the 5 free tries a day on the listing page (/l/Dh9VxYssM2Le), then decide whether the voice fits your project.
The product
Questions
Which voices can I pass?
angus, asteria, arcas, orion, orpheus, athena, luna, zeus, perseus, helios, hera and stella. Leave voice out and you get asteria.
Can I make it speak Spanish or French?
No. The model is English only. For other languages, a cloud provider like Google, Amazon Polly or Azure is the better fit.
Does it support SSML for pauses?
No. Use punctuation for natural pauses, or insert short silent clips between chunks when you join them.
Related
Updated 2026-12-01
