Google Translate API: Text Translation as JSON
To translate text with Google Translate as JSON, call GET /v1/google-translate/translate with three required parameters: source (a language code or auto), text and target. The recorded call with source=en, text=Hello and target=de returned {"translated_text": "Hallo"}. Each successful call costs 1 credit and translates one string into one language. This guide covers the request, the language codes, the response, a cached batch translator in Python and what a localization job costs.
What does the Google Translate endpoint return?
The Google Translate API on Perceptlayer has one endpoint. You send a string, the language it's in and the language you want, and you get the translated string back as JSON. It's a GET request, it costs 1 credit per successful call, and it has no optional parameters, so there is nothing to tune: every call is one string, one source language and one target language.
| Method | Path | Returns | Credits | Required | Optional |
|---|---|---|---|---|---|
GET |
/v1/google-translate/translate |
The translated text | 1 | source, text, target |
none |
The data is collected from publicly available sources and returned as found at the time of the call. This isn't Google's own developer product, and this guide makes no claims about that one. If you're weighing the two, read Google's own documentation and terms and compare limits, pricing and output yourself. The Google Translate API page has the endpoint and a playground where you can run it with your key.
How do you send a translation request?
Send a GET request to https://api.perceptlayer.com/v1/google-translate/translate with the three parameters in the query string and your key in the X-API-Key header. This is the request behind the recorded response in this guide. Keys start with sk_live_, and where you can't set a header, the key can go in the api_key query parameter instead.
curl "https://api.perceptlayer.com/v1/google-translate/translate?source=en&text=Hello&target=de" \
-H "X-API-Key: $API_KEY"
Text with spaces, punctuation or non-ASCII characters has to be URL-encoded, because it travels in the query string. With cURL, --get and --data-urlencode do the encoding for you:
curl --get "https://api.perceptlayer.com/v1/google-translate/translate" \
--data-urlencode "source=en" \
--data-urlencode "target=fr" \
--data-urlencode "text=Your order has shipped." \
-H "X-API-Key: $API_KEY"
In Python, requests encodes a params dictionary the same way. The authentication docs cover both ways of sending the key, and the requests and responses docs cover the request format in general.
Which parameters does the endpoint take?
The endpoint takes exactly three parameters, all required. The table lists what the API documents about each; nothing else is documented, including a maximum length for text, so test long inputs in the playground before you depend on them.
| Parameter | Type | Required | Documented values |
|---|---|---|---|
source |
string | yes | auto or one of 195 language codes |
text |
string | yes | None documented |
target |
string | yes | One of the same 195 language codes; auto is not allowed |
The asymmetry matters. source=auto is allowed, so you can send text without knowing its language. target has no auto, because you always have to say which language you want back.
Which language codes are accepted?
source and target share a list of 195 language codes, and source adds auto. Most are short codes such as en, de, fr, es, ja, ko, ar and hi. Some carry a region or script suffix, such as zh-CN, zh-TW, fr-CA, pt-PT, ms-Arab, pa-Arab and mni-Mtei, so pt and pt-PT are two separate values. The full list is on the translate endpoint page.
A few pairs in the list look like aliases: iw and he, jw and jv, tl and fil. The API doesn't document whether each pair returns the same output, so pick one code per language and use it everywhere. That keeps your cache keys consistent too. A value that isn't in the list is a parameter error, which comes back as invalid_parameters (422) and costs nothing.
What does a translation response contain?
The recorded response is one JSON object with one field, translated_text, holding the translated string. This is the full recorded body for text=Hello, source=en, target=de:
{
"translated_text": "Hallo"
}
That's all the recording shows. There is no detected source language, no confidence score and no alternative translations in it, so code should read translated_text and treat anything else as unknown. If you send source=auto, you won't learn from this recording which language was detected; if you need that, record the source language on your side before you call.
The credit cost isn't in the body either, but it's in the headers. Every successful response carries X-Credits-Used (what the call cost) and X-Credits-Remaining (your balance after it), plus RateLimit-Remaining and RateLimit-Reset for the current minute. Read those headers if a batch job needs to stop before the balance runs out.
How do you translate a batch of strings in Python?
A batch translator loops over your strings and target languages, calls the endpoint once per pair, and stores each result. Because every call costs 1 credit, the most useful thing it can do is not repeat itself: a cache keyed on (source, target, text) means a string you translated last week is free this week. The script below uses requests and the standard library's sqlite3 and csv, and reads only translated_text.
import csv
import os
import sqlite3
import time
import requests
API_KEY = os.environ["API_KEY"]
URL = "https://api.perceptlayer.com/v1/google-translate/translate"
SOURCE = "en"
TARGETS = ["de", "fr", "es", "ja"]
db = sqlite3.connect("translations.db")
db.execute(
"CREATE TABLE IF NOT EXISTS t (source TEXT, target TEXT, text TEXT, out TEXT, "
"PRIMARY KEY (source, target, text))"
)
def translate(text, target, attempts=3):
row = db.execute(
"SELECT out FROM t WHERE source=? AND target=? AND text=?", (SOURCE, target, text)
).fetchone()
if row:
return row[0]
for _ in range(attempts):
r = requests.get(
URL,
params={"source": SOURCE, "text": text, "target": target},
headers={"X-API-Key": API_KEY},
timeout=60,
)
if r.status_code in (429, 503):
time.sleep(int(r.headers.get("Retry-After", "5")))
continue
r.raise_for_status()
out = r.json().get("translated_text")
db.execute("INSERT INTO t VALUES (?, ?, ?, ?)", (SOURCE, target, text, out))
db.commit()
time.sleep(1)
return out
raise RuntimeError(f"gave up on {target}: {text[:40]}")
with open("strings.csv", newline="") as f:
strings = [row[0] for row in csv.reader(f) if row]
with open("translated.csv", "w", newline="") as f:
out = csv.writer(f)
out.writerow(["text"] + TARGETS)
for s in strings:
out.writerow([s] + [translate(s, t) for t in TARGETS])
The one-second pause after each paid call keeps a free account within its 1 request a second; on a paid pack you can shorten it. Cached strings skip both the call and the pause. Run this against the recorded call in the playground first and confirm the response shape matches before you translate a whole file.
What should the script do with errors?
Every error returns an HTTP status and a JSON body with code, message and request_id, and costs nothing. Retry rate_limited (429) and temporarily_unavailable (503) after the seconds in Retry-After, as the script does. Treat invalid_parameters (422) as a bug: usually a language code that isn't in the list. insufficient_credits (402) means the balance has run out, and nothing more is debited. Requests have no idempotency key, so a client timeout can hide a call that succeeded and was billed; keep the timeout generous and let the cache absorb reruns. The errors docs list every code.
What does a translation job cost?
Every successful call costs 1 credit, and one call translates one string into one language. So the cost of a job is the number of unique strings times the number of target languages, minus whatever your cache already holds. Failed calls cost nothing and return X-Credits-Used: 0.
| Job | Arithmetic | Credits |
|---|---|---|
| Try it out | 100 strings × 1 language × 1 credit | 100 |
| App UI strings | 500 strings × 5 languages × 1 credit | 2,500 |
| Product catalogue | 2,000 strings × 4 languages × 1 credit | 8,000 |
| Help centre | 5,000 strings × 8 languages × 1 credit | 40,000 |
The 100 free credits on signup cover the first row: 100 translations, enough to check output quality in the languages you care about. The next two fit the Starter pack's 10,000 credits, and the help centre fits the Build pack's 50,000. Purchased credits last 12 months, with no subscription. See pricing for what credits cost and the credits docs for how calls are debited.
Speed is set by the request rate, not the cost. A free account runs 1 request a second, so the 2,500-call UI job takes about 42 minutes. Paying accounts run at least 5 requests a second, which brings it under 9 minutes. The rate limits docs list the rate each pack includes.
Can an AI agent call the translate endpoint?
Yes. The same key works through the MCP server at https://perceptlayer.com/mcp, which gives an agent three tools: search_endpoints and get_endpoint look up the catalogue for free, and call_endpoint runs an endpoint with its parameters and spends its credits only on success. An agent can look up the translate endpoint with search_endpoints, then call it with source, text and target like any other tool.
In Claude Code, one command adds the server:
claude mcp add --transport http perceptlayer https://perceptlayer.com/mcp --header "Authorization: Bearer $API_KEY"
call_endpoint reports the credits used and remaining with each result, so an agent that translates as part of a larger task can see what it's spending. The AI agents docs cover the setup for other clients.
To try it now, run source=en, text=Hello, target=de in the playground on the translate endpoint page, then get a free API key and point the script at your own strings. More guides like this one are in API guides, and other search engine sources are on the search engines APIs page.
APIs in this post
Frequently asked questions
Is this Google's official translation API?
No. It's a Perceptlayer endpoint that returns Google Translate output, collected from publicly available sources and returned as found at the time of the call. This guide makes no claims about Google's own developer offering, its quotas or its pricing. If you're choosing between the two, read Google's documentation and terms and compare them yourself.
How much does one translation cost?
One successful call costs 1 credit and translates one string into one language. Translating 500 strings into 5 languages is 2,500 calls, or 2,500 credits. Failed calls cost nothing and return X-Credits-Used: 0. The 100 free credits on signup cover 100 translations. See the pricing page for what credits cost.
Can I translate into several languages in one call?
No. target takes a single language code, and the endpoint has no optional parameters, so each call returns one translation. To translate a string into four languages, make four calls, which costs 4 credits. Cache each result by source, target and text so repeat runs don't pay again.
Does the response say which language was detected?
Not in the recorded response. It contains only translated_text, and the recording used source=en rather than auto. Run a call with source=auto in the playground to see what comes back, and until then, don't build on a detected-language field. If you need the source language, record it on your side.
Is there a limit on how much text I can send?
The API doesn't document a maximum length for text. Because the text goes in the query string, very long inputs are better split into sentences or paragraphs, which also caches better. Test your longest strings in the playground first. Account request rates apply: 1 request a second on a free account, at least 5 on a paid pack.