Code Examples

Take Screenshots with Node.js

Ready-to-copy Node.js code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses fetch (built-in) or node-fetch.

Quick Start

Screenshot in Node.js — minimal example

The simplest way to capture a screenshot. Just a URL and your API key.

Node.jsquickstart
const resp = await fetch(
  "https://api.screenshotdev.com/v1/screenshot?" +
  new URLSearchParams({ url: "https://example.com", access_key: "YOUR_ACCESS_KEY" })
);
const buffer = Buffer.from(await resp.arrayBuffer());
require("fs").writeFileSync("screenshot.png", buffer);
Full Example

Production-ready Node.js script

Complete working script with options for viewport, format, full page capture, and error handling.

Node.jsfull example
import fs from "node:fs/promises";

const API_KEY = "YOUR_ACCESS_KEY";
const BASE = "https://api.screenshotdev.com/v1/screenshot";

async function takeScreenshot(url, options = {}) {
  const params = new URLSearchParams({
    url,
    access_key: API_KEY,
    format: options.format ?? "webp",
    width: String(options.width ?? 1280),
    full_page: String(options.fullPage ?? false),
    dark_mode: String(options.darkMode ?? false),
  });

  const resp = await fetch(`${BASE}?${params}`);
  if (!resp.ok) {
    const body = await resp.text();
    throw new Error(`Screenshot failed (${resp.status}): ${body}`);
  }
  return Buffer.from(await resp.arrayBuffer());
}

const img = await takeScreenshot("https://example.com", {
  format: "webp",
  width: 1440,
  fullPage: true,
});
await fs.writeFile("screenshot.webp", img);
console.log(`Saved screenshot.webp (${img.length} bytes)`);
Advanced

More Node.js examples

PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.

PDF Generation

Node.jspdf
const params = new URLSearchParams({
  url: "https://example.com",
  access_key: "YOUR_ACCESS_KEY",
  format: "a4",
  landscape: "false",
  margin: "20px",
});
const resp = await fetch(`https://api.screenshotdev.com/v1/pdf?${params}`);
if (!resp.ok) throw new Error(`PDF failed: ${resp.status}`);
const buffer = Buffer.from(await resp.arrayBuffer());
await require("node:fs/promises").writeFile("page.pdf", buffer);

Batch Screenshots

Node.jsbatch
const API_KEY = "YOUR_ACCESS_KEY";
const urls = [
  "https://example.com",
  "https://github.com",
  "https://news.ycombinator.com",
];

const results = await Promise.all(
  urls.map(async (url) => {
    const params = new URLSearchParams({
      url,
      access_key: API_KEY,
      format: "webp",
    });
    const resp = await fetch(
      `https://api.screenshotdev.com/v1/screenshot?${params}`
    );
    if (!resp.ok) throw new Error(`Failed for ${url}: ${resp.status}`);
    const buf = Buffer.from(await resp.arrayBuffer());
    const name = new URL(url).hostname + ".webp";
    await require("node:fs/promises").writeFile(name, buf);
    return { name, size: buf.length };
  })
);

results.forEach(({ name, size }) =>
  console.log(`  ${name} (${size.toLocaleString()} bytes)`)
);

Async with Webhooks

Node.jswebhooks
const resp = await fetch("https://api.screenshotdev.com/v1/screenshot", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://example.com",
    access_key: "YOUR_ACCESS_KEY",
    webhook_url: "https://your-app.com/api/webhooks/screenshot",
    format: "webp",
    full_page: true,
  }),
});
const job = await resp.json();
console.log(`Job queued: ${job.id}`);
// Your webhook endpoint will receive the result when ready

Custom CSS/JS Injection

Node.jsinject
const params = new URLSearchParams({
  url: "https://example.com",
  access_key: "YOUR_ACCESS_KEY",
  inject_css: "body { font-family: 'Inter', sans-serif; } .ad, .banner { display: none; }",
  inject_js: "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())",
  block_ads: "true",
});
const resp = await fetch(
  `https://api.screenshotdev.com/v1/screenshot?${params}`
);
const buffer = Buffer.from(await resp.arrayBuffer());
await require("node:fs/promises").writeFile("clean.webp", buffer);

S3 Upload

Node.jss3
import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";

// 1. Capture the screenshot
const params = new URLSearchParams({
  url: "https://example.com",
  access_key: "YOUR_ACCESS_KEY",
  format: "webp",
});
const resp = await fetch(
  `https://api.screenshotdev.com/v1/screenshot?${params}`
);
const buffer = Buffer.from(await resp.arrayBuffer());

// 2. Upload to S3
const s3 = new S3Client({});
await s3.send(new PutObjectCommand({
  Bucket: "my-screenshots",
  Key: "captures/example-com.webp",
  Body: buffer,
  ContentType: "image/webp",
}));
console.log("Uploaded to s3://my-screenshots/captures/example-com.webp");

Try it live

Test the API in the interactive playground — pre-filled with Node.js snippets.

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
npm install node-fetch

Uses fetch (built-in) or node-fetch. Any HTTP client that supports GET requests will work.

Official SDK available

For TypeScript and Node.js projects, we offer an official SDK with typed responses, automatic retries, and built-in pagination for batch operations.

View SDK documentation →
More Examples

Code examples in other languages

Start capturing screenshots
with Node.js today.

100 free API calls per month. No credit card. First screenshot in under 30 seconds.

Take Screenshots with Node.js — ScreenshotDEV