TikTokAPI Python Package

May 26, 2023 Updated Jul 5, 2026 View Project

TikTokAPI is an unofficial TikTok API for Python that pulls public TikTok data like trending videos, hashtags, users, sounds, and comments, no official API access needed (TikTok doesn’t offer one for most of this anyway). I built it in 2019 because researchers and developers had no real way to get data out of TikTok, and I’ve maintained it ever since.

Quickstart

pip install TikTokApi
python -m playwright install
from TikTokApi import TikTokApi
import asyncio
import os
 
ms_token = os.environ.get("ms_token", None)
 
async def trending_videos():
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, browser=os.getenv("TIKTOK_BROWSER", "chromium"))
        async for video in api.trending.videos(count=30):
            print(video)
            print(video.as_dict)
 
if __name__ == "__main__":
    asyncio.run(trending_videos())

Full setup instructions, examples for every endpoint, and the API reference are in the GitHub repository and the documentation site.

What you can do with TikTokAPI

Adoption

TikTokAPI has over 3 million downloads, 7.4K+ stars on GitHub, and 250+ companies using it or a derivative of it. It’s cited in 26+ peer-reviewed papers, including research from Yale, Northwestern, and a UNESCO (United Nations) publication, and organizations like tracking.exposed have built research on top of it. TikTok actively tries to block this kind of data collection, so for an unofficial library that level of adoption says a lot.

Academic citations

Researchers cite TikTokAPI because it’s often the only practical way to collect TikTok data at scale. Here are the peer-reviewed papers and books I’ve found on Google Scholar that use or cite the library. If you know of others, let me know.

Undergraduate Papers

How it works: defeating TikTok’s bot detection

Scraping TikTok is harder than scraping most websites. TikTok signs its API requests with security parameters generated by obfuscated JavaScript, and it fingerprints clients pretty aggressively to catch automation (headless browsers, missing browser APIs, timing anomalies, etc). If you just send a plain HTTP request to their endpoints it gets rejected.

TikTokAPI gets around this by running a real browser session with Playwright in the background to generate valid security parameters, and patching the things that give away an automated browser. It’s a cat and mouse game. TikTok’s engineers watch the repository and patch the techniques it relies on, so the library has to keep evolving with each round of countermeasures. The whole point of the package is to hide all of that so you can write a few lines of Python and get structured data back.

I also wrote about the broader questions this kind of work raises in Is Web Scraping Ethical?, which draws a lot on my experience maintaining this library.

A personal note

I started this project in high school and it’s shaped my career more than anything else I’ve built. The reverse engineering skills I picked up maintaining it led directly to my security research, and seeing researchers at Yale, Northwestern, and the UN cite my code showed me open-source work can matter well beyond download counts. I’m grateful to the community that’s grown around the project and keeps it going.

← Back to projects