October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Download a Screenshot API Response as a File in Python

Save screenshot API output correctly in Python: check the response, write image bytes in binary mode, and handle streaming, redirects, and JSON responses.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a screenshot API returns image data directly, save it with Python in binary mode: check the HTTP status, then write response.content to a file opened with wb. First confirm the API’s response format: some endpoints return a redirect or JSON containing an image URL instead of image bytes.

Save a direct image response with Requests

For a small screenshot returned as raw bytes, use requests.get(), check for an HTTP error, and write the bytes to disk. This example assumes the endpoint accepts a GET request with a url query parameter and returns PNG data; replace those details with the screenshot provider’s documented request format.

As an Amazon Associate I earn from qualifying purchases.

import requests

response = requests.get(
    "SCREENSHOT_ENDPOINT",
    params={"url": "https://example.com"},
    timeout=30,
)
response.raise_for_status()

with open("screenshot.png", "wb") as image_file:
    image_file.write(response.content)

The 30-second timeout is an example, not a universal setting. Choose a finite timeout appropriate to your service and workload. Requests exposes response.content as bytes; raise_for_status() prevents an HTTP error response from being silently saved as though it were a screenshot. See the Requests API reference and its Quickstart.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep credentials out of source code

If the API requires a key, pass it using the provider’s documented authentication method. Keep secrets in an environment variable or secret store rather than committing them to a script or repository. The authentication scheme is provider-specific.

Identify what the API actually returns

Do not choose the save logic or filename based only on the word “screenshot” in the endpoint. Check the provider’s documentation and, where relevant, the response status, headers, and body.

  • Raw image bytes: Save the response body as bytes, as in the first example.
  • Redirect to an image: Follow the provider’s documented redirect behavior and save the final image response. Confirm that your client follows redirects as expected.
  • JSON with an image URL: Parse the JSON, extract the documented URL field, then make a second request and save that response’s bytes. Do not save the JSON response with a .png extension.

As one provider-specific example, Screenshot API documents JSON by default and a redirect=1 option for image or PDF output; its Python example reads a screenshotUrl from JSON. That is not a universal screenshot API contract. See its REST API documentation.

Stream large screenshots to disk

For a potentially large response, stream chunks to the file instead of retaining the entire body in memory. Requests recommends iter_content() for streamed downloads; it handles gzip and deflate transfer encodings. The chunk size below is a code choice, not a performance benchmark.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import requests

with requests.get(
    "SCREENSHOT_ENDPOINT",
    params={"url": "https://example.com"},
    stream=True,
    timeout=30,
) as response:
    response.raise_for_status()

    with open("screenshot.png", "wb") as image_file:
        for chunk in response.iter_content(chunk_size=64 * 1024):
            if chunk:
                image_file.write(chunk)

Use a finite timeout appropriate to the provider and workload. A timeout is not a guarantee that a screenshot will finish within that duration: the provider’s rendering time and network conditions matter. Requests’ streaming guidance is in the Quickstart.

Match the file extension to the image format

A file extension does not convert the response. If the API returns JPEG bytes but the file is named screenshot.png, the contents remain JPEG. Use the format requested from the provider, if supported, or inspect Content-Type and the provider’s documentation before choosing an extension. Requests exposes response headers through response.headers; the endpoint’s exact output format cannot be inferred without its documentation.

Use Python’s standard library if you do not want Requests

urllib.request is a standard-library option for opening URLs. Use the provider’s actual endpoint, parameters, and authentication, and preserve the same essentials: handle HTTP errors, treat image payloads as bytes, and distinguish image data from JSON or redirects. See the Python 3.13 urllib.request documentation.

Common problems and fixes

  • The saved file is not an image: The server may have returned an error or JSON. Check the status with raise_for_status(), inspect Content-Type, and verify the documented response shape before writing.
  • The image opens in the wrong application or not at all: The extension may not match the returned format. Check the provider’s format setting and response headers; renaming the file does not convert it.
  • The request hangs or fails on slow captures: Set a finite timeout suitable for the service and workload. If it expires, adjust it based on the provider’s documented behavior and your application’s needs.
  • The response is JSON rather than an image: Parse the documented JSON field containing the screenshot URL, then download that URL in a second request.
  • The output is incomplete or memory use is high: For large responses, use stream=True and write non-empty chunks from iter_content().
  • The API rejects the request: Check its required HTTP method, authentication, parameter names, and response mode. These details vary by provider; the illustrative request above is not a universal API format.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

ScreenshotNeo returns an image or PDF from one GET request. For a PNG, JPEG, or WebP screenshot, use its documented format parameter as needed; this basic example saves the response body as WebP:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)

See the ScreenshotNeo documentation for request parameters and response details. It accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers identify the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 screenshots.

Learn about ScreenshotNeo, or sign up free for 1,000 screenshots a month with no card.

Frequently Asked Questions

Does writing a screenshot response with wb convert it to PNG?

No. Binary mode preserves the response bytes; it does not change their format. Match the extension to the returned format.

Should I use response.content or iter_content()?

Use response.content for a small response you can keep in memory. For a potentially large download, stream with stream=True and write chunks from iter_content().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.