tiktok-live-api 1.2.3 โ†’ 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +11 -0
  2. package/bin/demo.mjs +433 -151
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -18,6 +18,17 @@
18
18
 
19
19
  > This package is **not affiliated with or endorsed by TikTok**. It connects to the [TikTool Live](https://tik.tools) managed API service โ€” 99.9% uptime, no reverse engineering, no maintenance required. Also available for [Python](https://pypi.org/project/tiktok-live-api/) and [any language via WebSocket](https://tik.tools/docs).
20
20
 
21
+ ## ๐Ÿš€ One-Command Quick Start
22
+
23
+ Instantly connect to a live TikTok stream and print real-time events to your terminal. No signup, no install:
24
+
25
+ ```bash
26
+ npx tiktok-live-api
27
+ ```
28
+ *Or connect to a specific stream:* `npx tiktok-live-api @username`
29
+
30
+ ---
31
+
21
32
  ## Install
22
33
 
23
34
  ```bash
package/bin/demo.mjs CHANGED
@@ -1,185 +1,467 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- /**
4
- * tiktok-live-api CLI
5
- * Instantly connects to a live TikTok stream and prints real-time events.
6
- *
7
- * Usage:
8
- * npx tiktok-live-api auto-find a live stream
9
- * npx tiktok-live-api @username connect to a specific stream
10
- * npx tiktok-live-api --key YOUR_KEY use your own API key
11
- *
12
- * Unofficial third-party API by TikTool ยท https://tik.tools
13
- * Not affiliated with TikTok or ByteDance.
14
- */
15
-
16
3
  import WebSocket from 'ws';
4
+ import readline from 'readline';
5
+ import { execSync } from 'child_process';
6
+ import fs from 'fs';
7
+ import path from 'path';
17
8
 
18
- const WS_BASE = 'wss://api.tik.tools';
19
- const DEMO_KEY = 'demo_tiktokliveapi_public_2026';
9
+ // โ”€โ”€ Boilerplate Components โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
20
10
 
21
- const CHANNELS = [
22
- 'aljazeeraenglish', 'cgtnofficial', 'france24_en',
23
- 'weathernewslive', 'gbnews', 'bbcnews',
24
- 'skynews', 'tv_asahi_news', 'abc7chicago', 'thairath_news',
25
- ];
11
+ const REACT_TEMPLATE = [
12
+ '"use client";',
13
+ "import React, { useEffect, useState, useRef } from 'react';",
14
+ "import { TikTokLive } from 'tiktok-live-api';",
15
+ "",
16
+ "export default function TikTokLiveComponent() {",
17
+ " const [events, setEvents] = useState([]);",
18
+ " const [username, setUsername] = useState('aljazeeraenglish');",
19
+ " const [apiKey, setApiKey] = useState('demo_tiktokliveapi_public_2026');",
20
+ " const [inputUsername, setInputUsername] = useState('aljazeeraenglish');",
21
+ " const [connected, setConnected] = useState(false);",
22
+ " const clientRef = useRef(null);",
23
+ "",
24
+ " useEffect(() => {",
25
+ " if (!username) return;",
26
+ " if (clientRef.current) { clientRef.current.disconnect(); }",
27
+ " setEvents([]);",
28
+ " setConnected(false);",
29
+ " ",
30
+ " const client = new TikTokLive(username, { apiKey });",
31
+ " clientRef.current = client;",
32
+ " ",
33
+ " client.on('chat', (e) => setEvents(prev => [...prev.slice(-40), \"๐Ÿ’ฌ \" + e.user.uniqueId + \": \" + e.comment]));",
34
+ " client.on('gift', (e) => setEvents(prev => [...prev.slice(-40), \"๐ŸŽ \" + e.user.uniqueId + \" sent \" + e.giftName]));",
35
+ " client.on('member', (e) => setEvents(prev => [...prev.slice(-40), \"๐Ÿ‘‹ \" + e.user.uniqueId + \" joined\"]));",
36
+ " client.on('like', (e) => setEvents(prev => [...prev.slice(-40), \"โค๏ธ \" + e.user.uniqueId + \" liked\"]));",
37
+ " client.on('connected', () => setConnected(true));",
38
+ " client.on('disconnected', () => setConnected(false));",
39
+ " ",
40
+ " client.connect().catch(() => setConnected(false));",
41
+ " ",
42
+ " return () => { if (clientRef.current) { clientRef.current.disconnect(); } };",
43
+ " }, [username, apiKey]);",
44
+ "",
45
+ " const handleConnect = (e) => {",
46
+ " e.preventDefault();",
47
+ " setUsername(inputUsername);",
48
+ " };",
49
+ "",
50
+ " return (",
51
+ " <div style={{ fontFamily: 'system-ui, sans-serif', maxWidth: 600, margin: '40px auto', border: '1px solid #e5e7eb', borderRadius: '12px', boxShadow: '0 10px 15px -3px rgba(0, 0, 0, 0.1)', background: '#fff' }}>",
52
+ " <div style={{ padding: '24px', borderBottom: '1px solid #e5e7eb' }}>",
53
+ " <h2 style={{ margin: '0 0 8px 0', fontSize: '1.5rem', fontWeight: 600 }}>TikTok Live SDK</h2>",
54
+ " <p style={{ margin: 0, fontSize: '0.875rem', color: '#6b7280' }}>",
55
+ " Get your own API key and unlock unlimited connections at <a href=\"https://tik.tools\" target=\"_blank\" style={{ color: '#3b82f6', textDecoration: 'none' }}>tik.tools</a>",
56
+ " </p>",
57
+ " </div>",
58
+ " <div style={{ padding: '24px' }}>",
59
+ " <form onSubmit={handleConnect} style={{ display: 'flex', flexDirection: 'column', gap: '16px', marginBottom: '24px' }}>",
60
+ " <div>",
61
+ " <label style={{ display: 'block', fontSize: '0.875rem', fontWeight: 500, marginBottom: '6px' }}>TikTok Username</label>",
62
+ " <input type=\"text\" value={inputUsername} onChange={(e) => setInputUsername(e.target.value)} style={{ width: '100%', padding: '8px 12px', borderRadius: '6px', border: '1px solid #d1d5db', boxSizing: 'border-box' }} placeholder=\"e.g. aljazeeraenglish\" />",
63
+ " </div>",
64
+ " <div>",
65
+ " <label style={{ display: 'block', fontSize: '0.875rem', fontWeight: 500, marginBottom: '6px' }}>API Key</label>",
66
+ " <input type=\"text\" value={apiKey} onChange={(e) => setApiKey(e.target.value)} style={{ width: '100%', padding: '8px 12px', borderRadius: '6px', border: '1px solid #d1d5db', boxSizing: 'border-box', fontFamily: 'monospace' }} />",
67
+ " <p style={{ margin: '4px 0 0 0', fontSize: '0.75rem', color: '#9ca3af' }}>The demo key is rate-limited. Replace with your own key for production.</p>",
68
+ " </div>",
69
+ " <button type=\"submit\" style={{ background: '#000', color: '#fff', padding: '10px 16px', borderRadius: '6px', border: 'none', fontWeight: 500, cursor: 'pointer' }}>Connect Stream</button>",
70
+ " </form>",
71
+ " <div style={{ marginBottom: '16px', display: 'flex', alignItems: 'center', gap: '8px' }}>",
72
+ " <div style={{ width: 10, height: 10, borderRadius: '50%', background: connected ? '#10b981' : '#ef4444', transition: 'background 0.3s' }}></div>",
73
+ " <span style={{ fontSize: '0.875rem', color: '#374151', fontWeight: 500 }}>{connected ? 'Connected to @' + username : 'Disconnected'}</span>",
74
+ " </div>",
75
+ " <div style={{ height: 350, overflowY: 'auto', background: '#f9fafb', padding: '12px', borderRadius: '8px', border: '1px solid #e5e7eb', fontSize: '0.875rem', display: 'flex', flexDirection: 'column' }}>",
76
+ " {events.map((e, i) => <div key={i} style={{ padding: '6px 0', borderBottom: '1px solid #f3f4f6', wordBreak: 'break-word' }}>{e}</div>)}",
77
+ " {events.length === 0 && <div style={{ color: '#9ca3af', textAlign: 'center', margin: 'auto 0' }}>{connected ? 'Waiting for events...' : 'Connect to a valid stream to see events'}</div>}",
78
+ " </div>",
79
+ " </div>",
80
+ " </div>",
81
+ " );",
82
+ "}"
83
+ ].join('\n') + "\n";
26
84
 
27
- // โ”€โ”€ ANSI โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
28
- const R = '\x1b[0m';
29
- const B = '\x1b[1m';
30
- const D = '\x1b[2m';
31
- const I = '\x1b[3m';
32
- const C = {
33
- cyan: '\x1b[38;5;80m', green: '\x1b[38;5;114m', yellow: '\x1b[38;5;222m',
34
- mag: '\x1b[38;5;176m', blue: '\x1b[38;5;111m', red: '\x1b[38;5;203m',
35
- gray: '\x1b[38;5;242m', white: '\x1b[38;5;252m', pink: '\x1b[38;5;218m',
36
- };
85
+ const VUE_TEMPLATE = [
86
+ "<template>",
87
+ ' <div style="font-family: system-ui, sans-serif; max-width: 600px; margin: 40px auto; border: 1px solid #e5e7eb; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); background: #fff">',
88
+ ' <div style="padding: 24px; border-bottom: 1px solid #e5e7eb">',
89
+ ' <h2 style="margin: 0 0 8px 0; font-size: 1.5rem; font-weight: 600">TikTok Live SDK</h2>',
90
+ ' <p style="margin: 0; font-size: 0.875rem; color: #6b7280">',
91
+ ' Get your own API key and unlock unlimited connections at <a href="https://tik.tools" target="_blank" style="color: #3b82f6; text-decoration: none">tik.tools</a>',
92
+ " </p>",
93
+ " </div>",
94
+ ' <div style="padding: 24px">',
95
+ ' <form @submit.prevent="handleConnect" style="display: flex; flex-direction: column; gap: 16px; margin-bottom: 24px">',
96
+ " <div>",
97
+ ' <label style="display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 6px">TikTok Username</label>',
98
+ ' <input type="text" v-model="inputUsername" style="width: 100%; padding: 8px 12px; border-radius: 6px; border: 1px solid #d1d5db; box-sizing: border-box" placeholder="e.g. aljazeeraenglish" />',
99
+ " </div>",
100
+ " <div>",
101
+ ' <label style="display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 6px">API Key</label>',
102
+ ' <input type="text" v-model="apiKey" style="width: 100%; padding: 8px 12px; border-radius: 6px; border: 1px solid #d1d5db; box-sizing: border-box; font-family: monospace" />',
103
+ ' <p style="margin: 4px 0 0 0; font-size: 0.75rem; color: #9ca3af">The demo key is rate-limited. Replace with your own key for production.</p>',
104
+ " </div>",
105
+ ' <button type="submit" style="background: #000; color: #fff; padding: 10px 16px; border-radius: 6px; border: none; font-weight: 500; cursor: pointer">Connect Stream</button>',
106
+ " </form>",
107
+ ' <div style="margin-bottom: 16px; display: flex; align-items: center; gap: 8px">',
108
+ " <div :style=\"{ width: '10px', height: '10px', borderRadius: '50%', background: connected ? '#10b981' : '#ef4444', transition: 'background 0.3s' }\"></div>",
109
+ " <span style=\"font-size: 0.875rem; color: '#374151'; font-weight: 500\">{{ connected ? 'Connected to @' + activeUsername : 'Disconnected' }}</span>",
110
+ " </div>",
111
+ ' <div style="height: 350px; overflow-y: auto; background: #f9fafb; padding: 12px; border-radius: 8px; border: 1px solid #e5e7eb; font-size: 0.875rem; display: flex; flex-direction: column">',
112
+ ' <div v-for="(e, i) in events" :key="i" style="padding: 6px 0; border-bottom: 1px solid #f3f4f6; word-break: break-word">',
113
+ " {{ e }}",
114
+ " </div>",
115
+ ' <div v-if="events.length === 0" style="color: #9ca3af; text-align: center; margin: auto 0">{{ connected ? "Waiting for events..." : "Connect to a valid stream to see events" }}</div>',
116
+ " </div>",
117
+ " </div>",
118
+ " </div>",
119
+ "</template>",
120
+ "",
121
+ "<script setup>",
122
+ "import { ref, onMounted, onUnmounted } from 'vue'",
123
+ "import { TikTokLive } from 'tiktok-live-api'",
124
+ "",
125
+ "const inputUsername = ref('aljazeeraenglish')",
126
+ "const activeUsername = ref('aljazeeraenglish')",
127
+ "const apiKey = ref('demo_tiktokliveapi_public_2026')",
128
+ "const connected = ref(false)",
129
+ "const events = ref([])",
130
+ "let client = null",
131
+ "",
132
+ "const connectClient = () => {",
133
+ " if (client) client.disconnect()",
134
+ " events.value = []",
135
+ " connected.value = false",
136
+ " activeUsername.value = inputUsername.value",
137
+ " ",
138
+ " if (!activeUsername.value) return",
139
+ " ",
140
+ " client = new TikTokLive(activeUsername.value, { apiKey: apiKey.value })",
141
+ " ",
142
+ " client.on('chat', (e) => {",
143
+ ' events.value.push("๐Ÿ’ฌ " + e.user.uniqueId + ": " + e.comment)',
144
+ " if (events.value.length > 40) events.value.shift()",
145
+ " })",
146
+ " ",
147
+ " client.on('gift', (e) => {",
148
+ ' events.value.push("๐ŸŽ " + e.user.uniqueId + " sent " + e.giftName)',
149
+ " if (events.value.length > 40) events.value.shift()",
150
+ " })",
151
+ "",
152
+ " client.on('member', (e) => {",
153
+ ' events.value.push("๐Ÿ‘‹ " + e.user.uniqueId + " joined")',
154
+ " if (events.value.length > 40) events.value.shift()",
155
+ " })",
156
+ " ",
157
+ " client.on('like', (e) => {",
158
+ ' events.value.push("โค๏ธ " + e.user.uniqueId + " liked")',
159
+ " if (events.value.length > 40) events.value.shift()",
160
+ " })",
161
+ "",
162
+ " client.on('connected', () => connected.value = true)",
163
+ " client.on('disconnected', () => connected.value = false)",
164
+ " ",
165
+ " client.connect().catch(() => connected.value = false)",
166
+ "}",
167
+ "",
168
+ "const handleConnect = () => {",
169
+ " connectClient()",
170
+ "}",
171
+ "",
172
+ "onMounted(() => {",
173
+ " connectClient()",
174
+ "})",
175
+ "",
176
+ "onUnmounted(() => {",
177
+ " if (client) client.disconnect()",
178
+ "})",
179
+ "</script>"
180
+ ].join('\n') + "\n";
37
181
 
38
- const TAG = {
39
- chat: `${C.cyan}chat${R}`,
40
- gift: `${C.yellow}gift${R}`,
41
- like: `${C.mag}like${R}`,
42
- member: `${C.green}join${R}`,
43
- follow: `${C.green}follow${R}`,
44
- viewer: `${C.blue}viewers${R}`,
45
- share: `${C.white}share${R}`,
182
+ const E = String.fromCharCode(27);
183
+ const R = E + "[0m";
184
+ const B = E + "[1m";
185
+ const D = E + "[2m";
186
+ const C = {
187
+ cyan: E + "[38;5;80m", green: E + "[38;5;114m", yellow: E + "[38;5;222m",
188
+ mag: E + "[38;5;176m", blue: E + "[38;5;111m", red: E + "[38;5;203m",
189
+ gray: E + "[38;5;242m", white: E + "[38;5;252m", pink: E + "[38;5;218m",
46
190
  };
47
191
 
48
- // โ”€โ”€ Parse args โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
49
- let target = '';
50
- let apiKey = DEMO_KEY;
192
+ // โ”€โ”€ Interactive Prompt โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
51
193
 
52
194
  const args = process.argv.slice(2);
53
- for (let i = 0; i < args.length; i++) {
54
- const a = args[i];
55
- if (a === '--key' || a === '-k') { apiKey = args[++i] || DEMO_KEY; }
56
- else if (a === '--help' || a === '-h') { help(); process.exit(0); }
57
- else if (!a.startsWith('-')) { target = a.replace(/^@/, ''); }
58
- }
195
+ const isInteractive = args.length === 0;
59
196
 
60
- function help() {
61
- console.log(`
62
- ${B}tiktok-live-api${R} ${D}Real-time TikTok Live events in your terminal${R}
197
+ if (!isInteractive) {
198
+ runDemo();
199
+ } else {
200
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
63
201
 
64
- ${B}Usage${R}
65
- ${C.cyan}npx tiktok-live-api${R} ${D}auto-find a live stream${R}
66
- ${C.cyan}npx tiktok-live-api${R} ${C.white}@username${R} ${D}connect to a specific user${R}
67
- ${C.cyan}npx tiktok-live-api${R} ${C.gray}--key KEY${R} ${D}use your own API key${R}
202
+ console.log("\n " + B + "tiktok-live-api" + R + "\n Unofficial TikTok LIVE SDK by TikTool\n");
203
+ console.log(" What would you like to do?");
204
+ console.log(" 1) " + C.cyan + "Watch Live Terminal Demo" + R);
205
+ console.log(" 2) " + C.green + "Scaffold a Nuxt 3 (Vue) App" + R);
206
+ console.log(" 3) " + C.blue + "Scaffold a Next.js (React) App" + R);
207
+ console.log(" 4) " + C.yellow + "Scaffold a Vite (React) App" + R + "\n");
68
208
 
69
- ${D}Unofficial third-party API by TikTool${R}
70
- ${D}https://tik.tools ยท Not affiliated with TikTok or ByteDance${R}
71
- `);
72
- }
209
+ function promptAction() {
210
+ rl.question(" Choose [1-4]: ", (answer) => {
211
+ handleMenu(answer.trim());
212
+ });
213
+ }
73
214
 
74
- // โ”€โ”€ WebSocket probe โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
75
- function probe(uid, ms = 8000) {
76
- return new Promise(resolve => {
77
- const ws = new WebSocket(`${WS_BASE}?uniqueId=${uid}&apiKey=${apiKey}`);
78
- const t = setTimeout(() => { ws.close(); resolve(null); }, ms);
79
- ws.on('message', () => { clearTimeout(t); resolve(ws); });
80
- ws.on('error', () => { clearTimeout(t); resolve(null); });
81
- ws.on('close', () => { clearTimeout(t); });
215
+ function handleMenu(choice) {
216
+ if (choice === '1') {
217
+ rl.close();
218
+ return runDemo();
219
+ }
220
+
221
+ if (!['2', '3', '4'].includes(choice)) {
222
+ console.log(" " + C.red + "Invalid choice." + R);
223
+ return promptAction();
224
+ }
225
+
226
+ rl.question("\n Target directory (e.g. ./my-app) [./tiktok-live-project]: ", (dir) => {
227
+ dir = dir.trim() || './tiktok-live-project';
228
+
229
+ console.log("\n Select Package Manager:");
230
+ console.log(" 1) npm " + D + "(Default)" + R);
231
+ console.log(" 2) yarn");
232
+ console.log(" 3) pnpm");
233
+ console.log(" 4) bun");
234
+
235
+ rl.question("\n Choose [1-4]: ", (pmAnswer) => {
236
+ const pmChoice = pmAnswer.trim() || '1';
237
+ const pmMap = { '1': 'npm', '2': 'yarn', '3': 'pnpm', '4': 'bun' };
238
+ const pm = pmMap[pmChoice] || 'npm';
239
+
240
+ rl.close();
241
+ runScaffold(choice, dir, pm);
242
+ });
82
243
  });
244
+ }
245
+
246
+ promptAction();
83
247
  }
84
248
 
85
- // โ”€โ”€ Banner โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
86
- function banner() {
87
- console.log();
88
- console.log(` ${B}tiktok-live-api${R} ${D}ยท${R} ${D}Real-time TikTok Live events${R}`);
89
- console.log(` ${D}Unofficial API by TikTool ยท https://tik.tools${R}`);
90
- console.log();
249
+ // โ”€โ”€ Scaffolding Logic โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
250
+
251
+ function runScaffold(choice, targetDir, pm) {
252
+ const isNuxt = choice === '2';
253
+ const isNext = choice === '3';
254
+ const isVite = choice === '4';
255
+ const fullPath = path.resolve(process.cwd(), targetDir);
256
+
257
+ const npxCmd = pm === 'bun' ? 'bunx' : (pm === 'pnpm' ? 'pnpm dlx' : 'npx');
258
+ const installCmd = pm === 'npm' ? 'npm install tiktok-live-api' : pm === 'yarn' ? 'yarn add tiktok-live-api' : pm === 'pnpm' ? 'pnpm add tiktok-live-api' : 'bun add tiktok-live-api';
259
+ const devCmd = pm === 'npm' ? 'npm run dev' : pm === 'yarn' ? 'yarn dev' : pm === 'pnpm' ? 'pnpm dev' : 'bun run dev';
260
+
261
+ console.log("\n " + B + "๐Ÿš€ Scaffolding project into " + targetDir + " using " + pm + "..." + R + "\n");
262
+
263
+ try {
264
+ if (isNuxt) {
265
+ execSync(npxCmd + ' nuxi@latest init "' + targetDir + '" --packageManager ' + pm, { stdio: 'inherit' });
266
+ } else if (isNext) {
267
+ execSync(npxCmd + ' create-next-app@latest "' + targetDir + '" --js --eslint --tailwind --app --src-dir --import-alias "@/*" --use-' + pm, { stdio: 'inherit' });
268
+ } else if (isVite) {
269
+ const createCmd = pm === 'npm' ? 'npm create vite@latest' : pm === 'yarn' ? 'yarn create vite' : pm === 'pnpm' ? 'pnpm create vite' : 'bun create vite';
270
+ execSync(createCmd + ' "' + targetDir + '" -- --template react', { stdio: 'inherit' });
271
+ }
272
+
273
+ console.log("\n " + B + "๐Ÿ“ฆ Installing " + C.cyan + "tiktok-live-api" + B + " SDK..." + R + "\n");
274
+ execSync(installCmd, { cwd: fullPath, stdio: 'inherit' });
275
+
276
+ if (isNuxt) {
277
+ const compDir = path.join(fullPath, 'components');
278
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
279
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.vue'), VUE_TEMPLATE);
280
+
281
+ const appVue = path.join(fullPath, 'app.vue');
282
+ if (fs.existsSync(appVue)) {
283
+ fs.writeFileSync(appVue, [
284
+ '<template>',
285
+ ' <main style="min-height: 100vh; background: #f9fafb; padding-top: 40px;">',
286
+ ' <TikTokLive />',
287
+ ' </main>',
288
+ '</template>'
289
+ ].join('\n') + '\n');
290
+ }
291
+ } else if (isNext) {
292
+ const compDir = path.join(fullPath, 'src', 'components');
293
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
294
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
295
+
296
+ const pageJs = path.join(fullPath, 'src', 'app', 'page.js');
297
+ if (fs.existsSync(pageJs)) {
298
+ fs.writeFileSync(pageJs, [
299
+ "import TikTokLive from '@/components/TikTokLive';",
300
+ "",
301
+ "export default function Home() {",
302
+ " return (",
303
+ ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
304
+ " <TikTokLive />",
305
+ " </main>",
306
+ " );",
307
+ "}"
308
+ ].join('\n') + '\n');
309
+ }
310
+ } else if (isVite) {
311
+ const compDir = path.join(fullPath, 'src', 'components');
312
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
313
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
314
+
315
+ const appJsx = path.join(fullPath, 'src', 'App.jsx');
316
+ if (fs.existsSync(appJsx)) {
317
+ fs.writeFileSync(appJsx, [
318
+ "import TikTokLive from './components/TikTokLive';",
319
+ "",
320
+ "export default function App() {",
321
+ " return (",
322
+ ' <main style={{ minHeight: "100vh", background: "#f9fafb", paddingTop: "40px" }}>',
323
+ " <TikTokLive />",
324
+ " </main>",
325
+ " );",
326
+ "}"
327
+ ].join('\n') + '\n');
328
+ }
329
+ }
330
+
331
+ console.log("\n " + C.green + "โœ” Project successfully scaffolded!" + R + "\n");
332
+ console.log(" " + B + "Next steps:" + R);
333
+ console.log(" cd " + targetDir);
334
+ if (isVite && pm !== 'bun') {
335
+ console.log(" " + pm + " install");
336
+ }
337
+ console.log(" " + devCmd + "\n");
338
+
339
+ } catch (err) {
340
+ console.error("\n " + C.red + "Error during scaffolding:" + R, err.message);
341
+ process.exit(1);
342
+ }
91
343
  }
92
344
 
93
- // โ”€โ”€ Format โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
94
- function ts() {
345
+ // โ”€โ”€ Terminal Demo Logic โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
346
+
347
+ async function runDemo() {
348
+ const WS_BASE = 'wss://api.tik.tools';
349
+ const DEMO_KEY = 'demo_tiktokliveapi_public_2026';
350
+ const CHANNELS = [
351
+ 'aljazeeraenglish', 'cgtnofficial', 'france24_en',
352
+ 'weathernewslive', 'gbnews', 'bbcnews',
353
+ 'skynews', 'tv_asahi_news', 'abc7chicago', 'thairath_news',
354
+ ];
355
+
356
+ const TAG = {
357
+ chat: C.cyan + "chat" + R,
358
+ gift: C.yellow + "gift" + R,
359
+ like: C.mag + "like" + R,
360
+ member: C.green + "join" + R,
361
+ follow: C.green + "follow" + R,
362
+ viewer: C.blue + "viewers" + R,
363
+ share: C.white + "share" + R,
364
+ };
365
+
366
+ let target = '';
367
+ let apiKey = DEMO_KEY;
368
+ for (let i = 0; i < args.length; i++) {
369
+ const a = args[i];
370
+ if (a === '--key' || a === '-k') { apiKey = args[++i] || DEMO_KEY; }
371
+ else if (a === '--help' || a === '-h') { help(); process.exit(0); }
372
+ else if (!a.startsWith('-')) { target = a.replace(/^@/, ''); }
373
+ }
374
+
375
+ function help() {
376
+ console.log("\n " + B + "tiktok-live-api" + R + " " + D + "Real-time TikTok Live SDK" + R);
377
+ console.log("\n " + B + "Usage" + R);
378
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + D + "interactive menu (demo or scaffold)" + R);
379
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + C.white + "@username" + R + " " + D + "connect to a specific user" + R);
380
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + C.gray + "--key KEY" + R + " " + D + "use your own API key" + R);
381
+ console.log("\n " + D + "Unofficial API by TikTool ยท https://tik.tools" + R + "\n");
382
+ }
383
+
384
+ function probe(uid, ms = 8000) {
385
+ return new Promise(resolve => {
386
+ const ws = new WebSocket(WS_BASE + "?uniqueId=" + uid + "&apiKey=" + apiKey);
387
+ const t = setTimeout(() => { ws.close(); resolve(null); }, ms);
388
+ ws.on('message', () => { clearTimeout(t); resolve(ws); });
389
+ ws.on('error', () => { clearTimeout(t); resolve(null); });
390
+ ws.on('close', () => { clearTimeout(t); });
391
+ });
392
+ }
393
+
394
+ function ts() {
95
395
  const d = new Date();
96
- return `${C.gray}${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}${R}`;
97
- }
396
+ return C.gray + String(d.getHours()).padStart(2, '0') + ":" + String(d.getMinutes()).padStart(2, '0') + ":" + String(d.getSeconds()).padStart(2, '0') + R;
397
+ }
98
398
 
