Signed requests
To embed a screenshot directly in a browser (an <img> tag, for example) without exposing your secret key, use a signed request. You send the public access key (the key's id) plus a signature computed on your server with the signing secret. Both are shown once when you create a key.
How it works
A signed URL is a GET of /screenshot (or its alias /take) with three extra query parameters: access_key, expires (a unix time in seconds, at most 7 days ahead) and signature. The signature is the hex HMAC-SHA256, keyed with the signing secret, of three lines joined by \n:
- the method,
GET; - the endpoint's path,
/screenshotor/take; - every query parameter except
signature— includingaccess_keyandexpires— sorted by name, each name and value percent-encoded asencodeURIComponentdoes, and joined askey=value&….
Because the signature covers every parameter, a signed URL only ever produces that one exact screenshot; changing any value invalidates it. It works only as a GET of the endpoint it was signed for, never on another endpoint or method, and it stops working at expires. Sign URLs when you render the page that shows them, with a lifetime that suits it.
Example
import crypto from "crypto";
const path = "/screenshot";
const params = {
url: "https://example.com",
format: "png",
full_page: "true",
access_key: "<access_key>",
expires: String(Math.floor(Date.now() / 1000) + 3600), // valid for an hour
};
const query = Object.keys(params)
.sort()
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
.join("&");
const signature = crypto
.createHmac("sha256", SIGNING_SECRET)
.update(`GET\n${path}\n${query}`)
.digest("hex");
const src = `https://screenshotforweb.com/api/v1${path}?${new URLSearchParams({ ...params, signature })}`;
// <img src={src} /> — the secret never reaches the clientUse signed requests for anything rendered client-side. For server-to-server calls, just send your secret key as a header — see Authentication.