Skip to content

JavaScript Screenshot API: End-to-End Example

A complete JavaScript/Node.js tutorial covering installation, screenshots, PDF generation, automation steps, data extraction, and S3 delivery with the ScreenshotCenter SDK.

Installation

Install the JavaScript SDK from npm:

npm install screenshotcenter

Basic screenshot

const ScreenshotCenter = require('screenshotcenter');
const client = new ScreenshotCenter({ apiKey: 'YOUR_API_KEY' });

const result = await client.create({ url: 'https://example.com' });
console.log(result.screenshot_url);

The SDK handles polling internally — create() resolves when the screenshot is ready.

ES module import

import ScreenshotCenter from 'screenshotcenter';
const client = new ScreenshotCenter({ apiKey: process.env.SCREENSHOT_API_KEY });

Full-page screenshot with options

const result = await client.create({
  url: 'https://example.com',
  fullPage: true,
  screenWidth: 1920,
  screenHeight: 1080,
  deviceScale: 2,
  delay: 2000,
  hideAds: true,
  hidePopups: true,
});

PDF generation

const result = await client.create({
  url: 'https://example.com/invoice',
  pdf: true,
  pdfFormat: 'A4',
  pdfMarginTop: '20mm',
  pdfMarginBottom: '20mm',
  pdfBackground: true,
});

// Download the PDF
await client.download(result.id, 'invoice.pdf');

See PDF generation API for all available options.

Automation steps

const result = await client.create({
  url: 'https://app.example.com/login',
  steps: [
    { action: 'type', target: '#email', value: 'user@example.com' },
    { action: 'type', target: '#password', value: 's3cret' },
    { action: 'click', target: 'button[type=submit]' },
    { action: 'wait', value: 3000 },
  ],
});

See authenticated screenshot API for login patterns including cookie injection and SSO.

Data extraction with trackers

const result = await client.create({
  url: 'https://example.com/product/123',
  trackers: [
    { name: 'price', selector: '.product-price', type: 'text' },
    { name: 'title', selector: 'h1', type: 'text' },
    { name: 'rating', selector: '.star-rating', type: 'attribute', attribute: 'data-score' },
  ],
});

// Access extracted data
for (const tracker of result.trackers ?? []) {
  console.log(`${tracker.name}: ${tracker.value}`);
}

Trackers extract DOM values in the same request as the screenshot. See web page data extraction.

Country routing

const result = await client.create({
  url: 'https://example.com',
  country: 'jp',
  locale: 'ja-JP',
  timezone: 'Asia/Tokyo',
});

Route through 80+ countries to verify localized content.

S3 delivery

const result = await client.create({
  url: 'https://example.com',
  apps: [{
    app: 's3',
    bucket: 'my-screenshots',
    path: 'captures/{yyyy}/{mm}/{dd}/{id}.png',
  }],
});

See S3 integration for configuration and IAM setup.

Batch screenshots

const batch = await client.batchCreate({
  urls: [
    'https://example.com/page1',
    'https://example.com/page2',
    'https://example.com/page3',
  ],
  options: { fullPage: true, country: 'us' },
});

// Poll for completion
const info = await client.batchInfo(batch.id);
console.log(`Progress: ${info.completed}/${info.total}`);

See batch screenshots at scale for CSV upload and advanced options.

Error handling

try {
  const result = await client.create({ url: 'https://example.com' });
} catch (err) {
  if (err.statusCode === 429) {
    console.log('Rate limited — retry after', err.retryAfter);
  } else {
    console.error('API error:', err.message);
  }
}

Express.js integration

const express = require('express');
const ScreenshotCenter = require('screenshotcenter');

const app = express();
const client = new ScreenshotCenter({ apiKey: process.env.SCREENSHOT_API_KEY });

app.get('/api/screenshot', async (req, res) => {
  const { url } = req.query;
  if (!url) return res.status(400).json({ error: 'url required' });

  const result = await client.create({ url, fullPage: true });
  res.json({ screenshotUrl: result.screenshot_url });
});

app.listen(3001);

Next steps

Browse the full JavaScript SDK reference for all methods and parameters. For Python, see the Python SDK.