Skip to main content
Use a current supported Node.js version with native fetch and AbortSignal.timeout. No third-party HTTP client or published ScreenshotAPI SDK is required. The example uses the hosted API at https://api.screenshotapi.tech. Set SCREENSHOTAPI_API_KEY to an active key with screenshot:write and jobs:read permissions. You do not need to configure a base URL. The example deliberately submits an asynchronous PNG capture. It also accepts a direct completed result, polls pending jobs, honors rate-limit delays, checks nested job failure, and downloads the artifact without forwarding your API key to its host. Save the printed idempotency key. If submission fails at the network layer, recover by that key before creating another capture. CAPTURE_IDEMPOTENCY_KEY lets you replay the same logical submission; keep the request and API key unchanged. The overall client budget is three minutes. Reaching it does not cancel server-side work. In production, persist job handles and schedule polling or use webhooks rather than keeping a process alive for every capture. Save this as capture.mjs and run node capture.mjs:
See request recovery and response semantics before adapting this into a background worker.