Skip to main content

Django

This guide covers a function-based synchronous view that uploads a file to /convert/raw, maps contract status codes from API to idiomatic Django responses, and a async def variant with httpx for Django 4.1+.

Synchronous view (function-based)

Use requests because Django’s synchronous views already run on a dedicated WSGI worker; there is no benefit in putting async in there.
The @csrf_exempt decorator assumes that this endpoint is consumed by an API client (mobile app or another service), not by a browser form within the same Django session. If you expose it to a form with a user session, use the normal CSRF token instead of exempting it.

Vista basada en clase (alternativa)

If you prefer the CBV style, the same flow fits well in a View:

Vista async (Django 4.1+)

Django 4.1+ supports async def in views. Combined with httpx.AsyncClient, avoid blocking the worker while waiting for a response from API (useful if your deployment uses ASGI, e.g. behind Uvicorn/Daphne).
request.FILES in Django requires that the body has been parsed by multipart/form-data. With ASGI and async views this still works the same (Django parses the form before invoking the view), but if your client uploads the file as a raw body without multipart, it reads request.body instead.

URLs

Settings

Production

Timeouts

It will separate the connection timeout from the reading timeout. Large documents in quality mode may take longer than the default of requests:

Retries with exponential backoff

Just retry 429 and 5xx. For the rest of the 4xx, I corrected the request instead of retrying:

Large files with BYOS (output_url)

For large documents, avoid passing the entire Markdown through the body of your Django view: generate a pre-signed URL %0005%% from your own storage (S3, R2, etc.) and pass it as output_url. API uploads the result there directly and returns a small JSON instead of the full Markdown. This also auto-activates response_format=json, output_encoding=zstd and slim=true.
If the same document is uploaded twice, output_head_url allows API to detect that the object already exists in your storage and return the result without reprocessing the document — useful for deduplicating repeated uploads without extra logic on your side.