Google Translate API Usage Guide
Categories:
If you need to automate translation tasks via API, the Google Translate API is a solid choice. Its translation quality may be slightly behind DeepL, but it offers better value—especially with 500,000 free characters every month.
Product Overview
Everyone has used Google Translate. Here we’re talking about its API service, officially called Google Cloud Translation. With the API you can do bulk translation, build custom models, translate documents, and more.
Pricing
500,000 characters free per month. Beyond that, you pay per character.
Basic vs Advanced
Feature | Basic | Advanced |
---|---|---|
Free Monthly Quota | 500,000 | 500,000 |
Cost per 1 M chars | $20 | $80 |
Document Price | $0.08 / pg | $0.25 / pg |
Custom Models | ✘ | ✔ |
Getting Started
- Enable the API. If billing isn’t set up, you’ll be prompted to add a foreign-currency credit card.
$cred = gcloud auth print-access-token
$project_id = "example"
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://cloudresourcemanager.googleapis.com/v3/projects/${project_id}" | Select-Object -Expand Content
- Quick test
$cred = gcloud auth print-access-token
$project_id = "example"
$body = @{
"sourceLanguageCode" = "en"
"targetLanguageCode" = "zh"
"contents" = @("Hello, world!")
"mimeType" = "text/plain"
}
$body = $body | ConvertTo-Json
$headers = @{
"Authorization" = "Bearer $cred"
"Content-Type" = "application/json; charset=utf-8"
"x-goog-user-project" = $project_id
}
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://translation.googleapis.com/v3/projects/${project_id}:translateText" `
-Body $body | Select-Object -Expand Content
On Linux, use curl:
export CRED=$(gcloud auth print-access-token)
export PROJECT_ID="example"
export SOURCE_LANGUAGE_CODE="en"
export TARGET_LANGUAGE_CODE="zh"
export CONTENTS="Hello, world!"
export MIME_TYPE="text/plain"
curl -X POST -H "Authorization: Bearer $CRED" -H "Content-Type: application/json; charset=utf-8" -H "x-goog-user-project: $PROJECT_ID" -d "{
\"sourceLanguageCode\": \"$SOURCE_LANGUAGE_CODE\",
\"targetLanguageCode\": \"$TARGET_LANGUAGE_CODE\",
\"contents\": [\"$CONTENTS\"],
\"mimeType\": \"$MIME_TYPE\"
}" "https://translation.googleapis.com/v3/projects/$PROJECT_ID:translateText"
You now have everything you need to run bulk translations via the Google Translate API.
Common Use Cases
- Translate websites or apps
- Train custom translation models
- Add multilingual subtitles to videos
- Provide multilingual voiceovers
- Translate richly formatted documents
- Translate customer interactions in real time
Further Reading
- Cloud Translation Docs
- Authenticate with Cloud Translation
- Authenticate with API Keys
- Source and target languages use ISO-639 codes
- Custom Translation Overview
Closing Notes
Google’s official documentation can be verbose; there are often several ways to achieve the same goal. This guide picks the simplest and most recommended flow for typical users.
- We used local authentication (gcloud CLI)
- We relied on the REST API with Curl/Invoke-WebRequest
- And we opted for the Advanced tier
Originally published at blog.jqknono.dev, reproduction without permission is prohibited.