tiktok-live-api 1.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 TikTool
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,290 @@
1
+ # tiktok-live-api
2
+
3
+ **Unofficial TikTok LIVE API Client for Node.js & TypeScript** — Connect to any TikTok LIVE stream and receive real-time chat messages, gifts, likes, follows, viewer counts, battles, and more. Includes AI-powered live captions (speech-to-text). Powered by the [TikTool](https://tik.tools) managed API.
4
+
5
+ [![npm](https://img.shields.io/npm/v/tiktok-live-api)](https://www.npmjs.com/package/tiktok-live-api)
6
+ [![npm downloads](https://img.shields.io/npm/dm/tiktok-live-api)](https://www.npmjs.com/package/tiktok-live-api)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue)](https://www.typescriptlang.org/)
8
+ [![License](https://img.shields.io/npm/l/tiktok-live-api)](https://github.com/tiktool/tiktok-live-api/blob/main/LICENSE)
9
+
10
+ > 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).
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install tiktok-live-api
16
+ ```
17
+
18
+ ```bash
19
+ # or with yarn / pnpm / bun
20
+ yarn add tiktok-live-api
21
+ pnpm add tiktok-live-api
22
+ bun add tiktok-live-api
23
+ ```
24
+
25
+ ## Quick Start
26
+
27
+ ```typescript
28
+ import { TikTokLive } from 'tiktok-live-api';
29
+
30
+ const client = new TikTokLive('streamer_username', { apiKey: 'YOUR_API_KEY' });
31
+
32
+ client.on('chat', (event) => {
33
+ console.log(`${event.user.uniqueId}: ${event.comment}`);
34
+ });
35
+
36
+ client.on('gift', (event) => {
37
+ console.log(`${event.user.uniqueId} sent ${event.giftName} (${event.diamondCount} 💎)`);
38
+ });
39
+
40
+ client.on('like', (event) => {
41
+ console.log(`${event.user.uniqueId} liked (total: ${event.totalLikes})`);
42
+ });
43
+
44
+ client.on('follow', (event) => {
45
+ console.log(`${event.user.uniqueId} followed!`);
46
+ });
47
+
48
+ client.on('roomUserSeq', (event) => {
49
+ console.log(`${event.viewerCount} viewers watching`);
50
+ });
51
+
52
+ client.connect();
53
+ ```
54
+
55
+ That's it. **No complex setup, no protobuf, no reverse engineering, no breakages when TikTok updates.**
56
+
57
+ ## JavaScript (CommonJS)
58
+
59
+ ```javascript
60
+ const { TikTokLive } = require('tiktok-live-api');
61
+
62
+ const client = new TikTokLive('streamer_username', { apiKey: 'YOUR_API_KEY' });
63
+ client.on('chat', (e) => console.log(`${e.user.uniqueId}: ${e.comment}`));
64
+ client.connect();
65
+ ```
66
+
67
+ ## Get a Free API Key
68
+
69
+ 1. Go to [tik.tools](https://tik.tools)
70
+ 2. Sign up (no credit card required)
71
+ 3. Copy your API key
72
+
73
+ The free Sandbox tier gives you 50 requests/day and 1 WebSocket connection.
74
+
75
+ ## Environment Variable
76
+
77
+ Instead of passing `apiKey` directly, you can set it as an environment variable:
78
+
79
+ ```bash
80
+ # Linux / macOS
81
+ export TIKTOOL_API_KEY=your_api_key_here
82
+
83
+ # Windows (CMD)
84
+ set TIKTOOL_API_KEY=your_api_key_here
85
+
86
+ # Windows (PowerShell)
87
+ $env:TIKTOOL_API_KEY="your_api_key_here"
88
+ ```
89
+
90
+ ```typescript
91
+ import { TikTokLive } from 'tiktok-live-api';
92
+
93
+ // Automatically reads TIKTOOL_API_KEY from environment
94
+ const client = new TikTokLive('streamer_username');
95
+ client.on('chat', (e) => console.log(e.comment));
96
+ client.connect();
97
+ ```
98
+
99
+ ## Events
100
+
101
+ | Event | Description | Key Fields |
102
+ |-------|-------------|------------|
103
+ | `chat` | Chat message | `user`, `comment`, `emotes` |
104
+ | `gift` | Virtual gift | `user`, `giftName`, `diamondCount`, `repeatCount` |
105
+ | `like` | Like event | `user`, `likeCount`, `totalLikes` |
106
+ | `follow` | New follower | `user` |
107
+ | `share` | Stream share | `user` |
108
+ | `member` | Viewer joined | `user` |
109
+ | `subscribe` | New subscriber | `user` |
110
+ | `roomUserSeq` | Viewer count | `viewerCount`, `topViewers` |
111
+ | `battle` | Battle event | `type`, `teams`, `scores` |
112
+ | `envelope` | Treasure chest | `diamonds`, `user` |
113
+ | `streamEnd` | Stream ended | `reason` |
114
+ | `connected` | Connected | `uniqueId` |
115
+ | `disconnected` | Disconnected | `uniqueId` |
116
+ | `error` | Error occurred | `error` |
117
+ | `event` | Catch-all | Full raw event |
118
+
119
+ All events are fully typed with TypeScript interfaces. Your IDE will show autocompletion for every field.
120
+
121
+ ## Live Captions (Speech-to-Text)
122
+
123
+ Transcribe and translate any TikTok LIVE stream in real-time. **This feature is unique to TikTool Live — no other TikTok library offers it.**
124
+
125
+ ```typescript
126
+ import { TikTokCaptions } from 'tiktok-live-api';
127
+
128
+ const captions = new TikTokCaptions('streamer_username', {
129
+ apiKey: 'YOUR_API_KEY',
130
+ translate: 'en', // translate to English
131
+ diarization: true, // identify who is speaking
132
+ });
133
+
134
+ captions.on('caption', (event) => {
135
+ const speaker = event.speaker ? `[${event.speaker}] ` : '';
136
+ console.log(`${speaker}${event.text}${event.isFinal ? ' ✓' : '...'}`);
137
+ });
138
+
139
+ captions.on('translation', (event) => {
140
+ console.log(` → ${event.text}`);
141
+ });
142
+
143
+ captions.on('credits', (event) => {
144
+ console.log(`${event.remaining}/${event.total} minutes remaining`);
145
+ });
146
+
147
+ captions.connect();
148
+ ```
149
+
150
+ ### Caption Events
151
+
152
+ | Event | Description | Key Fields |
153
+ |-------|-------------|------------|
154
+ | `caption` | Real-time caption text | `text`, `speaker`, `isFinal`, `language` |
155
+ | `translation` | Translated caption | `text`, `sourceLanguage`, `targetLanguage` |
156
+ | `credits` | Credit balance update | `total`, `used`, `remaining` |
157
+ | `credits_low` | Low credit warning | `remaining`, `percentage` |
158
+ | `status` | Session status | `status`, `message` |
159
+
160
+ ## Chat Bot Example
161
+
162
+ ```typescript
163
+ import { TikTokLive } from 'tiktok-live-api';
164
+
165
+ const client = new TikTokLive('streamer_username', { apiKey: 'YOUR_API_KEY' });
166
+ const giftLeaderboard = new Map<string, number>();
167
+ let messageCount = 0;
168
+
169
+ client.on('chat', (event) => {
170
+ messageCount++;
171
+ const msg = event.comment.toLowerCase().trim();
172
+ const user = event.user.uniqueId;
173
+
174
+ if (msg === '!hello') {
175
+ console.log(`>> BOT: Welcome ${user}! 👋`);
176
+ } else if (msg === '!stats') {
177
+ console.log(`>> BOT: ${messageCount} messages, ${giftLeaderboard.size} gifters`);
178
+ } else if (msg === '!top') {
179
+ const top = [...giftLeaderboard.entries()]
180
+ .sort((a, b) => b[1] - a[1])
181
+ .slice(0, 5);
182
+ top.forEach(([name, diamonds], i) => {
183
+ console.log(` ${i + 1}. ${name} — ${diamonds} 💎`);
184
+ });
185
+ }
186
+ });
187
+
188
+ client.on('gift', (event) => {
189
+ const user = event.user.uniqueId;
190
+ const diamonds = event.diamondCount || 0;
191
+ giftLeaderboard.set(user, (giftLeaderboard.get(user) || 0) + diamonds);
192
+ });
193
+
194
+ client.connect();
195
+ ```
196
+
197
+ ## TypeScript
198
+
199
+ This package ships with full TypeScript support. All events are typed:
200
+
201
+ ```typescript
202
+ import { TikTokLive, ChatEvent, GiftEvent } from 'tiktok-live-api';
203
+
204
+ const client = new TikTokLive('streamer', { apiKey: 'KEY' });
205
+
206
+ // Full autocompletion — your IDE knows the type of `event`
207
+ client.on('chat', (event: ChatEvent) => {
208
+ console.log(event.user.uniqueId); // ✓ typed
209
+ console.log(event.comment); // ✓ typed
210
+ });
211
+
212
+ client.on('gift', (event: GiftEvent) => {
213
+ console.log(event.giftName); // ✓ typed
214
+ console.log(event.diamondCount); // ✓ typed
215
+ });
216
+ ```
217
+
218
+ ## API Reference
219
+
220
+ ### `new TikTokLive(uniqueId, options?)`
221
+
222
+ | Option | Type | Default | Description |
223
+ |--------|------|---------|-------------|
224
+ | `apiKey` | `string` | `process.env.TIKTOOL_API_KEY` | Your TikTool API key |
225
+ | `autoReconnect` | `boolean` | `true` | Auto-reconnect on disconnect |
226
+ | `maxReconnectAttempts` | `number` | `5` | Max reconnection attempts |
227
+
228
+ **Methods:**
229
+ - `client.on(event, handler)` — Register event handler
230
+ - `client.off(event, handler)` — Remove event handler
231
+ - `client.connect()` — Connect to stream (returns Promise)
232
+ - `client.disconnect()` — Disconnect from stream
233
+ - `client.connected` — Whether currently connected
234
+ - `client.eventCount` — Total events received
235
+
236
+ ### `new TikTokCaptions(uniqueId, options?)`
237
+
238
+ | Option | Type | Default | Description |
239
+ |--------|------|---------|-------------|
240
+ | `apiKey` | `string` | `process.env.TIKTOOL_API_KEY` | Your TikTool API key |
241
+ | `translate` | `string` | `undefined` | Target translation language |
242
+ | `diarization` | `boolean` | `true` | Enable speaker identification |
243
+ | `maxDurationMinutes` | `number` | `60` | Auto-disconnect timer |
244
+
245
+ **Methods:**
246
+ - `captions.on(event, handler)` — Register event handler
247
+ - `captions.off(event, handler)` — Remove event handler
248
+ - `captions.connect()` — Start receiving captions (returns Promise)
249
+ - `captions.disconnect()` — Stop receiving captions
250
+ - `captions.connected` — Whether currently connected
251
+
252
+ ## Why tiktok-live-api?
253
+
254
+ | | tiktok-live-api | tiktok-live-connector | TikTokLive (Python) |
255
+ |---|---|---|---|
256
+ | **Stability** | ✓ Managed API, 99.9% uptime | ✗ Breaks on TikTok updates | ✗ Breaks on TikTok updates |
257
+ | **TypeScript** | ✓ First-class, fully typed | Partial | N/A |
258
+ | **Live Captions** | ✓ AI speech-to-text | ✗ | ✗ |
259
+ | **Translation** | ✓ Real-time, 50+ languages | ✗ | ✗ |
260
+ | **Maintenance** | ✓ Zero — we handle it | ✗ You fix breakages | ✗ You fix breakages |
261
+ | **CAPTCHA Solving** | ✓ Built-in (Pro+) | ✗ | ✗ |
262
+ | **Feed Discovery** | ✓ See who's live | ✗ | ✗ |
263
+ | **Free Tier** | ✓ 50 requests/day | ✓ Free (unreliable) | ✓ Free (unreliable) |
264
+ | **ESM + CJS** | ✓ Both supported | ✓ | N/A |
265
+
266
+ ## Pricing
267
+
268
+ | Tier | Requests/Day | WebSocket Connections | Price |
269
+ |------|-------------|----------------------|-------|
270
+ | Sandbox | 50 | 1 (60s) | Free |
271
+ | Basic | 10,000 | 3 (8h) | $7/week |
272
+ | Pro | 75,000 | 50 (8h) | $15/week |
273
+ | Ultra | 300,000 | 500 (8h) | $45/week |
274
+
275
+ ## Also Available
276
+
277
+ - **Python**: [`pip install tiktok-live-api`](https://pypi.org/project/tiktok-live-api/)
278
+ - **Any language**: Connect via WebSocket: `wss://api.tik.tools?uniqueId=USERNAME&apiKey=KEY`
279
+ - **Unreal Engine**: Native C++/Blueprint plugin
280
+
281
+ ## Links
282
+
283
+ - 🌐 **Website**: [tik.tools](https://tik.tools)
284
+ - 📖 **Documentation**: [tik.tools/docs](https://tik.tools/docs)
285
+ - 🐍 **Python SDK**: [pypi.org/project/tiktok-live-api](https://pypi.org/project/tiktok-live-api/)
286
+ - 💻 **GitHub**: [github.com/tiktool/tiktok-live-api](https://github.com/tiktool/tiktok-live-api)
287
+
288
+ ## License
289
+
290
+ MIT
@@ -0,0 +1,349 @@
1
+ /**
2
+ * Type definitions for TikTok LIVE API events.
3
+ *
4
+ * These types provide IDE autocompletion and type safety
5
+ * when handling events from {@link TikTokLive} and {@link TikTokCaptions}.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ /** User profile attached to most LIVE events. */
10
+ interface TikTokUser {
11
+ userId: string;
12
+ uniqueId: string;
13
+ nickname: string;
14
+ profilePictureUrl: string;
15
+ followRole: number;
16
+ isSubscriber: boolean;
17
+ }
18
+ /** Payload for `chat` events. */
19
+ interface ChatEvent {
20
+ user: TikTokUser;
21
+ comment: string;
22
+ emotes: Array<{
23
+ emoteId: string;
24
+ image: string;
25
+ }>;
26
+ }
27
+ /** Payload for `gift` events. */
28
+ interface GiftEvent {
29
+ user: TikTokUser;
30
+ giftId: number;
31
+ giftName: string;
32
+ diamondCount: number;
33
+ repeatCount: number;
34
+ repeatEnd: boolean;
35
+ }
36
+ /** Payload for `like` events. */
37
+ interface LikeEvent {
38
+ user: TikTokUser;
39
+ likeCount: number;
40
+ totalLikes: number;
41
+ }
42
+ /** Payload for `member` (viewer join) events. */
43
+ interface MemberEvent {
44
+ user: TikTokUser;
45
+ actionId: number;
46
+ }
47
+ /** Payload for `follow` and `share` events. */
48
+ interface SocialEvent {
49
+ user: TikTokUser;
50
+ eventType: string;
51
+ }
52
+ /** Payload for `roomUserSeq` (viewer count) events. */
53
+ interface RoomUserSeqEvent {
54
+ viewerCount: number;
55
+ topViewers: TikTokUser[];
56
+ }
57
+ /** Payload for `battle` events. */
58
+ interface BattleEvent {
59
+ type: string;
60
+ teams: Array<Record<string, unknown>>;
61
+ scores: number[];
62
+ }
63
+ /** Payload for `caption` events from {@link TikTokCaptions}. */
64
+ interface CaptionEvent {
65
+ text: string;
66
+ speaker: string;
67
+ isFinal: boolean;
68
+ language: string;
69
+ }
70
+ /** Payload for `translation` events from {@link TikTokCaptions}. */
71
+ interface TranslationEvent {
72
+ text: string;
73
+ sourceLanguage: string;
74
+ targetLanguage: string;
75
+ }
76
+ /** Payload for `credits` events from {@link TikTokCaptions}. */
77
+ interface CreditsEvent {
78
+ total: number;
79
+ used: number;
80
+ remaining: number;
81
+ }
82
+ /** Connection event payload. */
83
+ interface ConnectedEvent {
84
+ uniqueId: string;
85
+ }
86
+ /** Disconnection event payload. */
87
+ interface DisconnectedEvent {
88
+ uniqueId: string;
89
+ }
90
+ /** Error event payload. */
91
+ interface ErrorEvent {
92
+ error: string;
93
+ }
94
+ /**
95
+ * Map of event names to their payload types for {@link TikTokLive}.
96
+ */
97
+ interface TikTokLiveEventMap {
98
+ chat: ChatEvent;
99
+ gift: GiftEvent;
100
+ like: LikeEvent;
101
+ follow: SocialEvent;
102
+ share: SocialEvent;
103
+ member: MemberEvent;
104
+ subscribe: SocialEvent;
105
+ roomUserSeq: RoomUserSeqEvent;
106
+ battle: BattleEvent;
107
+ envelope: Record<string, unknown>;
108
+ streamEnd: Record<string, unknown>;
109
+ roomInfo: Record<string, unknown>;
110
+ connected: ConnectedEvent;
111
+ disconnected: DisconnectedEvent;
112
+ error: ErrorEvent;
113
+ event: Record<string, unknown>;
114
+ }
115
+ /**
116
+ * Map of event names to their payload types for {@link TikTokCaptions}.
117
+ */
118
+ interface TikTokCaptionsEventMap {
119
+ caption: CaptionEvent;
120
+ translation: TranslationEvent;
121
+ credits: CreditsEvent;
122
+ credits_low: CreditsEvent;
123
+ status: Record<string, unknown>;
124
+ connected: ConnectedEvent;
125
+ disconnected: DisconnectedEvent;
126
+ error: ErrorEvent;
127
+ }
128
+
129
+ /**
130
+ * TikTokLive — Connect to any TikTok LIVE stream via WebSocket.
131
+ *
132
+ * Receives real-time events: chat messages, gifts, likes, follows,
133
+ * viewer counts, battles, and more. Powered by the TikTool managed API.
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * import { TikTokLive } from 'tiktok-live-api';
138
+ *
139
+ * const client = new TikTokLive('streamer_username', { apiKey: 'YOUR_KEY' });
140
+ *
141
+ * client.on('chat', (event) => {
142
+ * console.log(`${event.user.uniqueId}: ${event.comment}`);
143
+ * });
144
+ *
145
+ * client.connect();
146
+ * ```
147
+ *
148
+ * @packageDocumentation
149
+ */
150
+
151
+ /** Options for {@link TikTokLive} constructor. */
152
+ interface TikTokLiveOptions {
153
+ /** Your TikTool API key. Get one free at https://tik.tools */
154
+ apiKey?: string;
155
+ /** Auto-reconnect on disconnect (default: true). */
156
+ autoReconnect?: boolean;
157
+ /** Max reconnection attempts (default: 5). */
158
+ maxReconnectAttempts?: number;
159
+ }
160
+ type EventHandler$1<T> = (data: T) => void | Promise<void>;
161
+ /**
162
+ * Connect to a TikTok LIVE stream and receive real-time events.
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const client = new TikTokLive('username', { apiKey: 'KEY' });
167
+ * client.on('chat', (e) => console.log(e.comment));
168
+ * client.on('gift', (e) => console.log(`${e.giftName} worth ${e.diamondCount} 💎`));
169
+ * client.connect();
170
+ * ```
171
+ */
172
+ declare class TikTokLive {
173
+ /** TikTok username (without @). */
174
+ readonly uniqueId: string;
175
+ /** Your TikTool API key. */
176
+ readonly apiKey: string;
177
+ /** Whether to auto-reconnect on disconnect. */
178
+ readonly autoReconnect: boolean;
179
+ /** Maximum reconnection attempts. */
180
+ readonly maxReconnectAttempts: number;
181
+ private _handlers;
182
+ private _ws;
183
+ private _connected;
184
+ private _intentionalClose;
185
+ private _reconnectAttempts;
186
+ private _eventCount;
187
+ /**
188
+ * Create a new TikTokLive client.
189
+ *
190
+ * @param uniqueId - TikTok username (without @)
191
+ * @param options - Configuration options
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * const client = new TikTokLive('streamer', { apiKey: 'YOUR_KEY' });
196
+ * ```
197
+ */
198
+ constructor(uniqueId: string, options?: TikTokLiveOptions);
199
+ /** Whether the client is currently connected. */
200
+ get connected(): boolean;
201
+ /** Total number of events received this session. */
202
+ get eventCount(): number;
203
+ /**
204
+ * Register an event handler.
205
+ *
206
+ * @param event - Event name (chat, gift, like, follow, etc.)
207
+ * @param handler - Callback function
208
+ * @returns this (for chaining)
209
+ *
210
+ * @example
211
+ * ```typescript
212
+ * client.on('chat', (event) => console.log(event.comment));
213
+ * client.on('gift', (event) => console.log(event.giftName));
214
+ * ```
215
+ */
216
+ on<K extends keyof TikTokLiveEventMap>(event: K, handler: EventHandler$1<TikTokLiveEventMap[K]>): this;
217
+ on(event: string, handler: EventHandler$1<any>): this;
218
+ /**
219
+ * Remove an event handler.
220
+ *
221
+ * @param event - Event name
222
+ * @param handler - The handler to remove
223
+ */
224
+ off(event: string, handler: EventHandler$1<any>): this;
225
+ private _emit;
226
+ /**
227
+ * Connect to the TikTok LIVE stream.
228
+ *
229
+ * @returns Promise that resolves when connected, rejects on fatal error.
230
+ *
231
+ * @example
232
+ * ```typescript
233
+ * await client.connect();
234
+ * ```
235
+ */
236
+ connect(): Promise<void>;
237
+ /**
238
+ * Disconnect from the stream.
239
+ */
240
+ disconnect(): void;
241
+ private _maybeReconnect;
242
+ }
243
+
244
+ /**
245
+ * TikTokCaptions — Real-time AI speech-to-text for TikTok LIVE streams.
246
+ *
247
+ * Transcribe and translate any TikTok LIVE stream in real-time.
248
+ * This feature is unique to TikTool Live — no other service offers it.
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * import { TikTokCaptions } from 'tiktok-live-api';
253
+ *
254
+ * const captions = new TikTokCaptions('streamer', {
255
+ * apiKey: 'YOUR_KEY',
256
+ * translate: 'en',
257
+ * diarization: true,
258
+ * });
259
+ *
260
+ * captions.on('caption', (event) => {
261
+ * console.log(`[${event.speaker}] ${event.text}`);
262
+ * });
263
+ *
264
+ * captions.connect();
265
+ * ```
266
+ *
267
+ * @packageDocumentation
268
+ */
269
+
270
+ /** Options for {@link TikTokCaptions} constructor. */
271
+ interface TikTokCaptionsOptions {
272
+ /** Your TikTool API key. Get one at https://tik.tools */
273
+ apiKey?: string;
274
+ /** Target language code for real-time translation (e.g. "en", "es"). */
275
+ translate?: string;
276
+ /** Enable speaker identification (default: true). */
277
+ diarization?: boolean;
278
+ /** Auto-disconnect after N minutes (default: 60, max: 300). */
279
+ maxDurationMinutes?: number;
280
+ }
281
+ type EventHandler<T> = (data: T) => void | Promise<void>;
282
+ /**
283
+ * Real-time AI speech-to-text for TikTok LIVE streams.
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * const captions = new TikTokCaptions('streamer', {
288
+ * apiKey: 'KEY',
289
+ * translate: 'en',
290
+ * });
291
+ * captions.on('caption', (e) => console.log(e.text));
292
+ * captions.on('translation', (e) => console.log(`→ ${e.text}`));
293
+ * captions.connect();
294
+ * ```
295
+ */
296
+ declare class TikTokCaptions {
297
+ /** TikTok username (without @). */
298
+ readonly uniqueId: string;
299
+ /** Your TikTool API key. */
300
+ readonly apiKey: string;
301
+ /** Target translation language. */
302
+ readonly translate?: string;
303
+ /** Whether speaker diarization is enabled. */
304
+ readonly diarization: boolean;
305
+ /** Max session duration in minutes. */
306
+ readonly maxDurationMinutes?: number;
307
+ private _handlers;
308
+ private _ws;
309
+ private _connected;
310
+ private _intentionalClose;
311
+ /**
312
+ * Create a new TikTokCaptions client.
313
+ *
314
+ * @param uniqueId - TikTok username (without @)
315
+ * @param options - Configuration options
316
+ */
317
+ constructor(uniqueId: string, options?: TikTokCaptionsOptions);
318
+ /** Whether currently connected and receiving captions. */
319
+ get connected(): boolean;
320
+ /**
321
+ * Register an event handler.
322
+ *
323
+ * @param event - Event name (caption, translation, credits, status, error)
324
+ * @param handler - Callback function
325
+ *
326
+ * @example
327
+ * ```typescript
328
+ * captions.on('caption', (event) => {
329
+ * const prefix = event.speaker ? `[${event.speaker}] ` : '';
330
+ * console.log(`${prefix}${event.text}${event.isFinal ? ' ✓' : '...'}`);
331
+ * });
332
+ * ```
333
+ */
334
+ on<K extends keyof TikTokCaptionsEventMap>(event: K, handler: EventHandler<TikTokCaptionsEventMap[K]>): this;
335
+ on(event: string, handler: EventHandler<any>): this;
336
+ /** Remove an event handler. */
337
+ off(event: string, handler: EventHandler<any>): this;
338
+ private _emit;
339
+ /**
340
+ * Start receiving captions from the stream.
341
+ *
342
+ * @returns Promise that resolves when connected.
343
+ */
344
+ connect(): Promise<void>;
345
+ /** Stop receiving captions. */
346
+ disconnect(): void;
347
+ }
348
+
349
+ export { type BattleEvent, type CaptionEvent, type ChatEvent, type ConnectedEvent, type CreditsEvent, type DisconnectedEvent, type ErrorEvent, type GiftEvent, type LikeEvent, type MemberEvent, type RoomUserSeqEvent, type SocialEvent, TikTokCaptions, type TikTokCaptionsEventMap, type TikTokCaptionsOptions, TikTokLive, type TikTokLiveEventMap, type TikTokLiveOptions, type TikTokUser, type TranslationEvent };