Why Use TikTool Live for Python?
The popular TikTokLive Python library connects directly to TikTok's broadcast protocol. While it works, its maintainers explicitly warn it is not production-ready - it breaks frequently when TikTok updates their infrastructure.
TikTool Live is a managed API service that handles protocol changes, provides 99.9% uptime, and works with any language via standard WebSocket. No protocol handling required - just connect and receive events.
Prerequisites
- Python 3.8+ installed
- A free TikTool Live API key - get one here
Option 1: asyncio + websockets
The simplest approach - use the standard websockets library to connect to TikTool Live's WebSocket API:
pip install websocketsimport asyncio
import websockets
import json
import os
API_KEY = os.environ.get("TIKTOOL_API_KEY", "YOUR_API_KEY")
STREAMER = "tv_asahi_news" # TikTok username (without @)
async def connect():
uri = f"wss://api.tik.tools?uniqueId={STREAMER}&apiKey={API_KEY}"
async with websockets.connect(uri) as ws:
print(f"✅ Connected to @{STREAMER}")
async for message in ws:
event = json.loads(message)
event_type = event.get("event", "")
data = event.get("data", {})
if event_type == "chat":
user = data.get("user", {}).get("uniqueId", "?")
comment = data.get("comment", "")
print(f"[CHAT] {user}: {comment}")
elif event_type == "gift":
user = data.get("user", {}).get("uniqueId", "?")
gift = data.get("giftName", "?")
diamonds = data.get("diamondCount", 0)
print(f"[GIFT] {user} sent {gift} ({diamonds} 💎)")
elif event_type == "member":
user = data.get("user", {}).get("uniqueId", "?")
print(f"[JOIN] {user} joined")
elif event_type == "roomUserSeq":
viewers = data.get("viewerCount", 0)
print(f"[VIEWERS] {viewers} watching")
asyncio.run(connect())Option 2: REST API with requests
For one-off data fetching (room info, user profiles), use the REST API:
pip install requestsimport requests
API_KEY = "YOUR_API_KEY"
BASE = "https://api.tik.tools"
# Get room info for a streamer
resp = requests.get(f"{BASE}/webcast/room_info/", params={
"uniqueId": "tv_asahi_news",
"apiKey": API_KEY,
})
data = resp.json()
print(f"Title: {data.get('title', 'N/A')}")
print(f"Viewers: {data.get('viewer_count', 0)}")
print(f"Status: {'LIVE' if data.get('alive') else 'OFFLINE'}")Option 3: Live Captions (Speech-to-Text)
Transcribe and translate any TikTok LIVE stream's audio in real-time. This feature is unique to TikTool Live - no other service offers it.
import asyncio
import websockets
import json
import os
API_KEY = os.environ.get("TIKTOOL_API_KEY", "YOUR_API_KEY")
STREAMER = "tv_asahi_news"
async def captions():
uri = (
f"wss://api.tik.tools/captions"
f"?uniqueId={STREAMER}"
f"&apiKey={API_KEY}"
f"&translate=en"
f"&diarization=true"
)
async with websockets.connect(uri) as ws:
print(f"🎤 Captions started for @{STREAMER}")
async for message in ws:
event = json.loads(message)
msg_type = event.get("type", "")
if msg_type == "caption":
speaker = event.get("speaker", "")
text = event.get("text", "")
is_final = event.get("isFinal", False)
prefix = f"[{speaker}] " if speaker else ""
suffix = " ✓" if is_final else "..."
print(f"{prefix}{text}{suffix}")
elif msg_type == "translation":
print(f" → {event.get('text', '')}")
elif msg_type == "credits":
remaining = event.get("remaining", 0)
total = event.get("total", 0)
print(f"[CREDITS] {remaining}/{total} min remaining")
asyncio.run(captions())Running the Scripts
# Set your API key as environment variable
export TIKTOOL_API_KEY=your_api_key_here
# Run the WebSocket example
python tiktok_live.py
# Run the captions example
python tiktok_captions.pyExpected Output
✅ Connected to @tv_asahi_news
[VIEWERS] 1,234 watching
[CHAT] viewer42: Hello everyone!
[GIFT] big_fan sent Rose (1 💎)
[CHAT] news_watcher: Great coverage
[JOIN] new_viewer joined
[VIEWERS] 1,237 watchingNext Steps
- Store events in a database - pipe events to PostgreSQL, MongoDB, or SQLite
- Build a dashboard - use Flask or FastAPI to create a real-time web dashboard
- AI analysis - feed chat messages to GPT/Claude for sentiment analysis
- Multi-stream - monitor multiple streams simultaneously (Pro tier: 50 connections)
- Live subtitles - use the Captions API for accessibility or translation