A code window sending a web page to be captured as an image
DevelopersPythonInstant Snap API

How to take a website screenshot in Python

Install one package, set one environment variable, and a web page becomes a PNG, WebP or PDF in a few lines. No browser to install, update or keep alive on your server.

· 8 min read

The choice

Two ways to do it

There are two ways to screenshot a web page from Python. You can drive a browser yourself, with Selenium or Playwright, which means installing a browser where your code runs, keeping it updated, and writing your own answers to cookie banners, lazy-loaded images and pages that never finish loading. Or you can ask an API that already runs the browsers, and get the file back.

This post is the second way, with the official websitescreenshotworld library for the Instant Snap API. The library is open source under the MIT licence and needs Python 3.10 or later. Its only dependency is httpx.

Five minutes

The first capture

Create an Instant Snap API key in the app under Developer Tools › API Keys, then install the library and put the key in the environment:

Terminal
pip install websitescreenshotworld
export WSW_INSTANT_SNAP_API_KEY="wsw_api_..."

The library reads the key from that variable, so the program does not contain it:

capture.py
from websitescreenshotworld import WebsiteScreenshotWorld

# Reads WSW_INSTANT_SNAP_API_KEY from the environment.
with WebsiteScreenshotWorld() as client:
    capture = client.instant_snap.capture(
        url="https://example.com",
        format="webp",
        viewport={"width": 1440, "height": 900},
    )
    print(capture.id, capture.width, capture.height)
    print(capture.download_url)  # valid for 60 minutes

The answer is the capture: its id, its size in pixels and a signed download link. The link is valid for 60 minutes and the capture itself is kept for 24 hours. An option you leave out is not sent, so the API applies its default: a 1920 by 1080 window, with ads blocked and pop-ups dismissed.

The file

Getting the file

When you want the image on disk and nothing else, capture_file returns the file itself, streamed from the connection instead of held in memory. Use it in a with block so the connection is released:

save.py
from websitescreenshotworld import WebsiteScreenshotWorld

client = WebsiteScreenshotWorld()

with client.instant_snap.capture_file(url="https://example.com", format="png") as file:
    print(file.filename, file.width, file.height, file.sha256)
    file.save("example.png")

The object also carries the capture's details, including the SHA-256 of the file. A file returned this way is not kept by the API. To fetch a stored capture later, read it back by its id and download it:

capture = client.instant_snap.get("3f6c2a1e-9b8d-4c7e-a5f4-1d2e3c4b5a69")
client.instant_snap.download(capture, "example.png")

The download link is signed, so your API key is not sent with it. There is also capture_to_bucket, which uploads the capture to your own S3-compatible bucket.

Options

Full page, phone, PDF

Options are keyword arguments named exactly as in the API reference. Nested blocks such as viewport and selectors are plain dictionaries, typed so your editor knows their fields.

Four common captures
# The whole page, top to bottom
client.instant_snap.capture(url="https://example.com", scroll_mode="scroll")

# As a phone
client.instant_snap.capture(
    url="https://example.com",
    device_category="mobile",
    emulated_device="iPhone 13",
)

# A PDF of the whole page
client.instant_snap.capture(
    url="https://example.com",
    media_category="pdf",
    format="image_pdf",
    scroll_mode="scroll",
)

# Without the sticky header and the chat bubble
client.instant_snap.capture(
    url="https://example.com",
    selectors={"hide": "header.sticky, #chat-widget"},
)
  • Full page. scroll_mode is viewport by default, which captures what fits on screen. scroll walks the whole page. Our full-page guide covers the pages that fight back.
  • Phone. A named device decides the viewport, the pixel density and the touch behaviour, so a viewport sent with it is ignored. client.instant_snap.catalog() lists the devices you can name.
  • PDF. For a scrolling capture use image_pdf, whose pages are images merged into one document. document_pdf with scroll_mode set to scroll is refused.
  • Hiding elements. selectors.hide takes up to 25 comma-separated CSS selectors. One the browser rejects is skipped; it does not fail the capture.

