SnapService logoSnapService
Documentation

Getting Started with SnapService

Learn how to integrate our automated screenshot API into your applications. SnapService provides high-fidelity previews with intelligent caching.

API Endpoints

GEThttps://preview.coders.lt/api/screenshot

Returns PNG image bytes directly when cached.

GEThttps://preview.coders.lt/api/raw

Returns HTTP 302 redirect to the cached static image path.

Query Parameters

ParameterTypeDescription
urlstringThe target URL (must start with https://)
widthnumberViewport width (default: 480)
heightnumberViewport height (default: 270)

Allowed Resolutions

For security and performance, only specific viewport resolutions are supported. If only one dimension is provided, the API will automatically infer the other. If no match is found, it will default to 480x270.

320 x 568360 x 800375 x 667390 x 844414 x 896430 x 932480 x 2701280 x 7201366 x 7681440 x 9001536 x 8641920 x 1080

Response Behavior

200

Success

Returns the PNG image binary directly from cache on /api/screenshot.

302

Redirect

On /api/raw, cached images are returned as a redirect to a static PNG URL.

202

Accepted (Processing)

The screenshot is being generated in the background. A placeholder image is returned with aRefresh header.

Tip: Respect the Refresh header to automatically poll for completion.

Quick Example

The most efficient way to use the API is to check if the response contains a Refresh header.

HTTP/1.1 202 Accepted Content-Type: image/png Refresh: 5

JavaScript Implementation
/**
 * Fetches a screenshot and handles the asynchronous processing 
 * by respecting the 'Refresh' header.
 * 
 * @param {string} url - The URL to capture 
 * @param {HTMLImageElement} imgElement - The image element to update
 */
async function captureWithPolling(url, imgElement) {
  const apiEndpoint = `${origin}/api/screenshot?url=${encodeURIComponent(url)}`;
  
  // Initial request to trigger capture
  imgElement.src = apiEndpoint;
  
  // Polling function
  const poll = async () => {
    try {
      const response = await fetch(apiEndpoint, { method: 'HEAD' });
      
      // Check for the 'Refresh' header (usually set to '5' for 5 seconds)
      const refreshHeader = response.headers.get('Refresh');
      
      if (refreshHeader) {
        console.log(`Screenshot processing... retrying in ${refreshHeader}s`);
        setTimeout(poll, parseInt(refreshHeader) * 1000);
      } else {
        console.log("Screenshot ready!");
        imgElement.src = apiEndpoint;
      }
    } catch (err) {
      console.error("Polling failed:", err);
    }
  };

  // Start polling if needed (initially it might already be 200 or 202)
  // We'll wait a bit before first poll to let the initial request settle
  setTimeout(poll, 2000);
}