Install the SnapAPI Node.js SDK and capture your first screenshot in under a minute. Supports JavaScript and TypeScript.
Install the official SnapAPI package from npm:
npm install snapapi
Or with yarn:
yarn add snapapi
Get your API key from the SnapAPI dashboard and initialize the client:
const SnapAPI = require('snapapi');
// Initialize with your API key
const client = new SnapAPI('YOUR_API_KEY');
// Or use an environment variable (recommended)
const client = new SnapAPI(process.env.SNAPAPI_KEY);.env file with dotenv.
const fs = require('fs');
const SnapAPI = require('snapapi');
const client = new SnapAPI(process.env.SNAPAPI_KEY);
async function captureScreenshot() {
const image = await client.screenshot({
url: 'https://example.com',
format: 'png',
width: 1280,
height: 800
});
fs.writeFileSync('screenshot.png', image);
console.log('Screenshot saved!');
}
captureScreenshot();const image = await client.screenshot({
url: 'https://example.com',
format: 'png',
fullPage: true,
width: 1280
});const image = await client.screenshot({
url: 'https://example.com',
format: 'avif', // png, jpeg, webp, avif
width: 1440,
height: 900,
fullPage: false,
blockAds: true, // Block ads
blockCookies: true, // Block cookie banners
delay: 2000, // Wait 2s before capture
devicePreset: 'iPhone15', // Mobile device emulation
css: 'body { background: white; }', // Inject CSS
responseType: 'url' // Get hosted URL instead of binary
});const result = await client.screenshot({
url: 'https://example.com',
format: 'png',
responseType: 'url'
});
console.log(result.url);
// → https://cdn.snapapi.pics/screenshots/abc123.pngconst pdf = await client.pdf({
url: 'https://example.com',
format: 'A4',
printBackground: true,
margin: {
top: '20mm',
bottom: '20mm',
left: '15mm',
right: '15mm'
}
});
fs.writeFileSync('page.pdf', pdf);const pdf = await client.pdf({
html: '<h1>Invoice #1234</h1><p>Total: $99.00</p>',
format: 'A4',
printBackground: true
});Extract clean markdown or structured data from any webpage — perfect for LLM pipelines.
const content = await client.extract({
url: 'https://example.com/blog-post',
format: 'markdown'
});
console.log(content.markdown);
// → # Blog Post Title\n\nArticle content here...const data = await client.extract({
url: 'https://example.com/product',
format: 'structured',
schema: {
title: 'string',
price: 'number',
description: 'string'
}
});The SDK throws descriptive errors for common issues:
const SnapAPI = require('snapapi');
const client = new SnapAPI(process.env.SNAPAPI_KEY);
try {
const image = await client.screenshot({
url: 'https://example.com'
});
fs.writeFileSync('screenshot.png', image);
} catch (error) {
if (error.status === 401) {
console.error('Invalid API key');
} else if (error.status === 429) {
console.error('Rate limit exceeded. Retry after:', error.retryAfter);
} else if (error.status === 400) {
console.error('Bad request:', error.message);
} else {
console.error('Unexpected error:', error.message);
}
}429 Too Many Requests with a Retry-After header. Upgrade your plan for higher limits.
const image = await client.screenshot({
url: 'https://example.com/dashboard',
headers: {
'Authorization': 'Bearer your-token',
'Cookie': 'session=abc123'
}
});// Use a device preset
const mobile = await client.screenshot({
url: 'https://example.com',
devicePreset: 'iPhone15' // 26+ presets available
});
// Or set custom viewport
const custom = await client.screenshot({
url: 'https://example.com',
width: 375,
height: 812,
deviceScaleFactor: 3
});import SnapAPI, { ScreenshotOptions, PDFOptions } from 'snapapi';
const client = new SnapAPI(process.env.SNAPAPI_KEY!);
const options: ScreenshotOptions = {
url: 'https://example.com',
format: 'png',
width: 1280
};
const image: Buffer = await client.screenshot(options);