Go
This guide uses only the standard library (net/http, encoding/json), with no external dependencies. A reusable client is built with methods to upload a local file, convert from a signed URL and poll an asynchronous job, with idiomatic error handling in Go (own error type implemented by error, wrapped with %w).
Local file (minimal example)
URL signed (minimal example)
Cliente reutilizable
For real use, it is convenient to encapsulate the authentication logic, construction of requests, handling of202 and errors in a struct with methods. This client covers ConvertRaw (local file), ConvertFromURL (URL signed), and pollJob (%%0005% polling).
The code above assumes that you import
context in addition to the packages already listed; add it to the import block of your actual file ("context").Client usage
Language error handling
TypeAPIError implements Go’s error interface (method Error() string) and exposes StatusCode so that calling code can inspect it with errors.As. All internal errors (network, (de)serialization failures) are wrapped with %w to preserve the chain of causes and allow errors.Is/errors.As at any point:
Handling of 202 (auto-async)
When all backends are saturated, API returns202 with a job_id. The client above already resolves this transparently via PollJob, but if you prefer not to use the struct, here is the equivalent flow independently:
Production
time.Duration
default:"120s"
Always set an explicit timeout to
http.Client. The NewClient client above already does this, but if you use http.DefaultClient directly, it will run out of timeout and a hung connection will block your goroutine indefinitely.http.Transport with DialContext and ResponseHeaderTimeout:
Retries with exponential backoff
For errors429 (rate limit) and 5xx (transient server error), retry with exponential backoff and jitter. For the rest of the 4xx (400, 401, 403, 413, 415, 422) do not retry without correcting the request.
BYOS (Bring Your Own Storage) for large files
For large documents, instead of waiting for the full markdown in the HTTP response, you can ask the API to upload it directly to your own storage (S3, GCS, R2, etc.) using a signed URL of typePUT. This avoids keeping the connection open and downloading the entire file into memory.
Using
output_url automatically activates response_format=json, output_encoding=zstd, and slim=true. Pass output_head_url (a signed URL of type HEAD) if you want API to detect that the result already exists in your storage and avoid reprocessing the document.