The request parameters page lists every option with its default and its limits.

When it goes wrong

Errors and retries

Every error is a WebsiteScreenshotWorldError carrying the API's error_code. Branch on the class or on the code, never on the message:

errors.py
from websitescreenshotworld import (
    CaptureError,
    PlanLimitError,
    WebsiteScreenshotWorld,
    WebsiteScreenshotWorldError,
)

client = WebsiteScreenshotWorld()

try:
    capture = client.instant_snap.capture(url="https://example.com")
except CaptureError as error:
    print("The page could not be captured:", error.message)
except PlanLimitError as error:
    print("Plan limit:", error.error_code)
except WebsiteScreenshotWorldError as error:
    print("Failed:", error)
  • What is retried for you. A rate limit, a busy service and a connection failure are retried twice, three attempts in all. The wait is the one the API asked for, at most 60 seconds.
  • What is not. capture_failed and capture_timeout are answers about the website, and a retry would fail the same way. A plan limit is not retried either: waiting does not change it.
  • One timeout for the whole call. 180 seconds for a capture and 30 for anything else, retries included. Change them with capture_timeout and timeout on the client.
  • Your own idempotency key. Pass idempotency_key when a job queue may run the same task twice. A repeat with the same key and the same options gets the first capture back, for 24 hours.
Volume

Many pages: asyncio and the queue

For asyncio code, AsyncWebsiteScreenshotWorld has the same settings and methods; use it with async with and await the calls.

A workspace has at most two captures rendering at once, so firing fifty requests together does not make them finish sooner. For a batch, queue the captures instead: submit answers at once with the capture in status queued, and wait reads it until it has finished.

submit and wait
from websitescreenshotworld import CaptureStatus, WebsiteScreenshotWorld

with WebsiteScreenshotWorld() as client:
    queued = client.instant_snap.submit(url="https://example.com", format="png")
    capture = client.instant_snap.wait(queued)

    if capture.status == CaptureStatus.SUCCEEDED:
        client.instant_snap.download(capture, "example.png")
    elif capture.status == CaptureStatus.FAILED:
        print("Failed:", capture.error_code)

A queued capture waits its turn for up to 15 minutes, and a workspace can have 100 of them waiting or running. wait gives up after 360 seconds by default and returns the capture as last read, still unfinished; it does not raise. Call it again later.

The same page every day is a schedule, not a loop
If the plan is a cron job that captures the same addresses every morning, an Auto Schedule does that without any code, keeps the history and can tell you when a page changed. The API is for captures your program decides on.
Plain HTTP

Without the library

You do not need the library. The API is plain HTTPS and JSON, with the key in an X-Api-Key header:

requests
import os, requests

response = requests.post(
    "https://api.websitescreenshotworld.com/instant-snap/v1/captures",
    headers={"X-Api-Key": os.environ["WSW_API_KEY"]},
    json={"url": "https://example.com"},
)

capture = response.json()
print(capture["download_url"])

What you give up is the retry, timeout and idempotency code, which you would then write yourself. The library also verifies webhook signatures; the earlier post on verifying a webhook signature explains what that check does.

Questions

FAQ

Do I need Selenium, Playwright or Chrome installed?

No. The page is loaded in a real browser on our side and your code makes one HTTPS request. The library's only dependency is httpx.

Which Python versions does the library support?

Python 3.10 or later. It is tested on CPython 3.10 and 3.13, and it has both an ordinary client and an asyncio one.

How long is a capture kept?

A capture made with capture is kept for 24 hours. Its download link is valid for 60 minutes, and reading the capture again by its id gives a fresh link. A file returned by capture_file is not kept at all.

Can a retry charge me twice?

No. Every capture the library sends carries an idempotency key, reused on its retries, so a retried capture is never run or charged twice.

Can I try it without paying?

Yes. The free trial lasts one month, needs no card and includes 100 captures. Its API rate limit is 10 requests a minute. Request headers, cookies and choosing the country a capture comes from are not part of the trial.