> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Django (/docs/quickstart/django) Add Django views that read a profile, search many platforms and page through posts The app serves three routes under `/api`, and each answers with the API's JSON unchanged. ## Prerequisites - Python 3.12 or later. - Django 6. - A SocialCrawl API key. Keys start with `sc_`. Get a free API key at https://www.socialcrawl.dev/dashboard/api. New accounts start with 100 welcome credits and no card is needed. ## Set up Install Django and `requests`, make a project and an app, and set your key in the shell. Then add these lines to the end of `quickstart/settings.py`. ```bash title="Terminal" pip install django requests django-admin startproject quickstart . python manage.py startapp socialcrawl export SOCIALCRAWL_API_KEY=sc_your_key_here ``` Keep the key on the server, out of your code and out of the browser. ```python title="quickstart/settings.py" import os INSTALLED_APPS += ["socialcrawl"] # Read the key from the environment, never from the code. SOCIALCRAWL_API_KEY = os.environ["SOCIALCRAWL_API_KEY"] ``` ## Create a client This code sends your key in the `x-api-key` header. ```python title="socialcrawl/client.py" import logging import requests from django.conf import settings from django.http import JsonResponse API = "https://www.socialcrawl.dev" logger = logging.getLogger(__name__) def socialcrawl(path, params): """Answer with the API's JSON and status. Log error.type and error.doc_url when success is false.""" res = requests.get( API + path, params=params, headers={"x-api-key": settings.SOCIALCRAWL_API_KEY}, timeout=60, ) body = res.json() if not body["success"]: logger.warning("%s: %s", body["error"]["type"], body["error"].get("doc_url")) return JsonResponse(body, status=res.status_code) ``` Branch on `success`, then on `error.type`, never on the message text. ## Create views Each of the three routes under `/api` makes one call and answers with what comes back. ```python title="socialcrawl/views.py" from .client import socialcrawl def profile(request): return socialcrawl("/v1/tiktok/profile", {"handle": request.GET.get("handle", "charlidamelio")}) def search(request): # dry_run=1 prices the search and runs nothing. query = request.GET.get("query", "oat milk") return socialcrawl("/v1/search/everywhere", {"query": query, "format": "compact", "dry_run": 1}) def posts(request): # Send the last answer's pagination.next_cursor back as ?cursor=. params = {"handle": request.GET.get("handle", "charlidamelio")} if request.GET.get("cursor"): params["cursor"] = request.GET["cursor"] return socialcrawl("/v1/tiktok/profile/videos", params) ``` With `dry_run=1` the search is free. Drop it to run the search for 20 credits. A page of posts costs 1 credit. Send `pagination.next_cursor` back as `cursor`, and stop when `pagination.has_more` is false. ## Wire up URLs Wire the routes into the app. The app's routes go under `/api/` in the project's `urls.py`. ```python title="socialcrawl/urls.py" from django.urls import path from . import views urlpatterns = [ path("profile", views.profile), path("search", views.search), path("posts", views.posts), ] ``` ```python title="quickstart/urls.py" from django.contrib import admin from django.urls import include, path urlpatterns = [ path("admin/", admin.site.urls), path("api/", include("socialcrawl.urls")), ] ``` ## Test it Start the app with `python manage.py runserver`, then call the three routes. The answers hold `data.author.followers`, `data.estimate` and `pagination.next_cursor`. ```bash title="Terminal" curl "http://localhost:8000/api/profile?handle=charlidamelio" curl "http://localhost:8000/api/search?query=oat+milk" curl "http://localhost:8000/api/posts?handle=charlidamelio&cursor=" ``` ## Next steps - [Price a watch for free](/docs/engines/monitors.md): POST /v1/monitors?dry_run=1 quotes a scheduled watch of an account and creates nothing. - [Common jobs](/docs/common-jobs.md): The first call for each job, from profiles to comments and reviews. - [Search](/docs/search.md): Choose between one platform - [Errors](/docs/errors.md): Every error type, what it means, and which ones to retry.