Prerequisites
Before starting, make sure you have:- Python 3.10 or higher installed
- TikTokLive installed (
pip install TikTokLive) - A TikTok username to connect to (use
@tv_asahi_newsfor testing)
Create Your First Client
Import the library
Create a new Python file and import the necessary components:The
TikTokLiveClient is the main class for connecting to livestreams. Events like ConnectEvent and CommentEvent represent different actions that occur during a broadcast.Initialize the client
Create a client instance with the unique ID of the TikTok user whose livestream you want to monitor:
The
unique_id is the username visible in the TikTok URL. For example, @isaackogz from https://www.tiktok.com/@isaackogz.Add event listeners
Define async functions to handle events. You can use decorators or add listeners manually:All event handlers must be async functions since TikTokLive uses asyncio for non-blocking operations.
Complete Example
Here’s the full working code:Handling More Events
TikTokLive supports 100+ event types. Here are some commonly used ones:Non-Blocking Connection
If you want to run the client without blocking your main thread, usestart() instead of run():
Enabling Debug Logging
To see detailed connection logs and debug information:Next Steps
Core Concepts
Learn about the TikTokLiveClient and its properties
Event Handling
Master event listeners and handlers
Handling Gifts
Learn about gift streaks and metadata
Error Handling
Handle connection errors and exceptions