API reference
Website screenshots with PHP
From PHP, call /v1/screenshot with the curl extension (ext-curl, included in most PHP builds): send the page URL and your secret key, then write the response body to a file. The examples need PHP 8 and read the key from the SHOTFLY_SECRET_KEY environment variable.
How do I take a screenshot in PHP?
<?php
$query = http_build_query(['url' => 'https://example.com', 'full_page' => 'true', 'format' => 'webp']);
$ch = curl_init("https://api.shotfly.dev/v1/screenshot?$query");
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('SHOTFLY_SECRET_KEY')],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_TIMEOUT => 90,
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $headerSize);
$body = substr($response, $headerSize);
if ($status !== 200) {
// Failures cost nothing. A proxy in front of the API can answer with HTML, not JSON.
$error = json_decode($body, true)['error'] ?? ['code' => "http_$status", 'message' => 'no details'];
throw new RuntimeException("{$error['code']}: {$error['message']}");
}
file_put_contents('example.webp', $body);
preg_match('/^x-credits-charged:\s*(\d+)/mi', $headers, $credits);
echo 'saved example.webp, ' . ($credits[1] ?? '?') . " credit\n";A non-200 response is JSON with an error code. Failed requests cost nothing; retry 429 and 5xx responses, and treat the others as final (see the table below).
How do I embed a screenshot without exposing my key?
<?php
/** A URL for <img src> or og:image: it carries the public key and a signature, never the secret. */
function signed_screenshot_url(array $params, string $publicKey, string $secret, ?int $expiresIn = null): string
{
$params = array_map('strval', $params); // pass booleans as 'true' or 'false'
$params['access_key'] = $publicKey;
if ($expiresIn !== null) {
$params['expires'] = (string) (time() + $expiresIn);
}
ksort($params, SORT_STRING);
$pairs = [];
foreach ($params as $key => $value) {
$pairs[] = rawurlencode($key) . '=' . rawurlencode($value);
}
$signature = hash_hmac('sha256', implode('&', $pairs), $secret);
return 'https://api.shotfly.dev/v1/screenshot?' . http_build_query($params + ['signature' => $signature], '', '&', PHP_QUERY_RFC3986);
}
echo signed_screenshot_url(
['url' => 'https://example.com', 'viewport_width' => 1200, 'viewport_height' => 630],
getenv('SHOTFLY_PUBLIC_KEY'),
getenv('SHOTFLY_SECRET_KEY'),
86400,
) . "\n";Put the URL in <img src> or an og:image meta tag. It carries your public key (pk_…) and a signature; the secret stays on your server, and changing any parameter breaks the signature.
Which errors should I handle?
| Status | Codes | What to do |
|---|---|---|
| 400 | invalid_params, url_not_allowed | Fix the request; details lists each problem |
| 401, 403 | unauthorized, forbidden, signature_invalid, signature_expired, domain_blocked | Check the key, the signature or the target |
| 402 | quota_exceeded, credits_limit | Upgrade, wait for the next period, or raise max_credits |
| 422 | target_blocked, blank_page, target_http_error, cookie_wall | Not charged. Change the parameters (wait_for_selector, escalation=auto) or skip the page |
| 429 | rate_limited, concurrency_limited | Wait (Retry-After when present) and retry |
| 500, 502, 503 | internal_error, render_failed, service_unavailable | Retry with backoff; not charged |
| 504 | render_timeout | Raise timeout, or queue the capture as an async job |
Every error is JSON: { "error": { "code", "message", "request_id", "docs_url" } }. See error codes for the full list.
Last updated 2026-12-01. Markdown version: /docs/guides/php.md