Guides · Python · requests
Take a screenshot of a URL in Python with requests
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 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": "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 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
| Header | Meaning |
|---|---|
X-Cache | HIT = served from the 24h cache, free. |
X-Quota-Remaining | Metered renders left this month. |
X-RateLimit-Remaining | Requests left in the current second. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Take a screenshot of a URL in Python with httpx
- Take a screenshot of a URL in Node.js with built-in fetch
- Take a screenshot of a URL in Node.js with axios
- Take a screenshot of a URL in PHP with cURL extension
- Take a screenshot of a URL in Ruby with Net::HTTP
More with Python + requests
- Convert an HTML form to a fillable PDF
- Get a fillable PDF of just the form — no nav, no footer
- Generate a fillable invoice PDF from a web page
- Turn an online registration form into a fillable PDF
- Wait for a JavaScript-rendered form, then make it fillable
- Verify a fillable PDF render without opening the file
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a retina-quality (2x) screenshot
- Take a JPEG screenshot at a custom viewport size
- Use the 24-hour render cache to cut costs
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing
- Add page numbers and headers to a PDF