Guides · Python · httpx

Take a JPEG screenshot at a custom viewport size in Python with httpx

Two independent knobs, one request: format picks the codec ("jpeg" here — much smaller files for photographic content), and width/height set the viewport the page believes it is being viewed in. 390×844 is an iPhone-class viewport, so responsive pages serve their mobile layout — media queries fire on viewport width, no user-agent tricks needed.

Below is a complete, runnable Python program using httpx (pip install httpx). It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

import os
import httpx

resp = httpx.post(
    "https://snapdok.io/v1/render",
    headers={"Authorization": "Bearer " + os.environ["SNAPDOK_KEY"]},
    json={
        "url": "https://your-app.com/report/42",
        "format": "jpeg",
        "width": 390,
        "height": 844,
    },
    timeout=90,
)
resp.raise_for_status()

with open("mobile.jpg", "wb") as f:
    f.write(resp.content)

When to pick which format: JPEG compresses gradients and photos far better and has no alpha channel; PNG is lossless and wins on flat UI, text and sharp edges. For mobile-layout QA screenshots at a fixed viewport, JPEG cuts transfer size dramatically with no visible cost. If you need the image to sit on a transparent background, that is PNG territory.

height here is the viewport height — what "above the fold" means for this render. Without full_page you get exactly that viewport; combine full_page: true with a width and the height is measured from the document instead, at which point the viewport height only affects lazy-load behaviour. All three parameters change the cache fingerprint, so a 390-wide render and a 1280-wide render are separate cache entries — as they should be.

Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.

Notes for httpx

httpx looks like requests but differs where it bites: it ships a 5-second default timeout, which a render that drives a real browser will blow through on heavier pages — hence the explicit timeout=90. raise_for_status() exists and behaves the same. If you are on asyncio already, the same call works with httpx.AsyncClient and await client.post(...) — the request body and headers are identical.

Response headers worth reading

HeaderMeaning
Content-Typeimage/jpeg for this request.
X-CacheEach distinct width/height/format is its own cache entry.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Python + httpx