Skip to main content

Unofficial TikTok API in Python

This is an unofficial api wrapper for TikTok.com in python. With this api you are able to call most trending and fetch specific user information as well as much more.

DOI LinkedIn Sponsor Me GitHub release (latest by date) GitHub Downloads Support Server

This api is designed to retrieve data TikTok. It can not be used post or upload content to TikTok on the behalf of a user. It has no support for any user-authenticated routes, if you can't access it while being logged out on their website you can't access it here.

Sponsors

These sponsors have paid to be placed here or are my own affiliate links which I may earn a commission from, and beyond that I do not have any affiliation with them. The TikTokAPI package will always be free and open-source. If you wish to be a sponsor of this project check out my GitHub sponsors page.

Table of Contents

Documentation

You can find the full documentation here

Getting Started

To get started using this API follow the instructions below.

Note: If you want to learn how to web scrape websites check my free and open-source course for learning everything web scraping

How to Support The Project

  • Star the repo 😎
  • Consider sponsoring me on GitHub
  • Send me an email or a LinkedIn message telling me what you're using the API for, I really like hearing what people are using it for.
  • Submit PRs for issues :)

Installing

Note: Installation requires python3.9+

If you run into an issue please check the closed issues on the github, although feel free to re-open a new issue if you find an issue that's been closed for a few months. The codebase can and does run into similar issues as it has before, because TikTok changes things up.

pip install TikTokApi
python -m playwright install

If you would prefer a video walk through of setting up this package YouTube video just for that. (is a version out of date, installation is the same though)

If you want a quick video to listen for TikTok Live events in python.

Docker Installation

Clone this repository onto a local machine (or just the Dockerfile since it installs TikTokApi from pip) then run the following commands.

docker pull mcr.microsoft.com/playwright:focal
docker build . -t tiktokapi:latest
docker run -v TikTokApi --rm tiktokapi:latest python3 your_script.py

Note this assumes your script is named your_script.py and lives in the root of this directory.

Common Issues

  • EmptyResponseException - this means TikTok is blocking the request and detects you're a bot. This can be a problem with your setup or the library itself

    • you may need a proxy to successfully scrape TikTok, I've made a web scraping lesson explaining the differences of "tiers" of proxies, I've personally had success with webshare's residential proxies (affiliate link), but you might have success on their free data center IPs or a cheaper competitor.
    • not sure which proxy provider will work for you? This library integrates with my ProxyProviders package, which makes it easy to try out and switch between different proxy providers with a single line via create_sessions(proxy_provider=...). See examples/proxy_provider_example.py for usage.
  • Browser Has no Attribute - make sure you ran python3 -m playwright install, if your error persists try the playwright-python quickstart guide and diagnose issues from there.

  • API methods returning Coroutine - many of the API's methods are async so make sure your program awaits them for proper functionality

Quick Start Guide

Here's a quick bit of code to get the most recent trending videos on TikTok. There's more examples in the examples directory.

Note: If you want to learn how to web scrape websites check my free and open-source course for web scraping

from TikTokApi import TikTokApi
import asyncio
import os

ms_token = os.environ.get("ms_token", None) # get your own ms_token from your cookies on tiktok.com

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())

To directly run the example scripts from the repository root, use the -m option on python.

python -m examples.trending_example

You can access the full data dictionary the object was created from with .as_dict. On a video this may look like this. TikTok changes their structure from time to time so it's worth investigating the structure of the dictionary when you use this package.

Metadata

Release files for TikTokApi 7.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for TikTokApi 7.3.5
File Size Uploaded Provenance Actions
tiktokapi-7.3.5.tar.gz 50.0 kB Without provenance Details for tiktokapi-7.3.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for TikTokApi 7.3.5
File Interpreter ABI Platform Actions
tiktokapi-7.3.5-py3-none-any.whl Python 3 none any Details for tiktokapi-7.3.5-py3-none-any.whl

Total release size: 115.5 kB

Release files / tiktokapi-7.3.5.tar.gz

Download URL tiktokapi-7.3.5.tar.gz
Size 50.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1a3dec72338668e4bbf0ae064a85f7cc7b7addb475c81b5fd23a79d0145ac90c
BLAKE2b-256 checksum
How to use checksums
340ce285b94336633dd73fefd9db012c27ec34c62f572bdccc019a6028341d10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / tiktokapi-7.3.5-py3-none-any.whl

Download URL tiktokapi-7.3.5-py3-none-any.whl
Size 65.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
898a4c74660ae9974e8d8ca3adf3fafbd89b084c399ef8905262792580fd7180
BLAKE2b-256 checksum
How to use checksums
ce315bf37364e9ae496365f7b96e96b76e8f023fcf911b9ab874eb6bf70de6f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for TikTokApi 7.3.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

7.3.5 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page