Build a Claude API Translation App: Full Tutorial
Building a translation app with the Claude API means sending text to Claude with a well-structured prompt, parsing the response, and wiring that into a frontend users can type into. Unlike dedicated translation APIs, Claude understands context, tone, and idioms, so it handles slang, technical jargon, and ambiguous phrasing better than traditional machine translation — at the cost of a bit more prompt engineering on your end.
This tutorial walks through the whole flow: designing a translation prompt, handling multiple languages, streaming responses for a responsive UI, and deploying the thing so other people can actually use it. By the end you'll have a working app and a clear understanding of the tradeoffs involved in using an LLM instead of a dedicated translation service.
Why use Claude for translation instead of a dedicated API
Dedicated translation APIs (Google Translate, DeepL) are fast and cheap for literal translation. Claude is a better fit when you need:
- Context-aware translation — preserving tone, formality, or brand voice
- Domain-specific accuracy — legal, medical, or technical text where word choice matters
- Format preservation — translating inside JSON, Markdown, or code comments without breaking structure
- Combined tasks — translate and summarize, translate and extract entities, translate and rewrite for a different audience, all in one call
If you just need "translate this sentence to Spanish" at scale with no nuance, a dedicated API is cheaper. If quality and context matter, Claude is worth the extra setup.
Step 1: Design the translation prompt
The core of a translation app is the prompt. A naive prompt like "Translate this to French: {text}" works but is fragile — Claude might add explanations, quotes, or commentary you don't want.
A more reliable system prompt:
You are a professional translator. Translate the user's text into {target_language}.
Rules:
- Output only the translation, no explanations or notes.
- Preserve formatting (line breaks, markdown, punctuation).
- Preserve the original tone (formal/informal).
- If the text contains a mix of languages, translate only the parts not already in {target_language}.
Keep the actual text to translate in the user message, not the system prompt. This separation makes it easy to swap target languages without touching the instructions.
Step 2: Call the API
Here's a basic request using the Messages API shape:
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"system": "You are a professional translator. Translate into Japanese. Output only the translation.",
"messages": [
{"role": "user", "content": "Could you send me the invoice by Friday?"}
]
}'
If you're building this as a product you expose to other users — a SaaS feature, an internal tool, a client-facing app — you probably don't want to manage raw Anthropic API keys, rate limits, and billing per customer yourself. This is where a layer like SubToAPI helps: it turns your Claude access into a standard HTTPS API with scoped sub_live_... keys per app or customer, so you can issue a key per client and track usage without building that infrastructure. The request shape is nearly identical:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"system": "You are a professional translator. Translate into Japanese. Output only the translation.",
"messages": [
{"role": "user", "content": "Could you send me the invoice by Friday?"}
]
}'
See /docs/quickstart and /docs/messages for the full request/response reference.
Step 3: Detect the source language automatically
Most translation apps don't ask users to specify the source language — they auto-detect it. You can let Claude handle this in the same call by adjusting the prompt instead of running a separate detection step:
Detect the language of the user's text and translate it into {target_language}.
If the text is already in {target_language}, return it unchanged.
Output only the translation, nothing else.
This avoids a second API round-trip. For apps translating short UI strings or chat messages, this single-call approach keeps latency and cost down.
Step 4: Stream the response for a responsive UI
Translation apps feel sluggish if the user has to wait for the full response before seeing anything. Streaming fixes this — tokens appear as they're generated, similar to how Claude's own chat interface behaves.
const response = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"content-type": "application/json",
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
stream: true,
system: "Detect the source language and translate into German. Output only the translation.",
messages: [{ role: "user", content: sourceText }],
}),
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
let output = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
output += decoder.decode(value);
renderPartialTranslation(output); // update your UI incrementally
}
Details on parsing streamed events are covered in /docs/streaming.
Step 5: Handle structured content (not just plain text)
Real translation apps often need to translate inside JSON payloads — app UI strings, CMS fields, product descriptions — without breaking the structure. Instruct Claude explicitly:
Translate only the string values in this JSON object into Portuguese.
Keep all keys unchanged. Return valid JSON only, no markdown fences.
Then validate the output with JSON.parse() before using it, and retry once with a stricter prompt if parsing fails — Claude occasionally wraps JSON in a code block despite instructions.
Step 6: Handle rate limits and multi-user usage
If your translation app has multiple users or clients, you need per-user usage visibility and rate limiting — otherwise one heavy user can exhaust your quota. Managing this yourself means building key issuance, usage tracking, and billing. A platform like SubToAPI gives you per-key usage metadata and team seats out of the box, so you can issue a key per customer or environment and see consumption in one dashboard instead of building that tooling from scratch. Plans start at €9/month with a free trial at /signup.
Putting it together
A minimal but production-viable translation app needs: a prompt that enforces output-only translation, language auto-detection baked into the prompt, streaming for perceived speed, JSON-safe handling for structured content, and a sane way to issue and track API keys per user. None of these pieces are complicated individually — the work is in getting the prompt reliable enough that you don't need post-processing to strip commentary out of every response.
questions
Can Claude translate more accurately than Google Translate or DeepL? For nuanced, idiomatic, or domain-specific text, Claude often produces more natural results because it understands context rather than translating word-by-word. For high-volume literal translation, dedicated translation APIs are usually faster and cheaper.
How do I stop Claude from adding explanations to translations? Be explicit in the system prompt: "Output only the translation, no notes or explanations." If it still happens occasionally, add a post-processing step that strips anything outside the first paragraph, or lower max_tokens to discourage rambling.
Can I use this for real-time chat translation? Yes — combine streaming (see /docs/streaming) with a short, cached system prompt per language pair. For very low latency needs, consider whether a dedicated translation API alongside Claude for ambiguous cases is a better hybrid approach.