Node.js SDK — Quick Start

Install the SnapAPI Node.js SDK and capture your first screenshot in under a minute. Supports JavaScript and TypeScript.

On This Page

Installation

Install the official SnapAPI package from npm:

npm install snapapi

Or with yarn:

yarn add snapapi

Authentication

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);
💡 Tip: Never hardcode your API key. Use environment variables or a .env file with dotenv.

Take Screenshots

Basic Screenshot

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();

Full Page Screenshot

const image = await client.screenshot({
  url: 'https://example.com',
  format: 'png',
  fullPage: true,
  width: 1280
});

Screenshot with Options

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
});

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.png

Generate PDFs

const 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);

PDF from HTML String

const pdf = await client.pdf({
  html: '<h1>Invoice #1234</h1><p>Total: $99.00</p>',
  format: 'A4',
  printBackground: true
});

Extract Content

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...

Extract Structured Data

const data = await client.extract({
  url: 'https://example.com/product',
  format: 'structured',
  schema: {
    title: 'string',
    price: 'number',
    description: 'string'
  }
});

Error Handling

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);
  }
}
⚠️ Rate Limits: Free tier allows 200 requests/month. If you hit the limit, the API returns 429 Too Many Requests with a Retry-After header. Upgrade your plan for higher limits.

Advanced Options

Custom Headers & Cookies

const image = await client.screenshot({
  url: 'https://example.com/dashboard',
  headers: {
    'Authorization': 'Bearer your-token',
    'Cookie': 'session=abc123'
  }
});

Viewport & Device Emulation

// 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
});

TypeScript Support

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);

Next Steps