Quick Start with Requests
The requests library makes SnapAPI integration straightforward in Python:
import requests
import os
API_KEY = os.environ['SNAPAPI_KEY']
def screenshot(url, fmt='png', width=1280, height=800):
response = requests.get(
'https://apisnap.dev/api/screenshot',
params={'url': url, 'format': fmt, 'width': width, 'height': height},
headers={'Authorization': f'Bearer {API_KEY}'},
)
response.raise_for_status()
return response.content
# Usage
image = screenshot('https://github.com')
with open('github.png', 'wb') as f:
f.write(image)
print(f"Remaining: {response.headers.get('X-Usage-Remaining')}")
The response is raw binary data. Write it directly to a file or pass it to image processing libraries like Pillow.
Flask Integration
Add a screenshot endpoint to your Flask API:
from flask import Flask, request, Response
import requests as http
app = Flask(__name__)
@app.route('/preview')
def preview():
url = request.args.get('url')
if not url:
return {'error': 'URL required'}, 400
res = http.get(
'https://apisnap.dev/api/screenshot',
params={'url': url, 'format': 'png', 'width': 1200, 'height': 630},
headers={'Authorization': f'Bearer {os.environ["SNAPAPI_KEY"]}'},
)
if res.status_code != 200:
return {'error': 'Screenshot failed'}, 502
return Response(res.content, mimetype='image/png',
headers={'Cache-Control': 'public, max-age=86400'})
This creates a /preview?url=... endpoint that generates social-media-sized screenshots with 24-hour caching.
Start Capturing Screenshots Today
100 free screenshots per month. No credit card required. API key delivered instantly.
Get Started FreeAsync with AIOHTTP
For async Python applications (FastAPI, aiohttp), use an async HTTP client:
import aiohttp
import os
async def screenshot_async(url, fmt='png'):
async with aiohttp.ClientSession() as session:
params = {'url': url, 'format': fmt}
headers = {'Authorization': f'Bearer {os.environ["SNAPAPI_KEY"]}'}
async with session.get(
'https://apisnap.dev/api/screenshot',
params=params,
headers=headers
) as resp:
if resp.status != 200:
error = await resp.json()
raise Exception(error.get('error', 'Failed'))
return await resp.read()
Async integration is essential for high-throughput applications where you need to capture multiple screenshots concurrently without blocking.
Batch Processing
Process multiple URLs efficiently with concurrent requests:
import asyncio
import aiohttp
async def batch_screenshots(urls, max_concurrent=5):
semaphore = asyncio.Semaphore(max_concurrent)
results = {}
async def capture(session, url):
async with semaphore:
params = {'url': url, 'format': 'png'}
headers = {'Authorization': f'Bearer {os.environ["SNAPAPI_KEY"]}'}
async with session.get(
'https://apisnap.dev/api/screenshot',
params=params, headers=headers
) as resp:
if resp.status == 200:
results[url] = await resp.read()
async with aiohttp.ClientSession() as session:
tasks = [capture(session, url) for url in urls]
await asyncio.gather(*tasks)
return results
# Capture 20 sites concurrently (5 at a time)
urls = ['https://github.com', 'https://stripe.com', ...]
images = asyncio.run(batch_screenshots(urls))