Menu

How to Build a Screenshot API in Node and Puppeteer

How to Build a Screenshot API in Node and Puppeteer

Let’s be honest, sometimes you just need a quick screenshot of a webpage without having to manually click on 'PRTSC' or paying for a 3rd party tool. Maybe you’re generating previews for a newsletter, checking competitor designs, or throwing together an SEO report.

I didn’t want to rely on another third-party tool or bloated screenshot SaaS. So I built my own mini screenshot API using Node.js and Puppeteer. It’s about 50 lines of code and it just works.

For this implementation, you only need three things: Node.js, Express, and Puppeteer. That’s it. No API keys, no dashboards, no weird limitations.

The idea is simple: you hit an endpoint with a URL and it returns a PNG screenshot of that page.

Install Express and Puppeteer

To get started, you'll need a Node project with express and puppeteer installed:

npm install express puppeteer

Code Implementation

const express = require('express');
const puppeteer = require('puppeteer');
const app = express();

app.get('/screenshot', async (req, res) => {
  const { url } = req.query;
  if (!url) return res.status(400).send('Missing URL');

  const browser = await puppeteer.launch({ headless: 'new' });
  const page = await browser.newPage();
  await page.goto(url, { waitUntil: 'networkidle2' });
  const screenshot = await page.screenshot();

  await browser.close();
  res.set('Content-Type', 'image/png');
  res.send(screenshot);
});

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

To try it out:

node index.js

Then visit:
http://localhost:3000/screenshot?url=https://example.com

You’ll get a screenshot served directly in your browser.

I use this for generating quick preview thumbnails, monitoring layout changes, and debugging weird rendering issues. You can customize the viewport, delay rendering, or add auth headers if needed. It’s just JavaScript, so tweak away.

The funny part? I built this faster than I could write this post. Sometimes the smallest tools are the most useful.

Walter Guevara is a Computer Scientist, software engineer, startup founder and previous mentor for a coding bootcamp. He has been creating software for the past 20 years.
AD: "Heavy scripts slowing down your site? I use Fathom Analytics because it’s lightweight, fast, and doesn’t invade my users privacy." - Get $10 OFF your first invoice.

Community Comments

No comments posted yet

Code Your Own Classic Snake Game – The Right Way

Master the fundamentals of game development and JavaScript with a step-by-step guide that skips the fluff and gets straight to the real code.

Ad Unit

Current Poll

Help us and the community figure out what the latest trends in coding are.

Total Votes:
Q:
Submit

Add a comment