Guides · Python · httpx

Take a screenshot of a URL in Python with httpx

The simplest request the API takes: a URL in, PNG bytes out. No full_page, no extra options — you get exactly what a visitor with a 1280×800 browser window would see above the fold. That is the right default more often than people expect: link previews, monitoring thumbnails and visual smoke tests all want the fold, not a 40,000-pixel scroll of the footer.

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": "png",
        "width": 1280,
        "height": 800,
    },
    timeout=90,
)
resp.raise_for_status()

with open("screenshot.png", "wb") as f:
    f.write(resp.content)

The two dimensions are the viewport, not the output size: the page lays itself out as if the browser window were 1280×800, and the image comes back at exactly those pixels (multiply by device_scale if you set it). Responsive sites react to the width — ask for 390 and you get the mobile layout, no user-agent spoofing involved.

Two habits worth starting on day one. Check the HTTP status before writing bytes to disk — errors arrive as JSON, and saving {"error":"UNAUTHORIZED"} as screenshot.png is the classic first-week bug. And log the X-Cache header: identical requests within 24 hours return HIT, cost nothing, and come back in milliseconds — failed renders are never metered either way.

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
X-CacheHIT = served from the 24h cache, free.
X-Quota-RemainingMetered renders left this month.
X-RateLimit-RemainingRequests left in the current second.

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

Related guides

Same task, other stacks

More with Python + httpx