1. Tutorials
  2. How to scrape TikTok videos by hashtag

How to scrape TikTok videos by hashtag

Get TikTok videos by hashtag as JSON with one request. It costs 1 credit.

Get an API key; the examples use api.socialscrape.dev.

Before you start

  • An API key, sent in the x-api-key header. Keep it on your server.
  • For this endpoint you send hashtag. The TikTok videos by hashtag page lists every parameter.

Step 1: Send the request

GET /v1/tiktok/search/hashtag. Pick your language; each one reads the key from SOCIALSCRAPE_API_KEY.

1curl -G "https://api.socialscrape.dev/v1/tiktok/search/hashtag" \2  -H "x-api-key: $SOCIALSCRAPE_API_KEY" \3  --data-urlencode 'hashtag=booktok'

Step 2: Read the response

Results are in data.items. Here is what one item gives you, from a recorded sample.

1for item in body["data"]["items"]:2    print(item["text"])3    print(item["id"])4    print(item["stats"]["views"])
SampleFields printed above
1{2  "text": "#WorldBookDay is 4/23, and I've joined the #BookTok community to share our favorite books with you all! Duet to share your favorite read! #PassTheBook",3  "id": "7223827494913215786",4  "stats": {5    "views": 392006  }7}

A field the platform does not give comes back as null. The full field list is on the endpoint page.

Step 3: Get the next page

Send the same request with the cursor you got back, until has_more is false. Each page costs its own price.

1import os2import requests34url = "https://api.socialscrape.dev/v1/tiktok/search/hashtag"5headers = {"x-api-key": os.environ["SOCIALSCRAPE_API_KEY"]}6params = {"hashtag": "booktok"}78items = []9while len(items) < 100:10    body = requests.get(url, headers=headers, params=params, timeout=60).json()11    if not body["ok"]:12        raise RuntimeError(body["error"]["code"])13    items += body["data"]["items"]14    if not body["has_more"]:15        break16    params["cursor"] = body["cursor"]1718print(len(items), "items")

Step 4: Handle errors

Check ok, then branch on error.code and error.retryable, never on the message.

1body = res.json()2if not body["ok"]:3    err = body["error"]4    if err["retryable"]:5        ...  # wait a moment and send the same request again6    else:7        raise RuntimeError(f'{err["code"]}: {err["message"]}')
CodeHTTPRetryMeaning
invalid_params400noA parameter is missing, unknown or has the wrong type.
not_found404noThe platform says this profile, post or item does not exist.

Other errors, such as unauthorized or rate_limited, are on the errors page.

With the SDK

The Python and TypeScript clients wrap all of this: typed errors, retries, and cursor paging. Install them with pip install socialscrape-sdk or npm install socialscrape.

1from socialscrape import SocialScrape23client = SocialScrape()  # reads SOCIALSCRAPE_API_KEY45for item in client.paginate(client.tiktok.search_hashtag, hashtag="booktok", max_items=100):6    print(item["text"])

What it costs

1 credit. A failed request costs nothing. Add max_age_days to reuse a cached result for 0 credits. Results are kept 30 days.

Next

Questions

How do I watch TikTok videos by hashtag for new items?

Create a monitor on this endpoint and get new items at a webhook, instead of polling. See the monitors reference.

Which language should I use?

Any. It is plain HTTP and JSON, so every language with an HTTP client works. The tabs show the same request nine ways.