Code Examples

Take Screenshots with Python

Ready-to-copy Python code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses requests.

Quick Start

Screenshot in Python — minimal example

The simplest way to capture a screenshot. Just a URL and your API key.

Pythonquickstart
import requests

resp = requests.get("https://api.screenshotdev.com/v1/screenshot", params={
    "url": "https://example.com",
    "access_key": "YOUR_ACCESS_KEY"
})
with open("screenshot.png", "wb") as f:
    f.write(resp.content)
Full Example

Production-ready Python script

Complete working script with options for viewport, format, full page capture, and error handling.

Pythonfull example
import requests
import sys
from pathlib import Path

API_KEY = "YOUR_ACCESS_KEY"
BASE = "https://api.screenshotdev.com/v1/screenshot"

def take_screenshot(
    url: str,
    format: str = "webp",
    width: int = 1280,
    full_page: bool = False,
    dark_mode: bool = False,
) -> bytes:
    resp = requests.get(BASE, params={
        "url": url,
        "access_key": API_KEY,
        "format": format,
        "width": width,
        "full_page": full_page,
        "dark_mode": dark_mode,
    })
    resp.raise_for_status()
    return resp.content

if __name__ == "__main__":
    url = sys.argv[1] if len(sys.argv) > 1 else "https://example.com"
    img = take_screenshot(url, format="webp", width=1440, full_page=True)
    Path("screenshot.webp").write_bytes(img)
    print(f"Saved screenshot.webp ({len(img)} bytes)")
Advanced

More Python examples

PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.

PDF Generation

Pythonpdf
import requests

resp = requests.get("https://api.screenshotdev.com/v1/pdf", params={
    "url": "https://example.com",
    "access_key": "YOUR_ACCESS_KEY",
    "format": "a4",
    "landscape": False,
    "margin": "20px",
})
resp.raise_for_status()
with open("page.pdf", "wb") as f:
    f.write(resp.content)

Batch Screenshots

Pythonbatch
import requests
from concurrent.futures import ThreadPoolExecutor

API_KEY = "YOUR_ACCESS_KEY"
urls = [
    "https://example.com",
    "https://github.com",
    "https://news.ycombinator.com",
]

def capture(url: str) -> tuple[str, int]:
    resp = requests.get("https://api.screenshotdev.com/v1/screenshot", params={
        "url": url,
        "access_key": API_KEY,
        "format": "webp",
    })
    resp.raise_for_status()
    filename = url.split("//")[1].replace("/", "_") + ".webp"
    with open(filename, "wb") as f:
        f.write(resp.content)
    return filename, len(resp.content)

with ThreadPoolExecutor(max_workers=5) as pool:
    for name, size in pool.map(capture, urls):
        print(f"  {name} ({size:,} bytes)")

Async with Webhooks

Pythonwebhooks
import requests

resp = requests.post("https://api.screenshotdev.com/v1/screenshot", json={
    "url": "https://example.com",
    "access_key": "YOUR_ACCESS_KEY",
    "webhook_url": "https://your-app.com/api/webhooks/screenshot",
    "format": "webp",
    "full_page": True,
})
resp.raise_for_status()
job = resp.json()
print(f"Job queued: {job['id']}")
# Your webhook endpoint will receive the result when ready

Custom CSS/JS Injection

Pythoninject
import requests

resp = requests.get("https://api.screenshotdev.com/v1/screenshot", params={
    "url": "https://example.com",
    "access_key": "YOUR_ACCESS_KEY",
    "inject_css": "body { font-family: 'Inter', sans-serif; } .ad, .banner { display: none; }",
    "inject_js": "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())",
    "block_ads": True,
})
resp.raise_for_status()
with open("clean.webp", "wb") as f:
    f.write(resp.content)

S3 Upload

Pythons3
import requests
import boto3

# 1. Capture the screenshot
resp = requests.get("https://api.screenshotdev.com/v1/screenshot", params={
    "url": "https://example.com",
    "access_key": "YOUR_ACCESS_KEY",
    "format": "webp",
})
resp.raise_for_status()

# 2. Upload to S3
s3 = boto3.client("s3")
s3.put_object(
    Bucket="my-screenshots",
    Key="captures/example-com.webp",
    Body=resp.content,
    ContentType="image/webp",
)
print("Uploaded to s3://my-screenshots/captures/example-com.webp")

Try it live

Test the API in the interactive playground — pre-filled with Python snippets.

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
pip install requests

Uses requests. Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with Python today.

100 free API calls per month. No credit card. First screenshot in under 30 seconds.

Take Screenshots with Python — ScreenshotDEV