Guides · Python · requests
Use the 24-hour render cache to cut costs in Python with requests
Every response carries an X-Cache header, and it is worth wiring into your logs on day one: HIT means the bytes came from the 24-hour cache — served in milliseconds and not counted against your quota. MISS means a real browser rendered the page and one render was metered.
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,
},
timeout=90,
)
resp.raise_for_status()
with open("shot.png", "wb") as f:
f.write(resp.content)
print(resp.headers.get("X-Cache"), "# HIT = free, MISS = rendered and metered")
The cache key is the normalised request body: URL plus every rendering parameter. Same URL at a different width, format or scale is a different entry; alias spellings (device_scale_factor vs device_scale) are folded together first, so they share an entry. Failed renders are never cached and never metered.
Two design consequences. First, idempotent retries are safe: a queue worker that re-fires the same job inside a day costs nothing extra. Second, if you need a fresh render of a page that just changed under the same URL, vary the request — the pragmatic trick is a throwaway query parameter on the target URL (?v=deploy-id), which changes the fingerprint and forces a MISS. Rate limiting still applies to HITs (it protects the endpoint, not the renderer), so keep an eye on X-RateLimit-Remaining in tight loops.
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 or MISS. |
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
- Use the 24-hour render cache to cut costs in Python with httpx
- Use the 24-hour render cache to cut costs in Node.js with built-in fetch
- Use the 24-hour render cache to cut costs in Node.js with axios
- Use the 24-hour render cache to cut costs in PHP with cURL extension
- Use the 24-hour render cache to cut costs 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
- Take a screenshot of a URL
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing
- Add page numbers and headers to a PDF