Screenshots
Capture high-quality screenshots of any webpage with full customization options including viewport, format, and blocking unwanted elements.
Basic Screenshot
Capture a simple screenshot of any URL:
basic-screenshot.ts
import { Stack0 } from '@stack0/sdk'const stack0 = new Stack0({apiKey: process.env.STACK0_API_KEY!})// Start screenshot capture (async)const { id, status } = await stack0.screenshots.capture({url: 'https://example.com',format: 'png',})// Poll for completionconst screenshot = await stack0.screenshots.get({ id })console.log('Image URL:', screenshot.imageUrl)
Screenshot and Wait
Capture a screenshot and automatically wait for completion:
screenshot-and-wait.ts
// This method handles polling automaticallyconst screenshot = await stack0.screenshots.captureAndWait({url: 'https://example.com',format: 'png',fullPage: true,}, {pollInterval: 1000, // Check every 1 second (default)timeout: 60000, // Timeout after 60 seconds (default)})console.log('Image URL:', screenshot.imageUrl)console.log('Dimensions:', screenshot.imageWidth, 'x', screenshot.imageHeight)
Screenshot Options
Full list of options for capturing screenshots:
| Option | Type | Description |
|---|---|---|
| url | string | URL to capture (required) |
| format | 'png' | 'jpeg' | 'webp' | 'pdf' | Output format (default: png) |
| quality | number | JPEG/WebP quality 0-100 |
| fullPage | boolean | Capture full page scroll |
| deviceType | 'desktop' | 'tablet' | 'mobile' | Device viewport preset |
| viewportWidth | number | Custom viewport width in pixels |
| viewportHeight | number | Custom viewport height in pixels |
| deviceScaleFactor | number | Retina scaling (1-3) |
| selector | string | CSS selector to capture specific element |
| clip | object | Capture specific region (x, y, width, height) |
Blocking Options
Remove unwanted elements from your screenshots:
blocking-options.ts
const screenshot = await stack0.screenshots.captureAndWait({url: 'https://example.com',format: 'png',// Block common annoyancesblockAds: true,blockCookieBanners: true,blockChatWidgets: true,blockTrackers: true,// Block specific URLsblockUrls: ['*analytics*','*tracking*',],// Block resource typesblockResources: ['image', 'font'], // Faster loading// Hide specific elementshideSelectors: ['.popup', '#banner', '.newsletter-signup'],})
Wait for Content
Wait for dynamic content to load before capturing:
wait-for-content.ts
const screenshot = await stack0.screenshots.captureAndWait({url: 'https://example.com/dashboard',format: 'png',// Wait for a specific element to appearwaitForSelector: '.chart-container',// Or wait a fixed amount of time (ms)waitForTimeout: 2000,// Click a button first (useful for modals, tabs, etc.)clickSelector: '.load-more-button',})
Custom Styles & Scripts
Inject custom CSS or JavaScript before capturing:
custom-styles.ts
const screenshot = await stack0.screenshots.captureAndWait({url: 'https://example.com',format: 'png',// Enable dark modedarkMode: true,// Inject custom CSScustomCss: `.ads, .popup { display: none !important; }body { background: #1a1a1a; }`,// Execute JavaScript before capturecustomJs: `document.querySelector('.lazy-load')?.click();window.scrollTo(0, 500);`,})
Authentication
Access authenticated pages with custom headers or cookies:
authentication.ts
const screenshot = await stack0.screenshots.captureAndWait({url: 'https://app.example.com/dashboard',format: 'png',// Set custom headersheaders: {'Authorization': 'Bearer your-token','X-Custom-Header': 'value',},// Or set cookiescookies: [{ name: 'session', value: 'abc123', domain: 'app.example.com' },{ name: 'auth_token', value: 'xyz789' },],// Custom user agentuserAgent: 'CustomBot/1.0',})
Batch Screenshots
Capture multiple URLs in a single batch job:
batch-screenshots.ts
// Start a batch jobconst batch = await stack0.screenshots.batch({urls: ['https://example.com','https://example.org','https://example.net',],config: {format: 'png',fullPage: true,blockAds: true,},})console.log('Batch ID:', batch.id)console.log('Total URLs:', batch.totalUrls)// Wait for completion with progress updatesconst completedJob = await stack0.screenshots.batchAndWait({urls: ['https://example.com', 'https://example.org'],config: { format: 'png' },}, {pollInterval: 2000,timeout: 300000, // 5 minutes})console.log(`Completed: ${completedJob.successfulUrls} success, ${completedJob.failedUrls} failed`)
Scheduled Screenshots
Create recurring screenshot schedules with change detection:
scheduled-screenshots.ts
// Create a schedule for daily screenshotsconst schedule = await stack0.screenshots.createSchedule({name: 'Daily homepage capture',url: 'https://example.com',frequency: 'daily', // 'hourly' | 'daily' | 'weekly' | 'monthly'config: {format: 'png',fullPage: true,blockAds: true,},// Detect visual changes between runsdetectChanges: true,changeThreshold: 5, // % change to trigger notification// Webhook notificationwebhookUrl: 'https://your-app.com/webhook',})// Toggle schedule on/offawait stack0.screenshots.toggleSchedule({ id: schedule.id })// List all schedulesconst { items } = await stack0.screenshots.listSchedules({isActive: true,})
List & Manage Screenshots
Query and manage your screenshots:
manage-screenshots.ts
// List screenshots with filtersconst { items, nextCursor } = await stack0.screenshots.list({status: 'completed',limit: 20,})for (const screenshot of items) {console.log(`[${screenshot.status}] ${screenshot.url}`)console.log(` Image: ${screenshot.imageUrl}`)console.log(` Size: ${screenshot.imageWidth}x${screenshot.imageHeight}`)}// Get a specific screenshotconst screenshot = await stack0.screenshots.get({ id: 'screenshot-id' })// Delete a screenshotawait stack0.screenshots.delete({ id: 'screenshot-id' })
Caching
Enable caching to avoid redundant screenshots:
caching.ts
const screenshot = await stack0.screenshots.captureAndWait({url: 'https://example.com',format: 'png',// Use a cache key to deduplicate requestscacheKey: 'homepage-desktop',// Cache TTL in seconds (default: 3600)cacheTtl: 86400, // 24 hours})// Same cacheKey within TTL returns cached result instantlyconst cached = await stack0.screenshots.captureAndWait({url: 'https://example.com',format: 'png',cacheKey: 'homepage-desktop',}) // Returns immediately with cached result