ScreenshotOne API JavaScript SDK
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
-
TypeError: screenshotone.Client is not a constructor
cause Attempting to use `require()` for the `Client` class in a CommonJS module when the SDK is primarily designed for ESM or when incorrectly accessing named exports.fixFor ES Modules, use `import { Client } from 'screenshotone-api-sdk';`. If forced to use CommonJS, ensure you're accessing the named export correctly: `const { Client } = require('screenshotone-api-sdk');` -
APIError: Access key is not valid or provided. (Status: 401, Code: INVALID_ACCESS_KEY)
cause The provided access key is either missing, incorrect, or not active. This indicates a problem with authentication.fixDouble-check your `accessKey` and `secretKey` values. Ensure they are correctly copied from your ScreenshotOne.com dashboard and are being passed to the `Client` constructor. -
APIError: Signature is invalid. (Status: 403, Code: INVALID_SIGNATURE)
cause The request signature generated by the SDK (using your secret key) does not match the signature expected by the API. This often happens if the secret key is incorrect or corrupted.fixVerify that your `secretKey` is correct and has not been truncated or altered. Ensure no extra characters or whitespace are included. Regenerate your secret key on the ScreenshotOne.com dashboard if issues persist.
Warnings
- gotcha API keys (access key and secret key) are crucial for authentication and generating valid, signed URLs. Exposing these keys directly in client-side code or committing them to public repositories is a severe security risk. Always use environment variables or a secure configuration management system.
- gotcha Network requests for downloading screenshots (`client.take()`) are asynchronous operations and can fail due to various reasons (network issues, API limits, invalid options). Proper error handling, including specific `APIError` checks, is essential.
- breaking Major version updates to the underlying ScreenshotOne API might introduce breaking changes to options or responses that the SDK needs to adapt to. While the SDK aims to stay synchronized, always review the changelog for new major versions of the SDK itself.
Install
-
npm install screenshotone-api-sdk -
yarn add screenshotone-api-sdk -
pnpm add screenshotone-api-sdk
Imports
- Client
const Client = require('screenshotone-api-sdk').Client;import { Client } from 'screenshotone-api-sdk'; - TakeOptions
import screenshotone from 'screenshotone-api-sdk'; const options = screenshotone.TakeOptions.url(...);
import { TakeOptions } from 'screenshotone-api-sdk'; - APIError
import * as screenshotone from 'screenshotone-api-sdk'; if (error instanceof screenshotone.APIError) { ... }import { APIError } from 'screenshotone-api-sdk';
Quickstart
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();