How to Generate Website Screenshots with Python in 3 Lines of Code
Source: Dev.to
Ever needed to capture a screenshot of a website programmatically? Whether you’re generating link previews, monitoring pages, or creating PDF reports, you can 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 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)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
}
)Supported options
format—png,jpeg, orwebpwidth/height— viewport dimensionsfull_page— capture the entire scrollable pagedelay_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)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": "
## Hello World
Rendered as an image.
",
"width": 800,
"height": 400
}
)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));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
- Free tier – 100 captures per month
- Paid plans – start at $19/month for 2,000 captures
- One‑time credit packs – available if you don’t need a subscription
Check it out at screenshotapis.org.