99
- function fmt(event, data) {
100
- const tag = TAG[event] || `${C.gray}${event.padEnd(7)}${R}`;
399
+ function fmt(event, data) {
400
+ const tag = TAG[event] || (C.gray + event.padEnd(7) + R);
101
401
  const u = data.user?.uniqueId || '';
102
402
  const pad = tag.length < 20 ? ' ' : ' ';
103
403
 
104
404
  switch (event) {
105
- case 'chat':
106
- return `${ts()} ${tag}${pad} ${B}${u}${R} ${data.comment || ''}`;
107
- case 'gift': {
108
- const name = data.giftName || 'gift';
109
- const n = data.repeatCount || 1;
110
- const d2 = data.diamondCount || 0;
111
- return `${ts()} ${tag}${pad} ${B}${u}${R} ${C.yellow}${name}${R} x${n} ${D}(${d2}๐Ÿ’Ž)${R}`;
112
- }
113
- case 'like':
114
- return `${ts()} ${tag}${pad} ${B}${u}${R} ${D}total ${(data.totalLikeCount || 0).toLocaleString()}${R}`;
115
- case 'member':
116
- return `${ts()} ${tag}${pad} ${C.green}${u}${R}`;
117
- case 'follow':
118
- return `${ts()} ${tag} ${C.green}${u}${R}`;
119
- case 'viewer':
120
- return `${ts()} ${tag} ${B}${(data.viewerCount || 0).toLocaleString()}${R}`;
121
- case 'share':
122
- return `${ts()} ${tag} ${u}`;
123
- default:
124
- return null; // skip noisy unknown events
405
+ case 'chat': return ts() + " " + tag + pad + " " + B + u + R + " " + (data.comment || '');
406
+ case 'gift': return ts() + " " + tag + pad + " " + B + u + R + " " + C.yellow + (data.giftName || 'gift') + R + " x" + (data.repeatCount || 1) + " " + D + "(" + (data.diamondCount || 0) + "๐Ÿ’Ž)" + R;
407
+ case 'like': return ts() + " " + tag + pad + " " + B + u + R + " " + D + "total " + (data.totalLikeCount || 0).toLocaleString() + R;
408
+ case 'member': return ts() + " " + tag + pad + " " + C.green + u + R;
409
+ case 'follow': return ts() + " " + tag + " " + C.green + u + R;
410
+ case 'roomUserSeq':
411
+ case 'viewer': return ts() + " " + TAG.viewer + " " + B + (data.viewerCount || 0).toLocaleString() + R;
412
+ case 'share': return ts() + " " + tag + " " + u;
413
+ default: return null;
125
414
  }
126
- }
415
+ }
127
416
 
