Answers
How do I get the videos for a TikTok hashtag through an API?
Short answer
There is no hashtag feed in the catalog, so search the tag as a keyword with TikTok video search (/v1/tiktok/videos/search) and keep the videos whose hashtags list contains it. Each page of about 30 videos costs $0.002; 300 videos take 10 calls, $0.02.
A café chain launched #matchamonday and wants to see what people posted with it: who joined, which videos got liked, what they said in the captions. On TikTok you open the hashtag page and scroll. Through an API you cannot fetch that page directly, at least not here. What you can do is search for the tag and filter. For most campaign checks that gets you the videos that matter, the ones TikTok ranks highest.
How to do it
- Search the tag text with TikTok video search, as plain words:
matchamondayormatcha monday. - Each video comes back with a
hashtagslist. Keep only the videos whose list contains the tag, compared in lowercase. - Page with
page.next_cursoruntil you stop finding new matches. - Use
sort=most_likedto see the top of the tag first.
import os, requests
URL = "https://api.okdata.online/v1/tiktok/videos/search"
H = {"Authorization": f"Bearer {os.environ['OKDATA_API_KEY']}"}
TAG = "matchamonday"
found, cursor = {}, None
for _ in range(10): # at most 10 pages, about 300 videos
params = {"query": TAG, "sort": "most_liked", **({"cursor": cursor} if cursor else {})}
body = requests.get(URL, headers=H, params=params, timeout=60).json()
if body["code"] != "ok":
raise RuntimeError(f"{body['code']}: {body['message']}")
for v in body["data"]["items"]:
if TAG in [h.lower() for h in (v["hashtags"] or [])]:
found[v["id"]] = (v["author"]["username"], v["metrics"]["likes"], v["url"])
if not body["page"]["has_more"]:
break
cursor = body["page"]["next_cursor"]
print(len(found), "videos carry", "#" + TAG)
Cost
| Depth | Calls | Cost |
|---|---|---|
| First page | 1 | $0.002 |
| About 300 videos | 10 | $0.02 |
| 1,000 videos | 34 | $0.068 |
You pay for every page you fetch, including the videos the filter throws away. A distinctive tag wastes little; a generic one like #fyp wastes most of each page.
Where this falls short
- You get what search returns for the words, ranked by relevance or likes. That overlaps with the hashtag page but is not the same list, and it is not every video ever tagged.
- The tag's total view count shown on its TikTok page is not returned.
- There is no date filter; sort on
published_atto see the newest posts. - For a big, long-running tag, you will see the head of it, not the long tail.
When to use something else
- TikTok's Research API can query by hashtag name with a date range, and it is free for approved non-profit academic researchers.
- TikTok Creative Center shows which hashtags are trending by region for advertisers, without code.
- TikTokApi's hashtag feature exists, though its issue tracker is full of reports that it stops returning results.
For tracking what rises and falls over weeks, see trend research.
Updated