Skip to main content
TikTokLive allows you to connect to livestreams as a logged-in user by providing a session ID. This enables access to features available only to authenticated users.

What is an Authenticated Session?

By default, TikTokLive connects to livestreams as an anonymous viewer. However, you can authenticate by providing your TikTok session ID, allowing you to:
  • View subscriber-only streams
  • Access authenticated-only features
  • Appear as a logged-in user in the stream
Never share your session ID publicly or commit it to version control. Treat it like a password.

Getting Your Session ID

To get your TikTok session ID:
1

Open TikTok in your browser

Navigate to tiktok.com and log in to your account.
2

Open Developer Tools

Press F12 or right-click and select “Inspect” to open browser developer tools.
3

Go to Application/Storage tab

Find the “Application” tab (Chrome) or “Storage” tab (Firefox).
4

Find session cookie

Navigate to Cookies → https://www.tiktok.com and look for the sessionid cookie. Copy its value.

Setting the Session ID

You must set the session ID before connecting to the livestream.
1

Import required modules

from TikTokLive import TikTokLiveClient
from TikTokLive.events import ConnectEvent
2

Create the client

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="@username"
)
3

Set session ID before connecting

# Set the login session ID token BEFORE connecting
client.web.set_session("your-session-id-here")
4

Connect to the livestream

client.run()

Complete Example

from TikTokLive.client.client import TikTokLiveClient
from TikTokLive.client.logger import LogLevel
from TikTokLive.events import ConnectEvent

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="@tv_asahi_news"
)


@client.on(ConnectEvent)
async def on_connect(event: ConnectEvent):
    client.logger.info(f"Connected to @{event.unique_id}!")


if __name__ == '__main__':
    # Enable download info
    client.logger.setLevel(LogLevel.INFO.value)

    # Set the login session ID token BEFORE connecting
    client.web.set_session("session-id-here")

    # Connect
    client.run()

Using Environment Variables

For better security, store your session ID in an environment variable:
import os
from TikTokLive import TikTokLiveClient

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="@username"
)

# Get session ID from environment variable
session_id = os.getenv("TIKTOK_SESSION_ID")
if session_id:
    client.web.set_session(session_id)

client.run()
Then set the environment variable:
export TIKTOK_SESSION_ID="your-session-id-here"
python your_script.py

Using a .env File

For local development, use a .env file:
1

Install python-dotenv

pip install python-dotenv
2

Create .env file

TIKTOK_SESSION_ID=your-session-id-here
3

Load in your script

import os
from dotenv import load_dotenv
from TikTokLive import TikTokLiveClient

# Load environment variables
load_dotenv()

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="@username"
)

client.web.set_session(os.getenv("TIKTOK_SESSION_ID"))
client.run()
4

Add .env to .gitignore

Make sure to add .env to your .gitignore file:
.env

Session Expiration

TikTok session IDs expire after a period of time. If you encounter authentication errors, you may need to get a new session ID.
Signs your session has expired:
  • Connection failures with authentication errors
  • Inability to access subscriber-only content
  • Being treated as an anonymous user

Best Practices

  1. Never hardcode session IDs - Use environment variables or secure secret management
  2. Rotate sessions regularly - Get a fresh session ID if you suspect it’s compromised
  3. Monitor for expiration - Implement error handling for expired sessions
  4. Use separate accounts - Don’t use your personal account for bot operations

Alternative: Using WebDefaults

You can also set cookies globally using WebDefaults:
from TikTokLive import TikTokLiveClient
from TikTokLive.client.web.web_settings import WebDefaults

# Set session cookie before creating client
WebDefaults.web_client_cookies = {
    'sessionid': 'your-session-id-here'
}

client: TikTokLiveClient = TikTokLiveClient(
    unique_id="@username"
)

client.run()
Using client.web.set_session() is the recommended approach as it’s more explicit and easier to manage per-client.

Build docs developers (and LLMs) love