> Documentation index: https://www.socialcrawl.dev/llms.txt > Every page is also available as Markdown by adding .md. # Python (/docs/quickstart/python) Read a profile, search many platforms and page through posts from Python with requests Each step adds to one file, `socialcrawl.py`. Run it with `python socialcrawl.py` after each step. ## Prerequisites - Python 3.10 or later. - 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 your API key Install `requests`, then set your key in the shell. ```bash title="Terminal" pip install requests export SOCIALCRAWL_API_KEY=sc_your_key_here ``` Keep the key on the server, out of your code and out of the browser. ## Get a profile Save this as `socialcrawl.py`. The `call` helper sends your key in the `x-api-key` header and stops when `success` is false. ```python title="socialcrawl.py" import json import os import requests API = "https://www.socialcrawl.dev" KEY = os.environ["SOCIALCRAWL_API_KEY"] def call(method, path, **kwargs): """Send one request. Raise with error.type and error.doc_url when success is false.""" res = requests.request( method, API + path, headers={"x-api-key": KEY}, timeout=60, **kwargs ) body = res.json() if not body["success"]: error = body["error"] raise RuntimeError(f"{error['type']}: {error['message']} ({error.get('doc_url')})") return body profile = call("GET", "/v1/tiktok/profile", params={"handle": "charlidamelio"}) print(f"followers: {profile['data']['author']['followers']} (credits_used {profile['credits_used']})") ``` Run `python socialcrawl.py`. It prints the follower count from `data.author.followers` and the `credits_used` of the call. Every answer carries `success`. Branch on it, then on `error.type`, never on the message text. ## Search across platforms Add this to `socialcrawl.py`. With `dry_run=1` the search runs nothing and is free. `data.estimate` holds what the real search would cost and how many seconds it would take. ```python title="socialcrawl.py" quote = call( "GET", "/v1/search/everywhere", params={"query": "oat milk", "format": "compact", "dry_run": 1}, ) estimate = quote["data"]["estimate"] print("estimate:", json.dumps({"credits": estimate["credits"], "seconds": estimate["seconds"]})) ``` Drop `dry_run` to run the search for 20 credits. ## Get posts and the next page Add this to `socialcrawl.py`. Each answer holds one page of posts in `data.items`. Send `pagination.next_cursor` back as `cursor`, and stop when `pagination.has_more` is false. ```python title="socialcrawl.py" params = {"handle": "charlidamelio"} for page in (1, 2): posts = call("GET", "/v1/tiktok/profile/videos", params=params) pagination = posts["pagination"] print( f"page {page}: {len(posts['data']['items'])} posts " f"(credits_used {posts['credits_used']}), next_cursor {pagination['next_cursor']}" ) if not pagination["has_more"]: break params["cursor"] = pagination["next_cursor"] ``` A page of posts costs 1 credit. Run `python socialcrawl.py` again to see all three steps. ## 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.