DEV Community

screenshotapis
screenshotapis

Posted on

How to Generate Website Screenshots with Python in 3 Lines of Code

Ever needed to capture a screenshot of a website programmatically? Maybe for generating link previews, monitoring pages, or creating PDF reports. Here's how to do it with Python in just a few lines.

The Problem

Taking website screenshots programmatically usually means setting up headless Chrome, installing Playwright or Puppeteer, managing browser instances, handling timeouts, and dealing with all the edge cases. It's a lot of infrastructure for what should be a simple task.

The Simple Way

ScreenshotAPIs is an API that handles all of that for you. Send a URL, get back an image or PDF.

1. Get Your API Key

Sign up at screenshotapis.org — the free plan gives you 100 screenshots per month.

2. Take a Screenshot

import requests

response = requests.post(
    "https://screenshotapis.org/v1/screenshot",
    headers={"X-API-Key": "your_api_key_here"},
    json={"url": "https://github.com"}
)

with open("screenshot.png", "wb") as f:
    f.write(response.content)
Enter fullscreen mode Exit fullscreen mode

That's it. Three lines of real code (plus the import and file save).

3. Customize It

You can control the output format, viewport size, and more:

response = requests.post(
    "https://screenshotapis.org/v1/screenshot",
    headers={"X-API-Key": "your_api_key_here"},
    json={
        "url": "https://github.com",
        "format": "jpeg",
        "width": 1280,
        "height": 720,
        "full_page": True
    }
)
Enter fullscreen mode Exit fullscreen mode

Supported options:

  • formatpng, jpeg, or webp
  • width / height — viewport dimensions
  • full_page — capture the entire scrollable page
  • delay_ms — wait before capturing (useful for pages with animations)

Generate PDFs Too

Same API, different endpoint:

response = requests.post(
    "https://screenshotapis.org/v1/pdf",
    headers={"X-API-Key": "your_api_key_here"},
    json={"url": "https://github.com"}
)

with open("page.pdf", "wb") as f:
    f.write(response.content)
Enter fullscreen mode Exit fullscreen mode

Render Raw HTML

Don't have a URL? You can pass HTML directly:

response = requests.post(
    "https://screenshotapis.org/v1/screenshot",
    headers={"X-API-Key": "your_api_key_here"},
    json={
        "html": "<h1>Hello World</h1><p>Rendered as an image.</p>",
        "width": 800,
        "height": 400
    }
)
Enter fullscreen mode Exit fullscreen mode

This is great for generating OG images, email previews, or dynamic social cards.

Node.js? Same Thing

const response = await fetch("https://screenshotapis.org/v1/screenshot", {
  method: "POST",
  headers: {
    "X-API-Key": "your_api_key_here",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ url: "https://github.com" })
});

const buffer = await response.arrayBuffer();
require("fs").writeFileSync("screenshot.png", Buffer.from(buffer));
Enter fullscreen mode Exit fullscreen mode

Use Cases

  • Link previews — Show a thumbnail of any URL in your app
  • PDF reports — Convert dashboards or pages to downloadable PDFs
  • OG images — Generate dynamic social media preview images from HTML templates
  • Monitoring — Take periodic screenshots to track visual changes on a page
  • Archiving — Save snapshots of web pages for compliance or records

Pricing

The free tier gives you 100 captures per month. Paid plans start at $19/month for 2,000 captures. There are also one-time credit packs if you don't need a subscription.

Check it out at screenshotapis.org.

Top comments (0)