tiktok-live-api 1.2.3 โ†’ 1.2.4

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 +334 -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,368 @@
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 } 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 [connected, setConnected] = useState(false);",
20
+ " ",
21
+ " useEffect(() => {",
22
+ " const client = new TikTokLive(username, { apiKey: 'demo_tiktokliveapi_public_2026' });",
23
+ " ",
24
+ ' client.on(\'chat\', (e) => setEvents(prev => [...prev.slice(-20), "๐Ÿ’ฌ " + e.user.uniqueId + ": " + e.comment]));',
25
+ ' client.on(\'gift\', (e) => setEvents(prev => [...prev.slice(-20), "๐ŸŽ " + e.user.uniqueId + " sent " + e.giftName]));',
26
+ " client.on('connected', () => setConnected(true));",
27
+ " client.on('disconnected', () => setConnected(false));",
28
+ " ",
29
+ " client.connect();",
30
+ " return () => client.disconnect();",
31
+ " }, [username]);",
32
+ "",
33
+ " return (",
34
+ " <div style={{ fontFamily: 'sans-serif', maxWidth: 400, margin: '40px auto', border: '1px solid #e5e7eb', padding: '24px', borderRadius: '12px', boxShadow: '0 10px 15px -3px rgba(0, 0, 0, 0.1)' }}>",
35
+ " <h2 style={{ margin: '0 0 16px 0', fontSize: '1.25rem', fontWeight: 600 }}>TikTok Live SDK Test</h2>",
36
+ " <div style={{ marginBottom: '16px', display: 'flex', alignItems: 'center', gap: '8px' }}>",
37
+ " <div style={{ width: 10, height: 10, borderRadius: '50%', background: connected ? '#10b981' : '#ef4444' }}></div>",
38
+ " <span style={{ fontSize: '0.875rem', color: '#374151' }}>{connected ? 'Connected to @' + username : 'Disconnected'}</span>",
39
+ " </div>",
40
+ " <div style={{ height: 320, overflowY: 'auto', background: '#f9fafb', padding: '12px', borderRadius: '8px', border: '1px solid #e5e7eb', fontSize: '0.875rem' }}>",
41
+ " {events.map((e, i) => <div key={i} style={{ padding: '6px 0', borderBottom: '1px solid #f3f4f6' }}>{e}</div>)}",
42
+ " {events.length === 0 && <div style={{ color: '#9ca3af', textAlign: 'center', marginTop: 120 }}>Waiting for events...</div>}",
43
+ " </div>",
44
+ " </div>",
45
+ " );",
46
+ "}"
47
+ ].join('\n') + "\n";
26
48
 
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
- };
49
+ const VUE_TEMPLATE = [
50
+ "<template>",
51
+ ' <div style="font-family: sans-serif; max-width: 400px; margin: 40px auto; border: 1px solid #e5e7eb; padding: 24px; border-radius: 12px; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1)">',
52
+ ' <h2 style="margin: 0 0 16px 0; font-size: 1.25rem; font-weight: 600">TikTok Live SDK Test</h2>',
53
+ ' <div style="margin-bottom: 16px; display: flex; align-items: center; gap: 8px">',
54
+ " <div :style=\"{ width: '10px', height: '10px', borderRadius: '50%', background: connected ? '#10b981' : '#ef4444' }\"></div>",
55
+ " <span style=\"font-size: 0.875rem; color: '#374151'\">{{ connected ? 'Connected to @' + username : 'Disconnected' }}</span>",
56
+ " </div>",
57
+ ' <div style="height: 320px; overflow-y: auto; background: #f9fafb; padding: 12px; border-radius: 8px; border: 1px solid #e5e7eb; font-size: 0.875rem">',
58
+ ' <div v-for="(e, i) in events" :key="i" style="padding: 6px 0; border-bottom: 1px solid #f3f4f6">',
59
+ " {{ e }}",
60
+ " </div>",
61
+ ' <div v-if="events.length === 0" style="color: #9ca3af; text-align: center; margin-top: 120px">Waiting for events...</div>',
62
+ " </div>",
63
+ " </div>",
64
+ "</template>",
65
+ "",
66
+ "<script setup>",
67
+ "import { ref, onMounted, onUnmounted } from 'vue'",
68
+ "import { TikTokLive } from 'tiktok-live-api'",
69
+ "",
70
+ "const username = ref('aljazeeraenglish')",
71
+ "const connected = ref(false)",
72
+ "const events = ref([])",
73
+ "let client = null",
74
+ "",
75
+ "onMounted(() => {",
76
+ " client = new TikTokLive(username.value, { apiKey: 'demo_tiktokliveapi_public_2026' })",
77
+ " ",
78
+ " client.on('chat', (e) => {",
79
+ ' events.value.push("๐Ÿ’ฌ " + e.user.uniqueId + ": " + e.comment)',
80
+ " if (events.value.length > 20) events.value.shift()",
81
+ " })",
82
+ " ",
83
+ " client.on('gift', (e) => {",
84
+ ' events.value.push("๐ŸŽ " + e.user.uniqueId + " sent " + e.giftName)',
85
+ " if (events.value.length > 20) events.value.shift()",
86
+ " })",
87
+ "",
88
+ " client.on('connected', () => connected.value = true)",
89
+ " client.on('disconnected', () => connected.value = false)",
90
+ " ",
91
+ " client.connect()",
92
+ "})",
93
+ "",
94
+ "onUnmounted(() => {",
95
+ " if (client) client.disconnect()",
96
+ "})",
97
+ "</script>"
98
+ ].join('\n') + "\n";
37
99
 
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}`,
100
+ const E = String.fromCharCode(27);
101
+ const R = E + "[0m";
102
+ const B = E + "[1m";
103
+ const D = E + "[2m";
104
+ const C = {
105
+ cyan: E + "[38;5;80m", green: E + "[38;5;114m", yellow: E + "[38;5;222m",
106
+ mag: E + "[38;5;176m", blue: E + "[38;5;111m", red: E + "[38;5;203m",
107
+ gray: E + "[38;5;242m", white: E + "[38;5;252m", pink: E + "[38;5;218m",
46
108
  };
47
109
 
48
- // โ”€โ”€ Parse args โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
49
- let target = '';
50
- let apiKey = DEMO_KEY;
110
+ // โ”€โ”€ Interactive Prompt โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
51
111
 
52
112
  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
- }
113
+ const isInteractive = args.length === 0;
59
114
 
60
- function help() {
61
- console.log(`
62
- ${B}tiktok-live-api${R} ${D}Real-time TikTok Live events in your terminal${R}
115
+ if (!isInteractive) {
116
+ runDemo();
117
+ } else {
118
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
63
119
 
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}
120
+ console.log("\n " + B + "tiktok-live-api" + R + "\n Unofficial TikTok LIVE SDK by TikTool\n");
121
+ console.log(" What would you like to do?");
122
+ console.log(" 1) " + C.cyan + "Watch Live Terminal Demo" + R + " (Default)");
123
+ console.log(" 2) " + C.green + "Scaffold a Nuxt 3 (Vue) App" + R);
124
+ console.log(" 3) " + C.blue + "Scaffold a Next.js (React) App" + R);
125
+ console.log(" 4) " + C.yellow + "Scaffold a Vite (React) App" + R + "\n");
68
126
 
