Using a translation API from Python
In Python, a translation API is one requests.post with your text and the target language; the work is in batching, caching and error handling around it. Our Translate API takes {"text": ..., "to": ...}, returns the translation as JSON and costs $0.01 per call for up to 5,000 characters. For offline use, Argos Translate runs locally, and the big clouds have official Python client libraries.
Try it free Translate Text API — natural translation into any language · $0.01 / call
The basic call
requests gets the 402 but can't pay it. Any x402 v2 client, for example Coinbase's x402 packages or Coinbase AgentKit with the iamai-agentkit plugin, signs $0.01 in USDC on Base and repeats the request with the PAYMENT-SIGNATURE header. You need a wallet with USDC on Base for that; the 200 answer has result.translation, result.from, result.to and result.characters.
import requests
URL = "https://iamai.markets/api/x402/services/tu4zsEbHxedw"
r = requests.post(URL, json={"text": "Thank you for your order.", "to": "Spanish"})
print(r.status_code) # 402: the price is in r.json()["accepts"][0]
Batch short strings to pay less
Calls are priced per call, not per character, so pack many short strings into one call. Line breaks are kept, so one string per line maps back cleanly.
def batches(strings, limit=5000):
batch, size = [], 0
for s in strings:
if batch and size + len(s) + 1 > limit:
yield batch
batch, size = [], 0
batch.append(s)
size += len(s) + 1
if batch:
yield batch
# For each batch: send "\n".join(batch) as text,
# then translation.split("\n") gives the lines back in order.
Cache and check what you get back
- Keep a dict or a small SQLite table keyed by (text, target language). Repeated strings cost nothing the second time.
- Check the line count matches before splitting a batch. If it doesn't, resend those lines one by one.
- Keep each string under 5,000 characters; longer text needs splitting at paragraph breaks.
- A failed call isn't charged, so a retry after a short wait is safe.
Python options compared
| Option | Setup | Trade-off |
|---|---|---|
| google-cloud-translate (official client) | Cloud project, credentials | Per-character billing, many languages |
| deepl (official Python package) | DeepL account and key | Strong quality, plan limits |
| argostranslate | pip install plus language packs | Free and offline; lower quality, CPU heavy |
| Translate API (iamai.markets) with requests | No account; an x402 client and USDC wallet | $0.01 per call up to 5,000 characters |
The product
Questions
Can I set the source language?
Yes, add "from": "English" (or a code). Without it the source is detected and returned in result.from.
Will it break my placeholders like {name}?
Names, numbers, URLs and code are kept as written, and placeholders usually survive. Check them in your tests.
Can I test without a wallet?
Yes, by hand on the listing page: 5 free translations a day.
Related
Updated 2026-12-01
