Signed links
A screenshot as an image address, for an <img> tag: rendered on its first view, served from the cache after.
How it works
The link holds the options in its query, then key_id, the first 8 characters of your API key, and last signature: the HMAC-SHA256, in hex, of everything before &signature=, exactly as it is in the link, with your signing secret. Make it on your server: the secret never goes in a page. The key itself isn't in the link either.
import hashlib, hmac
from urllib.parse import urlencode
KEY_ID = "pp_xxxxx" # the first 8 characters of your API key
SECRET = "YOUR_SIGNING_SECRET"
def signed_link(options):
query = urlencode(options) + "&key_id=" + KEY_ID
signature = hmac.new(SECRET.encode(), query.encode(), hashlib.sha256).hexdigest()
return "https://api.pagepeeker.com/v3/shot?" + query + "&signature=" + signature
print(signed_link({"url": "https://example.com/", "width": 480, "height": 360}))
const KEY_ID = 'pp_xxxxx'; // the first 8 characters of your API key
const SECRET = 'YOUR_SIGNING_SECRET';
function signed_link(array $options): string
{
$query = http_build_query($options) . '&key_id=' . KEY_ID;
return 'https://api.pagepeeker.com/v3/shot?' . $query . '&signature=' . hash_hmac('sha256', $query, SECRET);
}
echo signed_link(['url' => 'https://example.com/', 'width' => 480, 'height' => 360]);
import { createHmac } from "node:crypto";
const KEY_ID = "pp_xxxxx"; // the first 8 characters of your API key
const SECRET = "YOUR_SIGNING_SECRET";
function signedLink(options) {
const query = new URLSearchParams(options).toString() + "&key_id=" + KEY_ID;
const signature = createHmac("sha256", SECRET).update(query).digest("hex");
return "https://api.pagepeeker.com/v3/shot?" + query + "&signature=" + signature;
}
console.log(signedLink({ url: "https://example.com/", width: 480, height: 360 }));
Any change to the link, an option or a character of its encoding, needs a new signature: the API checks the query exactly as it receives it. Booleans are true, false, 1 or 0 (lower case: Python's True is refused). wait is not taken.
What the browser gets
| Answer | When |
|---|---|
302 to the image | The render is in the cache, or was done in up to 30 seconds. The redirect may be cached for what is left of cache_ttl. |
| A "being rendered" image, 640 x 480 | The render wasn't done in 30 seconds. Not cached: the next view gets the render. |
A "not available" image, 640 x 480, with X-PP-Error-Code | The page could not be rendered (the header has the reason), or the link was refused: invalid_signature, a wrong option, the rate limit, the quota. |
What a view costs
- Requests: every view of the link is a request, and counts toward your requests per minute like any other call. A view the browser answers from its own cache never reaches us and counts nothing.
- Renders: a view costs a render only when the page is rendered: the first view, and the first one after
cache_ttlhas run out. Every other view is answered from the cache, for no render. - A wrong signature costs nothing: someone making up links can't use your requests or your renders.
A link with cache_ttl=2592000 (30 days, the most) costs at most one render every 30 days, however often it is viewed. A shorter cache_ttl gives a fresher image, for more renders.