Node.js and Express power millions of web application backends. When your Express app needs to generate screenshots, SnapAPI provides a REST API that integrates naturally with Node's async patterns, Express middleware, and popular ecosystem tools like Axios, node-fetch, and got.
Installing and Configuring the SnapAPI Client
No special SDK is required. Use any HTTP client that supports binary responses. Install Axios if it is not already in your project:
npm install axios
Create lib/snapapi.js as a reusable module:
const axios = require("axios");
const SNAP_API = "https://snapapi.pics/screenshot";
const API_KEY = process.env.SNAPAPI_KEY;
async function capture(url, options = {}) {
const { data } = await axios.get(SNAP_API, {
params: {
access_key: API_KEY,
url,
viewport_width: options.width || 1280,
full_page: options.fullPage || false,
format: options.format || "png",
},
responseType: "arraybuffer",
timeout: 30000,
});
return Buffer.from(data);
}
module.exports = { capture };
Add SNAPAPI_KEY=your_key to your .env file and load it with dotenv. Never hardcode API keys in source files.
Express Route for On-Demand Screenshots
const express = require("express");
const snap = require("./lib/snapapi");
const router = express.Router();
router.post("/screenshot", async (req, res) => {
const { url, width } = req.body;
if (!url) return res.status(400).json({ error: "url required" });
try {
const img = await snap.capture(url, { width });
res.set("Content-Type", "image/png");
res.send(img);
} catch (err) {
res.status(502).json({ error: "capture failed", detail: err.message });
}
});
module.exports = router;
Async Queue with Bull for Background Captures
For production apps, process screenshot jobs in the background using Bull and Redis:
const Queue = require("bull");
const snap = require("./lib/snapapi");
const db = require("./db");
const screenshotQueue = new Queue("screenshots", process.env.REDIS_URL);
screenshotQueue.process(async (job) => {
const { recordId, url } = job.data;
const img = await snap.capture(url);
const key = `screenshots/${recordId}.png`;
await uploadToS3(key, img);
await db.records.update({ id: recordId }, { screenshotKey: key });
});
// Enqueue from your route:
// screenshotQueue.add({ recordId: record.id, url: record.url });
Use attempts: 3 and backoff: { type: "exponential", delay: 5000 } in the job options for automatic retry with backoff on failures.
Caching Screenshots with Node-Cache or Redis
const NodeCache = require("node-cache");
const cache = new NodeCache({ stdTTL: 86400 });
async function cachedCapture(url, options) {
const key = require("crypto").createHash("md5").update(url).digest("hex");
const hit = cache.get(key);
if (hit) return hit;
const img = await snap.capture(url, options);
cache.set(key, img);
return img;
}
For multi-instance deployments, replace node-cache with Redis-based caching using ioredis to share the cache across all Express instances.
Getting Started
Create a free account at snapapi.pics/dashboard. 200 screenshots per month, no credit card required. Full parameter reference at snapapi.pics/docs.