Code Examples
Complete examples showing how to capture screenshots in various programming languages.
cURL
curl -X POST "https://api.shotone.io/screenshot" \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://github.com",
"format": "png",
"fullPage": false,
"device": "desktop"
}' \
--output screenshot.png Node.js / TypeScript
import fs from 'fs';
async function captureScreenshot(url: string) {
const response = await fetch('https://api.shotone.io/screenshot', {
method: 'POST',
headers: {
'X-API-Key': process.env.SHOTONE_API_KEY!,
'Content-Type': 'application/json',
},
body: JSON.stringify({
url,
format: 'png',
fullPage: true,
device: 'desktop',
blockAds: true,
}),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.error);
}
const buffer = await response.arrayBuffer();
fs.writeFileSync('screenshot.png', Buffer.from(buffer));
}
captureScreenshot('https://github.com'); Python
import requests
import os
def capture_screenshot(url: str, output_path: str):
response = requests.post(
'https://api.shotone.io/screenshot',
headers={
'X-API-Key': os.environ['SHOTONE_API_KEY'],
'Content-Type': 'application/json',
},
json={
'url': url,
'format': 'png',
'fullPage': True,
'device': 'desktop',
'blockAds': True,
},
)
if response.status_code != 200:
raise Exception(response.json()['error'])
with open(output_path, 'wb') as f:
f.write(response.content)
capture_screenshot('https://github.com', 'screenshot.png') Go
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func captureScreenshot(url, outputPath string) error {
payload := map[string]interface{}{
"url": url,
"format": "png",
"fullPage": true,
"device": "desktop",
"blockAds": true,
}
body, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST",
"https://api.shotone.io/screenshot",
bytes.NewBuffer(body))
req.Header.Set("X-API-Key", os.Getenv("SHOTONE_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
return os.WriteFile(outputPath, data, 0644)
}
func main() {
captureScreenshot("https://github.com", "screenshot.png")
} PHP
<?php
function captureScreenshot(string $url, string $outputPath): void
{
$ch = curl_init('https://api.shotone.io/screenshot');
$payload = json_encode([
'url' => $url,
'format' => 'png',
'fullPage' => true,
'device' => 'desktop',
'blockAds' => true,
]);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'X-API-Key: ' . getenv('SHOTONE_API_KEY'),
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
throw new Exception(json_decode($response)->error);
}
file_put_contents($outputPath, $response);
}
captureScreenshot('https://github.com', 'screenshot.png'); Ruby
require 'net/http'
require 'json'
def capture_screenshot(url, output_path)
uri = URI('https://api.shotone.io/screenshot')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['X-API-Key'] = ENV['SHOTONE_API_KEY']
request['Content-Type'] = 'application/json'
request.body = {
url: url,
format: 'png',
fullPage: true,
device: 'desktop',
blockAds: true
}.to_json
response = http.request(request)
unless response.code == '200'
raise JSON.parse(response.body)['error']
end
File.binwrite(output_path, response.body)
end
capture_screenshot('https://github.com', 'screenshot.png') Error Handling
Always handle errors in your code:
- Check the HTTP status code
- Parse the error message from the JSON response
- Implement retry logic for transient errors (429, 502)
- Log errors for debugging
See Error Codes for a complete list of possible errors.