69
- ${D}Unofficial third-party API by TikTool${R}
70
- ${D}https://tik.tools ยท Not affiliated with TikTok or ByteDance${R}
71
- `);
72
- }
127
+ const timeoutId = setTimeout(() => {
128
+ console.log(D + "\r No input received. Auto-starting Terminal Demo..." + R + "\n");
129
+ rl.close();
130
+ runDemo();
131
+ }, 10000);
73
132
 
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); });
133
+ rl.question(" Choose [1-4]: ", (answer) => {
134
+ clearTimeout(timeoutId);
135
+ handleMenu(answer.trim() || '1');
136
+ });
137
+
138
+ function handleMenu(choice) {
139
+ if (choice === '1') {
140
+ rl.close();
141
+ return runDemo();
142
+ }
143
+
144
+ if (!['2', '3', '4'].includes(choice)) {
145
+ console.log(" " + C.red + "Invalid choice." + R);
146
+ rl.close();
147
+ process.exit(1);
148
+ }
149
+
150
+ rl.question("\n Target directory (e.g. ./my-app) [./tiktok-live-project]: ", (dir) => {
151
+ dir = dir.trim() || './tiktok-live-project';
152
+ rl.close();
153
+ runScaffold(choice, dir);
82
154
  });
155
+ }
83
156
  }
84
157
 
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();
158
+ // โ”€โ”€ Scaffolding Logic โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
159
+
160
+ function runScaffold(choice, targetDir) {
161
+ const isNuxt = choice === '2';
162
+ const isNext = choice === '3';
163
+ const isVite = choice === '4';
164
+ const fullPath = path.resolve(process.cwd(), targetDir);
165
+
166
+ console.log("\n " + B + "๐Ÿš€ Scaffolding project into " + targetDir + "..." + R + "\n");
167
+
168
+ try {
169
+ if (isNuxt) {
170
+ execSync('npx --yes nuxi@latest init "' + targetDir + '" --packageManager npm', { stdio: 'inherit' });
171
+ } else if (isNext) {
172
+ execSync('npx --yes create-next-app@latest "' + targetDir + '" --js --eslint --tailwind --app --src-dir --import-alias "@/*" --use-npm', { stdio: 'inherit' });
173
+ } else if (isVite) {
174
+ execSync('npm create vite@latest "' + targetDir + '" -- --template react', { stdio: 'inherit' });
175
+ }
176
+
177
+ console.log("\n " + B + "๐Ÿ“ฆ Installing " + C.cyan + "tiktok-live-api" + B + " SDK..." + R + "\n");
178
+ execSync('npm install tiktok-live-api', { cwd: fullPath, stdio: 'inherit' });
179
+
180
+ if (isNuxt) {
181
+ const compDir = path.join(fullPath, 'components');
182
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
183
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.vue'), VUE_TEMPLATE);
184
+
185
+ const appVue = path.join(fullPath, 'app.vue');
186
+ if (fs.existsSync(appVue)) {
187
+ fs.writeFileSync(appVue, [
188
+ '<template>',
189
+ ' <div style="min-height: 100vh; background: #fdfdfd; padding-top: 40px;">',
190
+ ' <TikTokLive />',
191
+ ' </div>',
192
+ '</template>'
193
+ ].join('\n') + '\n');
194
+ }
195
+ } else if (isNext) {
196
+ const compDir = path.join(fullPath, 'src', 'components');
197
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
198
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
199
+
200
+ const pageJs = path.join(fullPath, 'src', 'app', 'page.js');
201
+ if (fs.existsSync(pageJs)) {
202
+ fs.writeFileSync(pageJs, [
203
+ "import TikTokLive from '@/components/TikTokLive';",
204
+ "",
205
+ "export default function Home() {",
206
+ " return (",
207
+ ' <main className="min-h-screen bg-neutral-50 pt-10">',
208
+ " <TikTokLive />",
209
+ " </main>",
210
+ " );",
211
+ "}"
212
+ ].join('\n') + '\n');
213
+ }
214
+ } else if (isVite) {
215
+ const compDir = path.join(fullPath, 'src', 'components');
216
+ if (!fs.existsSync(compDir)) fs.mkdirSync(compDir, { recursive: true });
217
+ fs.writeFileSync(path.join(compDir, 'TikTokLive.jsx'), REACT_TEMPLATE);
218
+
219
+ const appJsx = path.join(fullPath, 'src', 'App.jsx');
220
+ if (fs.existsSync(appJsx)) {
221
+ fs.writeFileSync(appJsx, [
222
+ "import TikTokLive from './components/TikTokLive';",
223
+ "",
224
+ "export default function App() {",
225
+ " return (",
226
+ " <div style={{ minHeight: '100vh', background: '#fdfdfd', paddingTop: '40px' }}>",
227
+ " <TikTokLive />",
228
+ " </div>",
229
+ " );",
230
+ "}"
231
+ ].join('\n') + '\n');
232
+ }
233
+ }
234
+
235
+ console.log("\n " + C.green + "โœ” Project successfully scaffolded!" + R + "\n");
236
+ console.log(" " + B + "Next steps:" + R);
237
+ console.log(" cd " + targetDir);
238
+ console.log(" npm run dev\n");
239
+
240
+ } catch (err) {
241
+ console.error("\n " + C.red + "Error during scaffolding:" + R, err.message);
242
+ process.exit(1);
243
+ }
91
244
  }
92
245
 
93
- // โ”€โ”€ Format โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
94
- function ts() {
246
+ // โ”€โ”€ Terminal Demo Logic โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
247
+
248
+ async function runDemo() {
249
+ const WS_BASE = 'wss://api.tik.tools';
250
+ const DEMO_KEY = 'demo_tiktokliveapi_public_2026';
251
+ const CHANNELS = [
252
+ 'aljazeeraenglish', 'cgtnofficial', 'france24_en',
253
+ 'weathernewslive', 'gbnews', 'bbcnews',
254
+ 'skynews', 'tv_asahi_news', 'abc7chicago', 'thairath_news',
255
+ ];
256
+
257
+ const TAG = {
258
+ chat: C.cyan + "chat" + R,
259
+ gift: C.yellow + "gift" + R,
260
+ like: C.mag + "like" + R,
261
+ member: C.green + "join" + R,
262
+ follow: C.green + "follow" + R,
263
+ viewer: C.blue + "viewers" + R,
264
+ share: C.white + "share" + R,
265
+ };
266
+
267
+ let target = '';
268
+ let apiKey = DEMO_KEY;
269
+ for (let i = 0; i < args.length; i++) {
270
+ const a = args[i];
271
+ if (a === '--key' || a === '-k') { apiKey = args[++i] || DEMO_KEY; }
272
+ else if (a === '--help' || a === '-h') { help(); process.exit(0); }
273
+ else if (!a.startsWith('-')) { target = a.replace(/^@/, ''); }
274
+ }
275
+
276
+ function help() {
277
+ console.log("\n " + B + "tiktok-live-api" + R + " " + D + "Real-time TikTok Live SDK" + R);
278
+ console.log("\n " + B + "Usage" + R);
279
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + D + "interactive menu (demo or scaffold)" + R);
280
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + C.white + "@username" + R + " " + D + "connect to a specific user" + R);
281
+ console.log(" " + C.cyan + "npx tiktok-live-api" + R + " " + C.gray + "--key KEY" + R + " " + D + "use your own API key" + R);
282
+ console.log("\n " + D + "Unofficial API by TikTool ยท https://tik.tools" + R + "\n");
283
+ }
284
+
285
+ function probe(uid, ms = 8000) {
286
+ return new Promise(resolve => {
287
+ const ws = new WebSocket(WS_BASE + "?uniqueId=" + uid + "&apiKey=" + apiKey);
288
+ const t = setTimeout(() => { ws.close(); resolve(null); }, ms);
289
+ ws.on('message', () => { clearTimeout(t); resolve(ws); });
290
+ ws.on('error', () => { clearTimeout(t); resolve(null); });
291
+ ws.on('close', () => { clearTimeout(t); });
292
+ });
293
+ }
294
+
295
+ function ts() {
95
296
  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
- }
297
+ return C.gray + String(d.getHours()).padStart(2, '0') + ":" + String(d.getMinutes()).padStart(2, '0') + ":" + String(d.getSeconds()).padStart(2, '0') + R;
298
+ }
98
299
 
99
- function fmt(event, data) {
100
- const tag = TAG[event] || `${C.gray}${event.padEnd(7)}${R}`;
300
+ function fmt(event, data) {
301
+ const tag = TAG[event] || (C.gray + event.padEnd(7) + R);
101
302
  const u = data.user?.uniqueId || '';
102
303
  const pad = tag.length < 20 ? ' ' : ' ';
103
304
 
104
305
  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
306
+ case 'chat': return ts() + " " + tag + pad + " " + B + u + R + " " + (data.comment || '');
307
+ case 'gift': return ts() + " " + tag + pad + " " + B + u + R + " " + C.yellow + (data.giftName || 'gift') + R + " x" + (data.repeatCount || 1) + " " + D + "(" + (data.diamondCount || 0) + "๐Ÿ’Ž)" + R;
308
+ case 'like': return ts() + " " + tag + pad + " " + B + u + R + " " + D + "total " + (data.totalLikeCount || 0).toLocaleString() + R;
309
+ case 'member': return ts() + " " + tag + pad + " " + C.green + u + R;
310
+ case 'follow': return ts() + " " + tag + " " + C.green + u + R;
311
+ case 'roomUserSeq':
312
+ case 'viewer': return ts() + " " + TAG.viewer + " " + B + (data.viewerCount || 0).toLocaleString() + R;
313
+ case 'share': return ts() + " " + tag + " " + u;
314
+ default: return null;
125
315
  }
126
- }
316
+ }
317
+
318
+ console.log("\n " + B + "tiktok-live-api" + R + " " + D + "ยท" + R + " " + D + "Real-time TikTok Live events" + R);
319
+ console.log(" " + D + "Unofficial API by TikTool ยท https://tik.tools" + R + "\n");
127
320
 
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
- }
321
+ let ws = null, who = '';
322
+
323
+ if (target) {
324
+ process.stdout.write(" " + D + "connecting to" + R + " " + B + "@" + target + R + " " + D + "..." + R);
325
+ ws = await probe(target);
326
+ if (ws) { who = target; console.log(" " + C.green + "โ—" + R); }
327
+ 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); }
328
+ } else {
329
+ console.log(" " + D + "scanning for live streams..." + R + "\n");
330
+ for (const uid of CHANNELS) {
331
+ process.stdout.write(" " + C.gray + "@" + uid + R);
332
+ ws = await probe(uid);
333
+ if (ws) { who = uid; console.log(" " + C.green + "โ— live" + R); break; }
334
+ else { console.log(" " + D + "โ€”" + R); }
335
+ }
336
+ if (!ws) {
337
+ console.log("\n " + C.red + "No live streams found." + R + " Try: " + C.cyan + "npx tiktok-live-api @username" + R + "\n");
338
+ console.log(" " + D + "Get your own API key at" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
339
+ process.exit(1);
151
340
  }
341
+ }
152
342
 
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
- });
343
+ console.log("\n " + C.green + "โ—" + R + " " + B + "@" + who + R + " " + D + "โ€” streaming events. Ctrl+C to stop." + R);
344
+ console.log(" " + D + "โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€" + R + "\n");
168
345
 
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
- });
346
+ let count = 0;
347
+ ws.on('message', raw => {
348
+ try {
349
+ const m = JSON.parse(raw.toString());
350
+ const line = fmt(m.event || 'unknown', m.data || m);
351
+ if (line) { count++; console.log(" " + line); }
352
+ } catch { }
353
+ });
174
354
 
175
- ws.on('error', e => console.error(` ${C.red}error${R} ${e.message}`));
355
+ ws.on('close', () => {
356
+ console.log("\n " + D + "disconnected โ€” " + B + count + R + D + " events received" + R);
357
+ console.log(" " + D + "Get unlimited access:" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
358
+ process.exit(0);
359
+ });
176
360
 
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
- });
361
+ ws.on('error', e => console.error(" " + C.red + "error" + R + " " + e.message));
362
+ process.on('SIGINT', () => {
363
+ console.log("\n\n " + D + "stopped โ€” " + B + count + R + D + " events received" + R);
364
+ console.log(" " + D + "Get unlimited access:" + R + " " + C.cyan + "https://tik.tools" + R + "\n");
365
+ ws.close();
366
+ process.exit(0);
367
+ });
183
368
  }
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.4",
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",