tiktok-live-nuxt
v2.1.0
Published
Nuxt module for TikTok LIVE API - real-time chat, gifts, viewers, PK battles with MVP breakdown, and x2/x3 boosters
Maintainers
Readme
<script setup>
const { messages, viewers, gifts, connected } = useTikTokLive('creator_username')
</script>
<template>
<div>
<p>{{ viewers }} viewers</p>
<div v-for="msg in messages" :key="msg.data?.msgId">
<b>{{ msg.data?.user?.uniqueId }}:</b> {{ msg.data?.comment }}
</div>
</div>
</template>That's it. Auto-imported, reactive, auto-connects on mount.
Quick Setup
1. Install
# npm
npm install tiktok-live-nuxt
# pnpm
pnpm add tiktok-live-nuxt
# bun
bun add tiktok-live-nuxt
# yarn
yarn add tiktok-live-nuxt2. Configure
Add to nuxt.config.ts:
export default defineNuxtConfig({
modules: ['tiktok-live-nuxt'],
tiktool: {
apiKey: 'YOUR_API_KEY', // Get a key at https://tik.tools
creators: ['creator_username'] // creators this site may connect to
}
})| Option | Default | Description |
|--------|---------|-------------|
| apiKey | TIKTOOL_API_KEY | Your API key. Server-side only, never sent to the browser. |
| creators | [] | Creators the token route will issue tokens for. Any other creator gets 403. |
| allowAnyCreator | false | Skip the creators check. Only enable on a private or authenticated site. |
| tokensPerMinute | 10 | Tokens each visitor IP may request per minute. |
| trustProxy | false | Read the visitor IP from proxy headers. Behind a proxy or CDN set trustProxy: true, otherwise all visitors share one limit. |
| baseUrl | TIKTOOL_BASE_URL, then https://api.tik.tools | Server address for the token request and the browser WebSocket. |
Paid plans (Basic and up) can use the direct endpoint with the same key: set baseUrl: 'https://direct.tik.tools' (or TIKTOOL_BASE_URL=https://direct.tik.tools). The browser then connects to wss://direct.tik.tools. Sandbox keys are refused there.
Enable trustProxy only when your proxy overwrites these headers (Cloudflare does; for plain nginx set X-Forwarded-For to $remote_addr), otherwise clients can spoof them.
Or via environment variable (recommended for production). The key stays on your server: the module adds a /api/_tiktool/token route that hands the browser a short-lived token scoped to the creator it connects to.
TIKTOOL_API_KEY=your_api_key3. Use
<script setup>
const { messages, viewers, gifts, connected, error } = useTikTokLive('username')
</script>No imports needed - useTikTokLive is auto-imported by the module.
Upgrading from 1.x
- The API key is no longer exposed to the browser. The module adds a server route (
/api/_tiktool/token) that issues short-lived tokens, so your app needs a Nuxt server runtime; fully staticnuxt generatebuilds are not supported. - The composable's
apiKeyoption was removed. Set the key innuxt.config.tsorTIKTOOL_API_KEY. - List the creators you connect to under
tiktool.creators.
SSR & Client-Side
This composable uses WebSocket which only runs in the browser. It's designed to work correctly in both SSR and CSR modes:
| Mode | Behavior |
|------|----------|
| SSR (ssr: true) | Server renders initial empty state. WebSocket connects on client hydration via onMounted. |
| SPA (ssr: false) | WebSocket connects immediately on mount. |
| Client-only component | Wrap in <ClientOnly> if you want to avoid SSR flash of empty state. |
<!-- Option 1: Works out of the box (SSR-safe) -->
<script setup>
const { messages, connected } = useTikTokLive('creator_username')
</script>
<!-- Option 2: Client-only wrapper (avoids empty state flash) -->
<ClientOnly>
<TikTokChat username="creator_username" />
</ClientOnly>API Reference
useTikTokLive(uniqueId, options?)
| Param | Type | Default | Description |
|-------|------|---------|-------------|
| uniqueId | string | - | TikTok username (with or without @) |
| options.autoConnect | boolean | true | Connect on mount |
Reactive Returns
| Property | Type | Description |
|----------|------|-------------|
| connected | Ref<boolean> | WebSocket connection state |
| viewers | Ref<number> | Current viewer count |
| messages | Ref<TikTokEvent[]> | Chat messages (last 100) |
| gifts | Ref<TikTokEvent[]> | Gift events (last 50) |
| allEvents | Ref<TikTokEvent[]> | All events (last 200) |
| eventCount | Ref<number> | Total events received |
| error | Ref<string \| null> | Last error message |
Methods
| Method | Description |
|--------|-------------|
| connect() | Manually connect (if autoConnect: false) |
| disconnect() | Close the connection |
| on(event, handler) | Register a custom event handler |
Events
<script setup>
const tiktok = useTikTokLive('creator_username')
tiktok.on('chat', (data) => {
// data.user.uniqueId, data.comment
})
tiktok.on('gift', (data) => {
// data.user.uniqueId, data.giftName, data.diamondCount
})
tiktok.on('like', (data) => {
// data.user.uniqueId, data.likeCount
})
tiktok.on('follow', (data) => {
// data.user.uniqueId
})
tiktok.on('roomUserSeq', (data) => {
// data.viewerCount
})
</script>| Event | Key Fields |
|-------|-----------|
| chat | user.uniqueId, comment, emotes |
| gift | user.uniqueId, giftName, diamondCount, repeatCount, repeatEnd |
| like | user.uniqueId, likeCount, totalLikes |
| follow | user.uniqueId |
| share | user.uniqueId |
| member | user.uniqueId (viewer joined) |
| subscribe | user.uniqueId |
| roomUserSeq | viewerCount, topViewers[] |
| battle | battleId, status, teams[] (PK on/off) |
| battleArmies | battleId, hosts[] (per-host MVP contributors), secsRemaining |
| battleItemCard | effect (gloves / mist / booster_x2 / booster_x3 / match_guide / thunder / extra_time), multiplier, iconUrl, accentColor |
| roomPin | user.uniqueId, comment (starred message) |
PK Battles + Boosters
battleArmies.hosts[] gives per-host MVP breakdown sorted by score:
<script setup>
const tiktok = useTikTokLive('streamer_name')
tiktok.on('battleArmies', (e) => {
console.log(`${e.secsRemaining}s left`)
for (const host of e.hosts ?? []) {
const mvp = host.contributors[0]
console.log(`${host.hostUserId}: ${host.teamTotalScore} (MVP ${mvp?.nickname}: ${mvp?.score})`)
}
})
tiktok.on('battleItemCard', (e) => {
// x2/x3 boosters, gloves, mist, thunder, extra-time, match-guide
const tag = e.multiplier > 0 ? `x${e.multiplier} BOOSTER` : e.effect.toUpperCase()
console.log(`${e.senderNickname} -> ${tag}`)
// e.iconUrl + e.accentColor for drop-in overlay
})
</script>Full Example
<script setup>
const username = ref('creator_username')
const tiktok = useTikTokLive(username.value)
const topGifters = computed(() => {
const map = new Map()
for (const g of tiktok.gifts.value) {
const user = g.data?.user?.uniqueId
const diamonds = g.data?.diamondCount || 0
map.set(user, (map.get(user) || 0) + diamonds)
}
return [...map.entries()].sort((a, b) => b[1] - a[1]).slice(0, 5)
})
</script>
<template>
<div v-if="tiktok.error.value" class="error">{{ tiktok.error.value }}</div>
<div v-else-if="!tiktok.connected.value">Connecting to @{{ username }}...</div>
<div v-else>
<p>Connected - {{ tiktok.viewers.value }} viewers - {{ tiktok.eventCount.value }} events</p>
<h3>Chat</h3>
<div v-for="msg in tiktok.messages.value" :key="msg.data?.msgId">
<b>{{ msg.data?.user?.uniqueId }}:</b> {{ msg.data?.comment }}
</div>
<h3>Top Gifters</h3>
<div v-for="([name, diamonds], i) in topGifters" :key="name">
{{ i + 1 }}. @{{ name }} - {{ diamonds }} diamonds
</div>
</div>
</template>Other SDKs
| Platform | Package | Install |
|----------|---------|---------|
| Node.js / TypeScript | tiktok-live-api | npm i tiktok-live-api |
| Python | tiktok-live-api | pip install tiktok-live-api |
| Any language | WebSocket API | wss://api.tik.tools |
Links
- tik.tools - Dashboard & API keys
- Documentation
License
MIT - tik.tools
