Skip to main content

Basic Screenshot

curl -X POST https://api.snapopa.com/capture \
  -H "Authorization: Bearer $SNAPOPA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}'

Full Configuration

async function captureWithOptions(options) {
  const response = await fetch('https://api.snapopa.com/capture', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.SNAPOPA_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      url: options.url,
      format: options.format || 'png',
      viewport: {
        width: options.width || 1920,
        height: options.height || 1080,
      },
      isFullPage: options.fullPage || false,
      quality: options.quality || 95,
      waitUntil: options.waitUntil || 'domcontentloaded',
      responseCache: {
        enabled: options.cache || false,
        ttl: options.cacheTtl || 86400,
      },
    }),
  });

  return response.json();
}

// Usage
const result = await captureWithOptions({
  url: 'https://example.com',
  format: 'webp',
  width: 1280,
  height: 720,
  fullPage: true,
  cache: true,
});

Download to File

import { writeFile } from 'fs/promises';

async function downloadScreenshot(url, outputPath) {
  const response = await fetch('https://api.snapopa.com/capture', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.SNAPOPA_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      url,
      responseType: 'file',
    }),
  });

  const buffer = await response.arrayBuffer();
  await writeFile(outputPath, Buffer.from(buffer));
}

// Usage
await downloadScreenshot('https://example.com', './screenshot.png');

Error Handling

async function captureWithRetry(url, maxRetries = 3) {
  for (let attempt = 0; attempt < maxRetries; attempt++) {
    try {
      const response = await fetch('https://api.snapopa.com/capture', {
        method: 'POST',
        headers: {
          'Authorization': `Bearer ${process.env.SNAPOPA_API_KEY}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ url }),
      });

      if (response.status === 429) {
        const resetTime = response.headers.get('X-RateLimit-Reset');
        const waitMs = Math.max((resetTime * 1000) - Date.now(), 1000);
        await new Promise(r => setTimeout(r, waitMs));
        continue;
      }

      if (!response.ok) {
        throw new Error(`HTTP ${response.status}`);
      }

      return response.json();
    } catch (error) {
      if (attempt === maxRetries - 1) throw error;
      await new Promise(r => setTimeout(r, Math.pow(2, attempt) * 1000));
    }
  }
}