Screenshot API for Django: Complete Integration Guide
Django is one of the most widely deployed Python web frameworks, powering SaaS platforms, analytics dashboards, content management systems, and API backends across every industry. If your Django application needs screenshot, scraping, or PDF generation capabilities, SnapAPI provides a clean REST API that integrates with any Django project in minutes. This guide covers everything from a basic requests integration to a production-ready service class with Celery task offloading, Redis caching, and webhook support.
Basic Integration with requests
The simplest SnapAPI integration in Django uses the requests library:
import requests
SNAPAPI_KEY = "your_api_key_here" # or use settings.SNAPAPI_KEY
def capture_screenshot(url, full_page=False, format="png"):
response = requests.post(
"https://snapapi.pics/api/screenshot",
headers={"X-API-Key": SNAPAPI_KEY},
json={"url": url, "full_page": full_page, "format": format},
timeout=30
)
response.raise_for_status()
return response.json()["url"]Django Settings Configuration
Store credentials in settings.py using environment variables:
# settings.py
import os
SNAPAPI_KEY = os.environ.get("SNAPAPI_KEY", "")
# Usage in views:
from django.conf import settings
import requests
def my_view(request):
screenshot_url = capture_screenshot(
settings.SNAPAPI_KEY, "https://example.com"
)Service Class Pattern
For clean architecture, wrap SnapAPI in a reusable service class:
# services/snapapi.py
import requests
from django.conf import settings
from django.core.cache import cache
import hashlib
class SnapApiService:
BASE_URL = "https://snapapi.pics"
def __init__(self):
self.api_key = settings.SNAPAPI_KEY
def _headers(self):
return {"X-API-Key": self.api_key, "Content-Type": "application/json"}
def screenshot(self, url, **kwargs):
cache_key = "snap:" + hashlib.md5(url.encode()).hexdigest()
cached = cache.get(cache_key)
if cached:
return cached
resp = requests.post(
self.BASE_URL + "/api/screenshot",
headers=self._headers(),
json={"url": url, "full_page": kwargs.get("full_page", False),
"format": kwargs.get("format", "png")},
timeout=30
)
resp.raise_for_status()
result = resp.json()["url"]
cache.set(cache_key, result, 900) # 15-minute cache
return result
def extract(self, url, fields):
resp = requests.post(
self.BASE_URL + "/api/extract",
headers=self._headers(),
json={"url": url, "fields": fields},
timeout=30
)
resp.raise_for_status()
return resp.json().get("data", {})
def pdf(self, url):
resp = requests.post(
self.BASE_URL + "/api/screenshot",
headers=self._headers(),
json={"url": url, "format": "pdf", "full_page": True},
timeout=60
)
resp.raise_for_status()
return resp.json()["url"]Celery Background Tasks
For async screenshot processing, offload to Celery:
# tasks.py
from celery import shared_task
from .services.snapapi import SnapApiService
from .models import ScreenshotRecord
@shared_task(bind=True, max_retries=3)
def capture_screenshot_async(self, record_id, url):
try:
service = SnapApiService()
screenshot_url = service.screenshot(url, full_page=True)
ScreenshotRecord.objects.filter(pk=record_id).update(
screenshot_url=screenshot_url,
status="complete"
)
except Exception as exc:
raise self.retry(exc=exc, countdown=10)
# In your view:
def request_screenshot(request):
record = ScreenshotRecord.objects.create(url=url, status="pending")
capture_screenshot_async.delay(record.id, url)
return JsonResponse({"record_id": record.id})PDF Export View
from django.http import StreamingHttpResponse
import requests
def export_report_pdf(request, report_id):
report_url = request.build_absolute_uri(f"/reports/{report_id}/")
service = SnapApiService()
pdf_url = service.pdf(report_url)
pdf_response = requests.get(pdf_url, stream=True)
response = StreamingHttpResponse(
pdf_response.iter_content(chunk_size=8192),
content_type="application/pdf"
)
response["Content-Disposition"] = f"attachment; filename=report-{report_id}.pdf"
return responseTesting with unittest.mock
from unittest.mock import patch, MagicMock
from django.test import TestCase
class SnapApiServiceTest(TestCase):
@patch("myapp.services.snapapi.requests.post")
def test_screenshot_returns_url(self, mock_post):
mock_resp = MagicMock()
mock_resp.json.return_value = {
"url": "https://cdn.snapapi.pics/test.png",
"captured_at": "2026-04-04T10:00:00Z"
}
mock_post.return_value = mock_resp
service = SnapApiService()
url = service.screenshot("https://example.com")
self.assertEqual(url, "https://cdn.snapapi.pics/test.png")
mock_post.assert_called_once()Getting Started
Create your free account at snapapi.pics/dashboard. The free plan gives you 200 monthly screenshots to build and validate your Django integration. For production use, the Starter plan ($19/month) provides 5,000 captures, and the Growth plan ($79/month) scales to 50,000 with full extract and scrape endpoint access. The Python SDK is available via pip: pip install snapapi-python.