requests library to call the Screenshotly API from Python.
Prerequisites
pip install requests
Capture a screenshot
Binary response (default)
import requests
import os
API_KEY = os.environ['SCREENSHOTLY_API_KEY']
CAPTURE_URL = 'https://api.screenshotly.dev/v1/capture'
def capture_screenshot(url, output_path='screenshot.png', **options):
headers = {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
}
payload = {
'url': url,
'options': options
}
response = requests.post(CAPTURE_URL, headers=headers, json=payload)
response.raise_for_status()
with open(output_path, 'wb') as f:
f.write(response.content)
print(f'Saved to {output_path}')
capture_screenshot(
'https://example.com',
output_path='example.png',
format='png',
viewport_width=1280,
viewport_height=800
)
JSON response
def capture_screenshot_json(url, **options):
headers = {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
}
payload = {
'url': url,
'options': {**options, 'response_type': 'json'}
}
response = requests.post(CAPTURE_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()
result = capture_screenshot_json(
'https://example.com',
format='png',
viewport_width=1280
)
print('Image URL:', result['data']['image_url'])
print('Remaining credits:', result['remaining_credits'])
Full-page capture
result = capture_screenshot_json(
'https://example.com',
full_page=True,
format='jpeg',
image_quality=80
)
Check screenshot status
SCREENSHOTS_URL = 'https://api.screenshotly.dev/v1/screenshots'
def get_screenshot_status(screenshot_id):
headers = {'X-API-Key': API_KEY}
response = requests.get(
f'{SCREENSHOTS_URL}/{screenshot_id}/status',
headers=headers
)
response.raise_for_status()
return response.json()
status = get_screenshot_status('67b8a1c2d3e4f5a6b7c8d9e0')
print('Status:', status['data']['status'])
print('Image URL:', status['data'].get('imageUrl'))
Error handling
def capture_screenshot_safe(url, **options):
headers = {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
}
payload = {
'url': url,
'options': {**options, 'response_type': 'json'}
}
try:
response = requests.post(CAPTURE_URL, headers=headers, json=payload)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as e:
error = e.response.json().get('error', {})
print(f"Error [{error.get('code')}]: {error.get('message')}")
except requests.exceptions.ConnectionError:
print('Connection error — check your network')
except requests.exceptions.Timeout:
print('Request timed out')
return None
Batch processing
def capture_batch(urls, **options):
headers = {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
}
payload = {
'urls': urls,
'options': options
}
response = requests.post(
'https://api.screenshotly.dev/v1/capture/batch',
headers=headers,
json=payload
)
response.raise_for_status()
return response.json()
result = capture_batch(
['https://example.com', 'https://google.com', 'https://github.com'],
format='png',
viewport_width=1280
)
print(f"Succeeded: {result['succeededCount']}, Failed: {result['failedCount']}")
print(f"Remaining credits: {result['remainingCredits']}")
Check usage
def get_usage():
headers = {'X-API-Key': API_KEY}
response = requests.get(
f'{SCREENSHOTS_URL}/usage',
headers=headers
)
response.raise_for_status()
return response.json()
usage = get_usage()
data = usage['data']
print(f"Plan: {data['subscription']['plan']}")
print(f"Usage: {data['usage']}/{data['limit']} ({data['percentUsed']}%)")
print(f"Remaining: {data['remaining']}")

