Guides · Python · requests

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

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 requests (pip install requests). It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

import os
import requests

resp = requests.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 requests

Three requests-specific details the sample already handles. resp.content is the binary body — resp.text would decode PDF bytes as text and corrupt them. raise_for_status() turns a JSON error response into an exception instead of letting it reach the open(..., "wb") call. And requests has no default timeout — without the explicit timeout= a hung connection blocks forever, which matters for a call that drives a real browser and legitimately takes seconds.

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 + requests