Skip to content
shotsmith
Features Docs Guides Pricing Changelog Sign inStart free

How to take website screenshots in Node.js

Capture screenshots, PDFs and videos of any web page from Node.js with one fetch call: no Puppeteer, no Chrome to install.

4 min read

Taking a screenshot from Node.js usually means installing Puppeteer, downloading Chrome, and then discovering that it needs extra system libraries, fonts and memory once you deploy it. With shotsmith it’s one fetch call. Chrome runs on our side, cookie banners and ads are removed for you, and the response body is the image.

Everything below uses only what ships with Node 18 and later: fetch, URLSearchParams and fs.

1. Get an API key

Create a free account and copy your access key from the API keys page. The free plan includes 100 renders a month. Keep the key in an environment variable, such as SHOTSMITH_ACCESS_KEY, rather than in your code.

2. Take your first screenshot

Pass your key and a URL to /v1/take, then write the response to a file:

import { writeFile } from 'node:fs/promises';

const params = new URLSearchParams({
  access_key: 'YOUR_ACCESS_KEY',
  url: 'https://shotsmith.dev/sample',
  format: 'png',
});

const res = await fetch(`https://shotsmith.dev/v1/take?${params}`);
if (!res.ok) throw new Error((await res.json()).error_message);

await writeFile('capture.png', Buffer.from(await res.arrayBuffer()));

Run it as an ES module (save it as screenshot.mjs) so top-level await works: node screenshot.mjs.

3. Full page, on a phone, without the clutter

Options are just more query parameters. This captures the whole page as an iPhone 15 Pro sees it, with cookie banners and ads removed:

import { writeFile } from 'node:fs/promises';

const params = new URLSearchParams({
  access_key: 'YOUR_ACCESS_KEY',
  url: 'https://shotsmith.dev/sample',
  full_page: 'true',
  viewport_device: 'iphone_15_pro',
  block_cookie_banners: 'true',
  block_ads: 'true',
  format: 'jpg',
});

const res = await fetch(`https://shotsmith.dev/v1/take?${params}`);
if (!res.ok) throw new Error((await res.json()).error_message);

await writeFile('capture.jpg', Buffer.from(await res.arrayBuffer()));

The screenshots guide covers capturing a single element, hiding parts of the page, dark mode and waiting for content to load. All 134 options are in the option reference.

4. Save a page as PDF

Set format to pdf. A POST request with a JSON body is tidier once you have more than a couple of options:

import { writeFile } from 'node:fs/promises';

const res = await fetch('https://shotsmith.dev/v1/take', {
  method: 'POST',
  headers: {
    'X-Access-Key': 'YOUR_ACCESS_KEY',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    url: 'https://shotsmith.dev/sample',
    format: 'pdf',
    pdf_paper_format: 'a4',
    pdf_print_background: true,
  }),
});
if (!res.ok) throw new Error((await res.json()).error_message);

await writeFile('page.pdf', Buffer.from(await res.arrayBuffer()));

5. Serve screenshots from an Express route

A common pattern is an endpoint in your own app that returns a screenshot, for example for link previews. Two things matter here. Only allow the sites you expect, because anyone who can call the route spends your renders. And turn on cache, so repeat requests within four hours come back instantly and don’t count towards your quota.

import express from 'express';

const app = express();
const ALLOWED_HOSTS = ['example.com', 'www.example.com'];

// GET /screenshot?url=https://example.com/pricing
app.get('/screenshot', async (req, res) => {
  // Only allow sites you expect: anyone who can call this route spends your renders.
  let target;
  try {
    target = new URL(String(req.query.url));
  } catch {
    return res.status(400).send('Invalid URL');
  }
  if (!ALLOWED_HOSTS.includes(target.hostname)) return res.status(403).send('Not allowed');

  const params = new URLSearchParams({
    access_key: process.env.SHOTSMITH_ACCESS_KEY,
    url: target.href,
    format: 'webp',
    block_cookie_banners: 'true',
    cache: 'true', // repeat requests within 4 hours are instant and free
  });

  const upstream = await fetch(`https://shotsmith.dev/v1/take?${params}`);
  if (!upstream.ok) {
    const { error_code, error_message } = await upstream.json();
    return res.status(upstream.status).json({ error_code, error_message });
  }

  res.set('Content-Type', upstream.headers.get('content-type'));
  res.set('Cache-Control', 'public, max-age=3600');
  res.send(Buffer.from(await upstream.arrayBuffer()));
});

app.listen(3000);

Want to put screenshots straight into an <img> tag without a route like this? Sign the URL on your server instead: see embedding screenshots with signed URLs.

6. Handle errors

Errors come back as JSON with a stable error_code, the HTTP status, and a message that says what to change. Failed renders are never billed.

const res = await fetch(`https://shotsmith.dev/v1/take?${params}`);

if (!res.ok) {
  // Every error is JSON with a stable error_code and a message saying what to change.
  const { error_code, error_message } = await res.json();

  switch (error_code) {
    case 'selector_not_found':
      // The element you asked for isn't on the page: fall back to a normal screenshot.
      break;
    case 'timeout_error':
      // The page took longer than 18 seconds: retry with async=true.
      break;
    case 'too_many_requests':
    case 'worker_busy':
      // Retry after the number of seconds in the Retry-After header.
      break;
    default:
      throw new Error(`${error_code}: ${error_message}`);
  }
}

Every code is listed in the error reference.

7. Long pages and videos

Synchronous requests have to finish within 18 seconds. For slow pages, scrolling videos and batches, add async=true: you get a 202 straight away and a signed webhook when the file is ready. See going async.

Prefer a typed client?

The official Node SDK wraps all of this with a typed option builder, automatic retries, signed URLs and webhook verification. It has no dependencies and also runs on Bun, Deno and edge runtimes.