Take a screenshot of a URL in Node.js
Simple example
javascript
// import the module
const shotSnap = require("shotsnap");
// take screenshot
shotSnap.takeScreenshot({
pageUrl: "https://google.com",
saveFile: true,
fileName: "test.png",
});Result:
How it works? When you run this code this requests going to browser on cloud, its creates a new browser instance and take a screenshot. If you make too many requests you can be rate-limited.
Calling the website screenshot API over HTTP
takeScreenshot is a thin wrapper around a public endpoint, so any language that can send a GET request can turn a URL into a screenshot:
bash
curl -o example.png "https://shotsnap.vercel.app/api/screenshot?page=https://example.com&width=800&height=768"| Parameter | Meaning |
|---|---|
page | The URL to capture. URL-encode it when it has a query string of its own. |
width | Viewport width in pixels. The Node.js library sends 800 when you leave it out. |
height | Viewport height in pixels. The Node.js library sends 768 when you leave it out. |
The response is a PNG. It carries a one-day cache header, so the same URL and size can come back as a cached image.
Comparing this with running a browser yourself? See Puppeteer screenshot vs a hosted screenshot API.