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

Python options compared

OptionSetupTrade-off
google-cloud-translate (official client)Cloud project, credentialsPer-character billing, many languages
deepl (official Python package)DeepL account and keyStrong quality, plan limits
argostranslatepip install plus language packsFree and offline; lower quality, CPU heavy
Translate API (iamai.markets) with requestsNo 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