ScreenshotOne API JavaScript SDK

1.1.21 · active · verified Tue Apr 21

The `screenshotone-api-sdk` is the official client library for the ScreenshotOne.com API, enabling developers to programmatically generate and download screenshots of any website. It provides a fluent API for configuring various screenshot options, such as delay, ad blocking, and more, directly mirroring the latest API specifications. The current stable version is 1.1.21. The package appears to have an active release cadence, with continuous integration indicated by its build badge and consistent updates to synchronize with the underlying ScreenshotOne API. Key differentiators include its tight integration with the ScreenshotOne service, offering specific error handling for API-level issues, and providing both URL generation and direct screenshot download functionalities. It is designed for both JavaScript and TypeScript environments, shipping with its own type definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the ScreenshotOne client, configure screenshot options using the fluent API, generate a signed URL, and then download the resulting screenshot to a local file. It also includes error handling for both API-specific and generic errors.

import * as fs from 'fs';
import { Client, TakeOptions, APIError } from 'screenshotone-api-sdk';

// Create API client using environment variables for security
const accessKey = process.env.SCREENSHOTONE_ACCESS_KEY ?? '';
const secretKey = process.env.SCREENSHOTONE_SECRET_KEY ?? '';

if (!accessKey || !secretKey) {
  console.error('SCREENSHOTONE_ACCESS_KEY and SCREENSHOTONE_SECRET_KEY environment variables must be set.');
  process.exit(1);
}

const client = new Client(accessKey, secretKey);

async function generateAndDownloadScreenshot() {
  // Set up options for the screenshot
  const options = TakeOptions
      .url("https://www.example.com")
      .delay(2) // Wait 2 seconds before taking the screenshot
      .blockAds(true)
      .fullPage(true) // Capture the entire page
      .viewportWidth(1280) // Set viewport width
      .viewportHeight(800); // Set viewport height

  try {
      // Generate a signed URL for the screenshot
      const url = client.generateTakeURL(options);
      console.log('Generated screenshot URL:', url);

      // Download the screenshot directly
      console.log('Downloading screenshot...');
      const imageBlob = await client.take(options);

      const buffer = Buffer.from(await imageBlob.arrayBuffer());
      const outputPath = 'example-screenshot.png';
      fs.writeFileSync(outputPath, buffer);
      console.log(`Screenshot saved to ${outputPath}`);
  } catch (error) {
      if (error instanceof APIError) {
          console.error(`ScreenshotOne API Error: ${error.errorMessage} (Status: ${error.httpStatusCode}, Code: ${error.errorCode})`);
          console.error(`Documentation: ${error.documentationUrl}`);
      } else if (error instanceof Error) {
          console.error("An unexpected error occurred:", error.message);
      } else {
          console.error("An unknown error occurred:", error);
      }
  }
}

generateAndDownloadScreenshot();

view raw JSON →