📋 Table of Contents
Introduction
Capturing website screenshots programmatically is a common need for many applications - from generating link previews to monitoring website changes, creating automated reports, or building SEO tools.
In this guide, we'll explore three different approaches to capture screenshots in Node.js:
- Puppeteer - Google's headless Chrome library
- Playwright - Microsoft's cross-browser automation
- Screenshot API - The simplest approach with SnapAPI
Method 1: Using Puppeteer
Puppeteer is a Node.js library that provides a high-level API to control Chrome or Chromium. It's powerful but requires managing browser installations.
Installation
npm install puppeteer
Basic Screenshot
const puppeteer = require('puppeteer');
async function takeScreenshot(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 720 });
await page.goto(url, { waitUntil: 'networkidle0' });
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
}
takeScreenshot('https://example.com');
⚠️ Note: Puppeteer downloads Chromium (~300MB) on install and requires system dependencies on Linux servers.
Method 2: Using Playwright
Playwright is similar to Puppeteer but supports multiple browsers (Chrome, Firefox, Safari) and has a more modern API.
Installation
npm install playwright
Basic Screenshot
const { chromium } = require('playwright');
async function takeScreenshot(url) {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.setViewportSize({ width: 1280, height: 720 });
await page.goto(url, { waitUntil: 'networkidle' });
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
}
takeScreenshot('https://example.com');
Method 3: Using a Screenshot API
The simplest approach is using a Screenshot API like SnapAPI. No browser installation, no dependencies to manage, just a simple HTTP request.
Using fetch (No dependencies!)
async function takeScreenshot(url) {
const apiKey = 'your-api-key';
const endpoint = `https://snapapi.alterra-solutions.com/v1/screenshot`;
const response = await fetch(
`${endpoint}?url=${encodeURIComponent(url)}&api_key=${apiKey}&response=binary`
);
const buffer = await response.arrayBuffer();
require('fs').writeFileSync('screenshot.png', Buffer.from(buffer));
}
takeScreenshot('https://example.com');
With cURL
curl "https://snapapi.alterra-solutions.com/v1/screenshot?url=https://example.com&api_key=YOUR_KEY&response=binary" -o screenshot.png
✅ Tip: SnapAPI offers 100 free screenshots per month - no credit card required!
Which Method to Choose?
Here's a quick comparison to help you decide:
| Factor | Puppeteer/Playwright | Screenshot API |
|---|---|---|
| Setup Time | 30+ minutes | 2 minutes |
| Dependencies | ~300MB browser | None |
| Server Resources | High (CPU/RAM) | Minimal |
| Maintenance | Browser updates | None |
| Best For | Full browser control | Quick screenshots |
Choose Puppeteer/Playwright if:
- You need complex browser automation (clicking, form filling)
- You want full control over the rendering
- You're already running heavy server infrastructure
Choose a Screenshot API if:
- You just need website screenshots
- You want minimal setup and maintenance
- You're building a serverless application
- You value simplicity and speed
Try SnapAPI Free
Get started with 100 free screenshots per month. No credit card required.
Get Your API Key →