Translate a Python source file
const url = 'https://api.legesher.io/v1/translate';const options = { method: 'POST', headers: {'Content-Type': 'application/json'}, body: '{"enhanced":false,"python_version":"3.10","source":"example","source_lang":"example","target_lang":"example"}'};
try { const response = await fetch(url, options); const data = await response.json(); console.log(data);} catch (error) { console.error(error);}curl --request POST \ --url https://api.legesher.io/v1/translate \ --header 'Content-Type: application/json' \ --data '{ "enhanced": false, "python_version": "3.10", "source": "example", "source_lang": "example", "target_lang": "example" }'Translate a complete Python source file between natural languages.
Supports en→X, X→en, and X→Y (pivoted through English). With
enhanced: true (any paid plan), a model pass additionally
translates the extracted comments, docstrings, string literals, and
identifier proposals, once, after the pivot.
Request Bodyrequired
Section titled “ Request Bodyrequired ”A complete Python source file to translate.
object
Request AI-enhanced translation: after the deterministic pack pass, a model pass translates the extracted comments, docstrings, string literals, and identifier proposals (collision-validated against the pack). Gated to paid plans, Individual and above (402 tier_required on Free); degrades gracefully to standard with mode_fallback=true when the model layer is unavailable.
Python grammar version used to select the language pack.
Python source text to translate.
Language the source is written in (ISO 639 code).
Language to translate into (ISO 639 code).
Responses
Section titled “ Responses ”Successful Response
Translated source plus the parameters that produced it.
object
Element-level metadata (cultural comments; enhanced element counts and identifier glossary when the model pass ran); null when there is none to report.
Element-level metadata for one translation.
Everything here is about the translation the caller just received, derived from their own submission, never from any other caller’s data.
object
How many extracted elements the model pass covered, by kind; null unless the enhanced pass ran.
Elements the enhanced model pass submitted, by kind.
object
Identifier translations applied by the enhanced pass (source identifier → translated identifier), collision-validated against the language pack; null unless the enhanced pass ran. With the opt-in X-Legesher-Repo header, later requests from the same caller for the same repository reuse this glossary for consistent naming; it is kept per caller and never shared with another.
object
True when enhanced was requested but the model pass could not run (or failed) and the deterministic result is served instead.
Which pipeline produced translated_source: ‘standard’ (deterministic pack pass) or ‘enhanced’ (pack pass + model pass over extracted elements).
True when translated via the English pivot (X→en→Y).
Example
{ "metadata": { "enhanced_elements": { "comments": 0, "docstrings": 0, "identifiers": 0, "strings": 0 } }, "mode_fallback": false, "mode_used": "standard", "python_version": "3.10"}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
An API key was sent and is not valid. A request with no key runs on the anonymous trial instead.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Enhanced translation needs a paid plan (tier_required).
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
The language has no pack for the requested Python version.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
The request body or the source is over the size limit.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
A field is missing or malformed, or a language code is not supported.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
Too many requests, or this month’s character allowance is used up.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Seconds to wait before retrying. Usually sent with rate_limited; absent on quota_exceeded, which lasts until the UTC month resets.
Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.
The translation engine is unavailable.
Envelope for every error response.
object
Machine-readable code + human message for one error.
object
Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.
Per-field breakdown (validation errors only).
One request-validation problem, stripped of the submitted input.
object
Dotted location of the offending field.
Human-readable description.
Pydantic error type identifier.
Human-readable explanation.
Example generated
{ "error": { "code": "example", "details": [ { "field": "example", "message": "example", "type": "example" } ], "message": "example" }}Headers
Section titled “ Headers ”Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.
Requests remaining in the current rate window.
Seconds until the rate limit fully resets.
Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.
Characters left in the caller’s monthly budget.
Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.