1.0.3 • Published 4 years ago

cs-tiktok-api v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

TikTok API

This is an unofficial Node.js implementation of the TikTok API. This module is not endorsed by, directly affiliated with, maintained, authorized, or sponsored by TikTok. Their internal API could change at any moment, and break this module's code. I try my best to maintain it, and keep it up-to-date. If you find bugs or have any questions, please submit an issue, and I will try my best to help you out.

Table of Contents

Quick Examples

Get the follower count of a TikTok user:

const tiktok = require('tiktok-app-api');

let tiktokApp;

main();

async function main() {
  tiktokApp = await tiktok();

  const user = await tiktokApp.getUserByName('example');
  const userInfo = await tiktokApp.getUserInfo(user);

  console.log(userInfo.followerCount);
}

Get the tags of the top trending video:

const tiktok = require('tiktok-app-api');

let tiktokApp;

main();

async function main() {
  tiktokApp = await tiktok();

  const trendingVideos = await tiktokApp.getTrendingVideos();

  console.log(trendingVideos[0].tags);
}

Take a look at tiktok-web-api-example for an up-to-date web API using this module.

Installation

Install the API:

npm i tiktok-app-api

If you would like to run the default signature service, you must also install tiktok-signature. To setup an independent signature service, take a look at tiktok-signature-api.

npm i tiktok-signature

Puppeteer will be installed alongside tiktok-signature, as API requests will be made through a headless browser instance.

Import into your program.

const tiktok = require('tiktok-app-api');

// Or, if you are using TypeScript:
import tiktok = require('tiktok-app-api'):

Usage

At the moment, the majority of the API returns promises, and may throw errors in certain situations. When a promise is not returned, or if a function may throw an error, it will be mentioned.

Application Instance

To start using the API, you must first instantiate an instance of the application.

const tiktok = require('tiktok-app-api');

let tiktokApp;

main();

async function main() {
  tiktokApp = await tiktok();
}

While instantiating a new instance of the application, the default signature service, if being used, and any default settings of the application will be set up.

Options

If you would like to use your own signature service instead of the default option, you can specify this in a TikTokOptions object. See TikTokOptions for a definition of the TikTokOptions object.

const tiktokApp = await tiktok({
  signatureService: 'http://localhost:8000/api/sign'
});

Trending

To get the top trending videos using the API is as simple as:

const trendingVideos = await tiktokApp.getTrendingVideos();

console.log(trendingVideos);

Users

To retrieve User information within the API, you will first need to get a User object. See User for a definition of the User object.

Get a User object from a TikTok user's username:

Will fetch ID of user from TikTok API. May throw an error in certain situations, see here.

const user = await tiktokApp.getUserByName('example');

Get a User object from a TikTok user's id:

Will not fetch username of user from TikTok API.

const user = tiktokApp.getUserByID('6828402025898902533');

Take note that getUserByID(id) does not return a promise, as it does not fetch any information from the TikTok API.

Now that we have a User object, we can retrieve some information from the TikTok user:

See UserInfo for a defintion of the UserInfo object. May throw an error in certain situations, see here.

const userInfo = await tiktokApp.getUserInfo(user);

console.log(userInfo.followingCount, userInfo.followerCount, userInfo.likeCount);

Now, let's get the user's lastest videos:

See VideoInfo for a definition of the VideoInfo object. May throw an error in certain situations, see here.

const recentVideos = await tiktokApp.getRecentVideos(user);

console.log(recentVideos);

Same idea, we can get the user's liked videos:

May throw an error in certain situations, see here.

const likedVideos = await tiktokApp.getLikedVideos(user);

console.log(likedVideos);

That covers users. Let's move on to TikTok videos.

Videos

Just like how previously we needed our User object, we now need our Video object. See Video for a definition of the Video object.

const video = tiktokApp.getVideo('6829280471411674374');

Take note that getVideo(id) does not return a promise, as it does not fetch any information from the TikTok API.

Now to get the information of this video:

May throw an error in certain situations, see here.

const videoInfo = await tiktokApp.getVideoInfo(video);

console.log(videoInfo.commentCount, videoInfo.author, videoInfo.video.id);

That's all there is for videos, for now. Now on to Tiktok audios.

Audios

Just like Users and Videos, we first need an Audio object.

const audio = tiktokApp.getAudio('6829280451471969029');

Take note that getAudio(id) does not return a promise, as it does not fetch any information from the TikTok API.

To get the information related to the audio:

See AudioInfo for a definition of the AudioInfo object. May throw an error in certain situations, see here.

const audioInfo = await tiktokApp.getAudioInfo(audio);

console.log(audioInfo.title, audioInfo.audio.title);

To get the top videos related to an audio:

The first object of this VideoInfo array will be the original video with the audio. May throw an error in certain situations, see here.

const topVideos = await tiktokApp.getAudioTopVideos(audio);

console.log(topVideos[0]);

Finally we have TikTok tags.

Tags

Just like Users, Videos, and Audios, we need a Tag object.

Will fetch the title from the TikTok API, therefore this function returns a promise. May throw an error in certain situations, see here.

const tag = await tiktokApp.getTag('fyp');

To retrieve the information associated with the tag:

See TagInfo for a definition of the TagInfo object. May throw an error in certain situations, see here.

const tagInfo = await tiktokApp.getTagInfo(tag);

console.log(tagInfo.description, tagInfo.videoCount, tagInfo.viewCount);

To get the top videos of a tag:

May throw an error in certain situations, see here.

const topVideos = await tiktokApp.getTagTopVideos(tag);

console.log(topVideos);

Object Reference

Below you will find the data that each object contains.

TikTokOptions

TikTokOptions {
  signatureService?: string // Not required.
}

User

User {
  id: string,
  username?: string // Not required initially.
}

UserInfo

UserInfo {
  user: User,
  avatar: string,
  nickname: string,
  signature: string,
  followingCount: number,
  followerCount: number,
  likeCount: number,
  videoCount: number
}

Video

Video {
  id: string
}

VideoInfo

VideoInfo {
  video: Video,
  author: User,
  playCount: number,
  likeCount: number,
  commentCount: number,
  shareCount: number,
  description: string,
  tags: Tag[],
  audio: AudioInfo
}

Audio

Audio {
  id: string
}

AudioInfo

AudioInfo {
  id: string,
  title: string
}

Tag

Tag {
  id: string,
  title: string
}

TagInfo

TagInfo {
  tag: Tag,
  description: string,
  videoCount: number,
  viewCount: number
}
1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago