TikTok-Api Python Wrapper

7.3.3 · active · verified Fri Apr 17

TikTok-Api is an unofficial, asynchronous Python 3 wrapper for interacting with the TikTok API. It leverages Playwright for headless browser automation to bypass bot detection and fetch data. Currently at version 7.3.3, it has a frequent release cadence, often with minor updates, but also introduces significant changes to handle TikTok's evolving anti-bot measures.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch trending TikTok videos. It highlights the use of `ms_token` for authentication/stability and the asynchronous nature of the library. Remember to run `playwright install` first.

import asyncio
from TikTokApi import TikTokApi
import os

async def main():
    # It is highly recommended to provide an ms_token for stability and to bypass some bot detections.
    # You can get this from your browser's cookies when logged into TikTok.
    ms_token = os.environ.get("TIKTOK_MS_TOKEN", "your_ms_token_here")

    # For development/debugging, you might need to set headless=False to see the browser.
    # You might also need to install Playwright browsers: `playwright install`.
    async with TikTokApi(ms_token=ms_token, headless=True) as api:
        # Create sessions. For more robust usage, consider num_sessions > 1 and proxy_providers.
        await api.create_sessions(num_sessions=1, sleep_after_for_retry=False)

        print("Fetching trending videos...")
        async for video in api.trending.videos(count=5):
            print(f"Video ID: {video.id}")
            print(f"Author: {video.author.nickname} (@{video.author.unique_id})")
            print(f"Description: {video.desc[:70]}...")
            print(f"Play URL: {video.video.play_addr.uri}")
            print("-" * 30)
            if video.stats:
                print(f"  Likes: {video.stats.digg_count}, Comments: {video.stats.comment_count}")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →