Use this file to discover all available pages before exploring further.
This guide walks you through creating a basic TikTok livestream client that connects to a live broadcast and handles real-time events like comments, gifts, and likes.
Create a new Python file and import the necessary components:
from TikTokLive import TikTokLiveClientfrom TikTokLive.events import ConnectEvent, CommentEvent
The TikTokLiveClient is the main class for connecting to livestreams. Events like ConnectEvent and CommentEvent represent different actions that occur during a broadcast.
2
Initialize the client
Create a client instance with the unique ID of the TikTok user whose livestream you want to monitor:
client = TikTokLiveClient(unique_id="@isaackogz")
The unique_id is the username visible in the TikTok URL. For example, @isaackogz from https://www.tiktok.com/@isaackogz.
3
Add event listeners
Define async functions to handle events. You can use decorators or add listeners manually:
from TikTokLive import TikTokLiveClientfrom TikTokLive.events import ConnectEvent, CommentEvent# Create the clientclient = TikTokLiveClient(unique_id="@isaackogz")# Listen to an event with a decorator@client.on(ConnectEvent)async def on_connect(event: ConnectEvent): print(f"Connected to @{event.unique_id} (Room ID: {client.room_id})")@client.on(CommentEvent)async def on_comment(event: CommentEvent): print(f"{event.user.nickname} -> {event.comment}")if __name__ == '__main__': # Run the client and block the main thread client.run()
Make sure the user is currently live before running your client. If they’re offline, the connection will fail. See Checking Live Status to learn how to check if a user is live.
TikTokLive supports 100+ event types. Here are some commonly used ones:
from TikTokLive.events import ( ConnectEvent, DisconnectEvent, CommentEvent, GiftEvent, LikeEvent, FollowEvent, ShareEvent, JoinEvent)@client.on(GiftEvent)async def on_gift(event: GiftEvent): # Check if gift streak is over if event.gift.streakable and not event.streaking: print(f"{event.user.nickname} sent {event.repeat_count}x {event.gift.name}") elif not event.gift.streakable: print(f"{event.user.nickname} sent {event.gift.name}")@client.on(LikeEvent)async def on_like(event: LikeEvent): print(f"{event.user.nickname} liked the stream")@client.on(FollowEvent)async def on_follow(event: FollowEvent): print(f"{event.user.nickname} followed the host")@client.on(ShareEvent)async def on_share(event: ShareEvent): print(f"{event.user.nickname} shared the stream")@client.on(DisconnectEvent)async def on_disconnect(event: DisconnectEvent): print("Disconnected from the livestream")
To see detailed connection logs and debug information:
from TikTokLive import TikTokLiveClientfrom TikTokLive.client.logger import LogLevelclient = TikTokLiveClient(unique_id="@isaackogz")# Set log level to INFO or DEBUGclient.logger.setLevel(LogLevel.INFO.value)if __name__ == '__main__': client.run()