SDKs and integrations
PHP and Laravel SDK
The official SDK for PHP 8.2+, with a Laravel integration. It has typed options for every API option, request signing, automatic retries and webhook verification. It uses Guzzle for HTTP.
Install
composer require shotsmith/shotsmith-php
Take a screenshot
<?php
// composer require shotsmith/shotsmith-php
require 'vendor/autoload.php';
use Shotsmith\Client;
use Shotsmith\Options\TakeOptions;
$shotsmith = new Client('YOUR_ACCESS_KEY');
$options = TakeOptions::url('https://shotsmith.dev/sample')
->fullPage()
->blockCookieBanners()
->format('webp');
file_put_contents('capture.webp', $shotsmith->take($options));
// In Laravel: Shotsmith::take($options) with SHOTSMITH_ACCESS_KEY in .env
Content-Type: image/webp
X-Capture-Id: 01m8kvpk6jsmeykdfs0wy6awbn
X-Cache: MISS
X-Render-Time: 296
X-Quota-Limit: 15000
X-Quota-Remaining: 14998
X-RateLimit-Limit: 80
X-RateLimit-Remaining: 79
(the WebP image: take() returns its bytes as a string, written to capture.webp)
Options can be a builder, as above, or an array with the API’s names: $shotsmith->take(['url' => $url, 'full_page' => true]). Every option in the option reference has a builder method, named in camelCase.
The constructor takes accessKey, then the optional named arguments secretKey (only for signed URLs), baseUrl, timeout (seconds, default 120) and maxRetries (default 2).
Laravel
The package registers itself. Add your keys to .env:
SHOTSMITH_ACCESS_KEY=shot_… SHOTSMITH_SECRET_KEY=shot_sk_… # only needed for signed URLs SHOTSMITH_WEBHOOK_SECRET=whsec_…
Then use the facade, or inject Shotsmith\Client:
use Shotsmith\Laravel\Facades\Shotsmith;
use Shotsmith\Options\TakeOptions;
$pdf = Shotsmith::take(TakeOptions::url(route('invoices.show', $invoice))->format('pdf')->pdfPrintBackground());
Storage::put("invoices/{$invoice->id}.pdf", $pdf);
Content-Type: application/pdf
X-Capture-Id: 01m8kvpk6jsmeykdfs0wy6awbn
X-Cache: MISS
X-Render-Time: 1840
X-Quota-Limit: 15000
X-Quota-Remaining: 14998
X-RateLimit-Limit: 80
X-RateLimit-Remaining: 79
(the PDF: Shotsmith::take() returns its bytes, saved to invoices/{id}.pdf)
For thumbnails in Blade, a signed URL can’t be altered, so it’s safe in public markup:
<img src="{{ Shotsmith::generateTakeUrl(TakeOptions::url($site->url)->imageWidth(400)->cache()) }}" alt="">
SHOTSMITH_TIMEOUT and SHOTSMITH_MAX_RETRIES set the timeout and retries. Publish the config file with php artisan vendor:publish --tag=shotsmith-config if you need it.
Methods
| Method | Returns |
|---|---|
| take($options) | The file’s bytes as a string |
| takeToFile($options, $path) | Writes the file and returns the path |
| takeWithInfo($options) | TakeResult: bytes plus capture ID, cache hit, render time and quota left |
| takeJson($options) | The capture as an array: file URL, metadata, vision answer (example) |
| takeAsync($options) / waitFor($id) | Queue a render, then poll until it’s done (example) |
| animate($options) | A scrolling video or GIF (example) |
| generateTakeUrl($options) / generateAnimateUrl($options) | A signed URL for an <img> or <video> tag (example) |
| bulk($requests, $options, execute: true) | Up to 100 captures (example) |
| getCapture / listCaptures / deleteCapture | Capture history |
| listMonitors / createMonitor / deleteMonitor | Page monitors (example) |
| usage(), devices(), countries() | Account and reference data |
| Shotsmith\Webhook::verify($body, $signature, $secret) | true if a webhook’s signature is valid |
| Shotsmith\Webhook::parse($body, $signature, $secret) | The decoded event; throws if the signature is wrong (example) |
Errors
Failed calls throw Shotsmith\Exceptions\ShotsmithException with the API’s errorCode, the HTTP status, per-option details for validation errors, and captureId when a capture was created. isRetryable() tells you whether trying again could help. Rate limits (429) and busy workers (503) are already retried automatically, honouring Retry-After. See Errors for every code.
use Shotsmith\Exceptions\ShotsmithException;
try {
$shotsmith->take(['url' => 'https://shotsmith.dev/sample', 'selector' => '#newsletter-signup']);
} catch (ShotsmithException $e) {
if ($e->errorCode === 'selector_not_found') {
// fall back to a viewport screenshot
}
}