Shotfly

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?

screenshot.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?

signed-url.php
<?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?

StatusCodesWhat to do
400invalid_params, url_not_allowedFix the request; details lists each problem
401, 403unauthorized, forbidden, signature_invalid, signature_expired, domain_blockedCheck the key, the signature or the target
402quota_exceeded, credits_limitUpgrade, wait for the next period, or raise max_credits
422target_blocked, blank_page, target_http_error, cookie_wallNot charged. Change the parameters (wait_for_selector, escalation=auto) or skip the page
429rate_limited, concurrency_limitedWait (Retry-After when present) and retry
500, 502, 503internal_error, render_failed, service_unavailableRetry with backoff; not charged
504render_timeoutRaise 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