128
- // โ”€โ”€ Main โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
129
- async function main() {
130
- banner();
131
- let ws = null, who = '';
132
-
133
- if (target) {
134
- process.stdout.write(` ${D}connecting to${R} ${B}@${target}${R} ${D}...${R}`);
135
- ws = await probe(target);
136
- if (ws) { who = target; console.log(` ${C.green}โ—${R}`); }
137
- else { console.log(` ${C.red}โœ—${R}\n\n ${C.red}Stream not found.${R} Make sure ${B}@${target}${R} is live.\n`); process.exit(1); }
138
- } else {
139
- console.log(` ${D}scanning for live streams...${R}\n`);
140
- for (const uid of CHANNELS) {
141
- process.stdout.write(` ${C.gray}@${uid}${R}`);
142
- ws = await probe(uid);
143
- if (ws) { who = uid; console.log(` ${C.green}โ— live${R}`); break; }
144
- else { console.log(` ${D}โ€”${R}`); }
145
- }
146
- if (!ws) {
147
- console.log(`\n ${C.red}No live streams found.${R} Try: ${C.cyan}npx tiktok-live-api @username${R}\n`);
148
- console.log(` ${D}Get your own API key at${R} ${C.cyan}https://tik.tools${R}\n`);
149
- process.exit(1);
150
- }
417
+ console.log("\n " + B + "tiktok-live-api" + R + " " + D + "ยท" + R + " " + D + "Real-time TikTok Live events" + R);
418
+ console.log(" " + D + "Unofficial API by TikTool ยท https://tik.tools" + R + "\n");
419
+
420
+ let ws = null, who = '';
421
+
422
+ if (target) {
423
+ process.stdout.write(" " + D + "connecting to" + R + " " + B + "@" + target + R + " " + D + "..." + R);
424
+ ws = await probe(target);
425
+ if (ws) { who = target; console.log(" " + C.green + "โ—" + R); }
426
+ else { console.log(" " + C.red + "โœ—" + R + "\n\n " + C.red + "Stream not found." + R + " Make sure " + B + "@" + target + R + " is live.\n"); process.exit(1); }
427
+ } else {
428
+ console.log(" " + D + "scanning for live streams..." + R + "\n");
429
+ for (const uid of CHANNELS) {
430
+ process.stdout.write(" " + C.gray + "@" + uid + R);
431
+ ws = await probe(uid);
432
+ if (ws) { who = uid; console.log(" " + C.green + "โ— live" + R); break; }
433
+ else { console.log(" " + D + "โ€”" + R); }
151
434
  }
435
+ if (!ws) {
436
+ console.log("\n " + C.red + "No live streams found." + R + " Try: " + C.cyan + "npx tiktok-live-api @username" + R + "\n");
437
+ console.log(" " + D + "Get your own API key at" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
438
+ process.exit(1);
439
+ }
440
+ }
152
441
 
153
- console.log();
154
- console.log(` ${C.green}โ—${R} ${B}@${who}${R} ${D}โ€” streaming events. Ctrl+C to stop.${R}`);
155
- console.log(` ${D}${'โ”€'.repeat(50)}${R}`);
156
- console.log();
157
-
158
- let count = 0;
159
- ws.on('message', raw => {
160
- try {
161
- const m = JSON.parse(raw.toString());
162
- const ev = m.event || 'unknown';
163
- const d = m.data || m;
164
- const line = fmt(ev, d);
165
- if (line) { count++; console.log(` ${line}`); }
166
- } catch { }
167
- });
442
+ console.log("\n " + C.green + "โ—" + R + " " + B + "@" + who + R + " " + D + "โ€” streaming events. Ctrl+C to stop." + R);
443
+ console.log(" " + D + "โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€" + R + "\n");
168
444
 
169
- ws.on('close', () => {
170
- console.log(`\n ${D}disconnected โ€” ${B}${count}${R}${D} events received${R}`);
171
- console.log(` ${D}Get unlimited access:${R} ${C.cyan}https://tik.tools${R}\n`);
172
- process.exit(0);
173
- });
445
+ let count = 0;
446
+ ws.on('message', raw => {
447
+ try {
448
+ const m = JSON.parse(raw.toString());
449
+ const line = fmt(m.event || 'unknown', m.data || m);
450
+ if (line) { count++; console.log(" " + line); }
451
+ } catch { }
452
+ });
174
453
 
175
- ws.on('error', e => console.error(` ${C.red}error${R} ${e.message}`));
454
+ ws.on('close', () => {
455
+ console.log("\n " + D + "disconnected โ€” " + B + count + R + D + " events received" + R);
456
+ console.log(" " + D + "Get unlimited access:" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
457
+ process.exit(0);
458
+ });
176
459
 
177
- process.on('SIGINT', () => {
178
- console.log(`\n\n ${D}stopped โ€” ${B}${count}${R}${D} events received${R}`);
179
- console.log(` ${D}Get your own key:${R} ${C.cyan}https://tik.tools${R}\n`);
180
- ws.close();
181
- process.exit(0);
182
- });
460
+ ws.on('error', e => console.error(" " + C.red + "error" + R + " " + e.message));
461
+ process.on('SIGINT', () => {
462
+ console.log("\n\n " + D + "stopped โ€” " + B + count + R + D + " events received" + R);
463
+ console.log(" " + D + "Get unlimited access:" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
464
+ ws.close();
465
+ process.exit(0);
466
+ });
183
467
  }
184
-
185
- main().catch(e => { console.error(` ${C.red}${e.message}${R}`); process.exit(1); });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tiktok-live-api",
3
- "version": "1.2.3",
3
+ "version": "1.2.5",
4
4
  "description": "Unofficial TikTok LIVE API Client โ€” Real-time chat, gifts, viewers, battles, and AI live captions from any TikTok livestream. Managed WebSocket API with 99.9% uptime.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",