Git Product home page Git Product logo

tiktok-api's Introduction

Unofficial TikTok/Musical.ly API

npm version Coverage Status Build Status PRs Welcome Supported TikTok version

A reverse-engineered implementation of the TikTok (previously musical.ly) app's API.

Installation

npm i tiktok-api

Usage

Creating an instance

import TikTokAPI, { getRequestParams } from 'tiktok-api';

// Required - a method that signs the URL with anti-spam parameters
// You must provide an implementation yourself to successfully make
// most requests with this library.
const signURL = async (url, ts, deviceId) => {
  const as = 'anti-spam parameter 1';
  const cp = 'anti-spam parameter 2'
  const mas = 'anti-spam parameter 3';
  return `${url}&as=${as}&cp=${cp}&mas=${mas}`;
}

// Required - device parameters
// You need to source these using a man-in-the-middle proxy such as mitmproxy,
// CharlesProxy or PacketCapture (Android)
const params = getRequestParams({
  device_id: '<device_id>',
  fp: '<device_fingerprint>',
  iid: '<install_id>',
  openudid: '<device_open_udid>',
});

const api = new TikTokAPI(params, { signURL });

// You are now able to make successful requests

Instance methods

.loginWithEmail(email, password)

Authenticates you with the API and stores your session data in a cookie jar. Subsequent requests will include these cookies.

api.loginWithEmail('<email>', '<password>')
  .then(res => console.log(res.data))
  .catch(console.log)

// Outputs:
// { email: '<email>', session_key: '123456', user_id: '123456', ... }

See the login types for the response data.

.loginWithUsername(username, password)

Authenticates you with the API and stores your session data in a cookie jar. Subsequent requests will include these cookies.

api.loginWithUsername('<username>', '<password>')
  .then(res => console.log(res.data))
  .catch(console.log)

// Outputs:
// { username: '<email>', session_key: '123456', user_id: '123456', ... }

See the login types for the response data.

.getUser(id)

Gets a user's profile.

api.getUser('<user_id>')
  .then(res => console.log(res.data.user))
  .catch(console.log);

// Outputs:
// { aweme_count: 1000, nickname: 'example', unique_id: 'musername', ... }

See the user types for the response data.

.searchUsers(params)

Searches for users.

api.searchUsers({
  keyword: 'example',
  count: 10,
  cursor: 0,
})
  .then(res => console.log(res.data.user_list))
  .catch(console.log);

// Outputs:
// [{ user_info: {...}, position: [], uniqposition: [] }, ...]

See the search types for the complete request/response objects.

.getQRCode(id, [schemaType])

Gets the QR code for a user.

api.getQRCode('<user_id>')
  .then(res => console.log(res.data.qrcode_url.url_list[0]))
  .catch(console.log);

// Outputs:
// 'http://p16.muscdn.com/img/musically-qrcode/1111111111111111111~c5_720x720.image'

See the QR code types for the complete request/response objects.

.getPost(id)

Gets a post.

api.getPost('<user_id>')
  .then(res => console.log(res.data.aweme_detail))
  .catch(console.log);

// Outputs:
// { author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...}, ... }

See the post types for the complete response object.

.listPosts(params)

Lists a user's posts.

api.listPosts({
  user_id: '<user_id>',
  max_cursor: 0,
})
  .then(res => console.log(res.data.aweme_list))
  .catch(console.log);

// Outputs:
// [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

See the post types for the complete request/response objects.

.listFollowers(params)

Lists the users that follow the specified user.

api.listFollowers({
  user_id: '<user_id>',
  max_time: Math.floor(new Date().getTime() / 1000),
})
  .then(res => console.log(res.data.followers))
  .catch(console.log);

// Outputs:
// [{ unique_id: 'follower1' }, { unique_id: 'follower2' }, ...]

See the follower types for the complete request/response objects.

.listFollowing(params)

Lists the users that the specified user follows.

api.listFollowing({
  user_id: '<user_id>',
  max_time: Math.floor(new Date().getTime() / 1000),
})
  .then(res => console.log(res.data.followings))
  .catch(console.log);

// Outputs:
// [{ unique_id: 'following1' }, { unique_id: 'following2' }, ...]

See the following types for the complete request/response objects.

.follow(id)

Follows a user.

api.follow('<user_id>')
  .then(res => console.log(res.data.follow_status))
  .catch(console.log);

// Outputs:
// 1

See the follow types for the response data.

.unfollow(id)

Stops following a user.

api.unfollow('<user_id>')
  .then(res => console.log(res.data.follow_status))
  .catch(console.log);

// Outputs:
// 0

See the follow types for the response data.

.listReceivedFollowRequests(params)

Lists the users that have requested to follow the logged in user.

api.listReceivedFollowRequests({
  max_time: Math.floor(new Date().getTime() / 1000),
  count: 10,
})
  .then(res => console.log(res.data.request_users))
  .catch(console.log);

// Outputs:
// [{ unique_id: 'user1' }, { unique_id: 'user2' }, ...]

See the follow types for the complete request/response objects.

.approveFollowRequest(id)

Approves a user's request to follow you.

api.approveFollowRequest('<user_id>')
  .then(res => console.log(res.data.approve_status))
  .catch(console.log);

// Outputs:
// 0

See the follow types for the response data.

.rejectFollowRequest(id)

Rejects a user's request to follow you.

api.rejectFollowRequest('<user_id>')
  .then(res => console.log(res.data.reject_status))
  .catch(console.log);

// Outputs:
// 0

See the follow types for the response data.

.likePost(id)

Likes a post.

api.likePost('<post_id>')
  .then(res => console.log(res.data.is_digg))
  .catch(console.log);

// Outputs:
// 1

.unlikePost(id)

Unlikes a post.

api.unlikePost('<post_id>')
  .then(res => console.log(res.data.is_digg))
  .catch(console.log);

// Outputs:
// 0

.listComments(params)

Lists comments for a post.

api.listComments({
  aweme_id: '<post_id>',
  cursor: 0,
})
  .then(res => console.log(res.data.comments))
  .catch(console.log);

// Outputs:
// [{ text: 'first!', user: {...} }, { text: 'second!', user: {...} }, ...]

See the comment types for the response data.

.postComment(postId, text, [tags])

Comments on a post.

api.postComment('<post_id>', 'first!')
  .then(res => console.log(res.data.comment))
  .catch(console.log);

// Outputs:
// { cid: '<comment_id>', text: 'first!', user: {...}, ... }

See the comment types for the response data.

.listCategories(params)

Lists popular categories/hashtags.

api.listCategories({
  count: 10,
  cursor: 0,
})
  .then(res => console.log(res.data.category_list))
  .catch(console.log);

// Outputs:
// [{ { challenge_info: { cha_name: 'posechallenge', cid: '123' }, desc: 'Trending Hashtag' }, ...]

See the category types for the complete request/response objects.

.searchHashtags(params)

Searches for hashtags.

api.searchHashtags({
  keyword: 'example',
  count: 10,
  cursor: 0,
})
  .then(res => console.log(res.data.challenge_list))
  .catch(console.log);

// Outputs:
// [{ challenge_info: {...}, position: [] }, ...]

See the search types for the complete request/response objects.

.listPostsInHashtag(params)

Lists posts in a hashtag.

api.listPostsInHashtag({
  ch_id: '<hashtag_id>',
})
  .then(res => console.log(res.data.aweme_list))
  .catch(console.log);

// Outputs:
// [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

See the hashtag types for the complete request/response objects.

.listForYouFeed([params])

Lists posts in the For You feed.

api.listForYouFeed()
  .then(res => console.log(res.data.aweme_list))
  .catch(console.log);

// Outputs:
// [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

See the feed types for the complete request/response objects.

.listFollowingFeed([params])

Lists posts in the Following feed.

api.listFollowingFeed()
  .then(res => console.log(res.data.aweme_list))
  .catch(console.log);

// Outputs:
// [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

See the feed types for the complete request/response objects.

.getSticker(id)

Gets information about a sticker/effect.

api.getSticker('<sticker_id>')
  .then(res => console.log(res.data.sticker_infos))
  .catch(console.log);

// Outputs:
// [{ id: '100000', name: 'cloned', owner_nickname: 'Effect Assistant', ...}]

See the sticker types for the complete response object.

.getStickers([ids])

Gets information about many stickers/effects.

api.getStickers(['<sticker_id>', '<sticker_id>'])
  .then(res => console.log(res.data.sticker_infos))
  .catch(console.log);

// Outputs:
// [{ id: '100000', name: 'cloned', owner_nickname: 'Effect Assistant', ...}, ...]

See the sticker types for the complete response object.

.listPostsBySticker(params)

Lists posts that use a sticker/effect.

api.listPostsBySticker({
  count: 20,
  cursor: 0,
  sticker_id: '100000',
})
  .then(res => console.log(res.data.aweme_list))
  .catch(console.log);

// Outputs:
// [{ author: {...}, aweme_id: '999', desc: 'description', music: {...}, statistics: {...}, video: {...} }, ...]

See the sticker types for the complete request/response object.

.joinLiveStream(id)

Joins a live stream.

The rtmp_pull_url value can be used with VLC's Open Network Stream option.

api.joinLiveStream('<room_id>')
  .then(res => console.log(res.data.room))
  .catch(console.log);

// Outputs:
// { create_time: 1000000000, owner: {...}, stream_url: {...}, title: 'Example', user_count: 1000, ... }

See the live stream types for the response data.

.leaveLiveStream(id)

Leaves a live stream.

api.leaveLiveStream('<room_id>')
  .then(res => console.log(res.data.status_code))
  .catch(console.log);

// Outputs:
// 0

.canStartLiveStream()

Determines if the current user is allowed to start a live stream.

api.canStartLiveStream()
  .then(res => console.log(res.data.can_be_live_podcast))
  .catch(console.log);

// Outputs:
// true

See the live stream types for the response data.

.startLiveStream(title, [contactsAuthorized])

Starts a live stream by calling createLiveStreamRoom then updateLiveStreamStatus.

Keep note of the room_id and stream_id properties because you will need them to end the live stream.

The rtmp_push_url value can be used with streaming applications such as OBS.

api.startLiveStream('title')
  .then(res => console.log(res.data.room))
  .catch(console.log);

// Outputs:
// { create_time: 1000000000, owner: {...}, stream_url: {...}, title: 'Example', user_count: 1000, ... }

See the live stream types for the response data.

.endLiveStream(roomId, streamId)

Ends a live stream.

You must call this method to so you are no longer marked as "live" in the app.

api.endLiveStream('<room_id>', '<stream_id>')
  .then(res => console.log(res.data.status_code))
  .catch(console.log);

// Outputs:
// 0

.createLiveStreamRoom(title, [contactsAuthorized])

Creates a room to host a live stream.

The rtmp_push_url value can be used with streaming applications such as OBS.

Note: This method only creates the room for the live stream. You'll need to call updateLiveStreamStatus to mark the stream as started. See startLiveStream for a helper method that makes these calls for you.

api.startLiveStream('title')
  .then(res => console.log(res.data.room))
  .catch(console.log);

// Outputs:
// { create_time: 1000000000, owner: {...}, stream_url: {...}, title: 'Example', user_count: 1000, ... }

See the live stream types for the response data.

.updateLiveStreamStatus(params)

Updates the status of a live stream.

api.updateLiveStreamStatus({
  room_id: '<room_id>',
  stream_id: '<stream_id>',
  status: LiveStreamStatus.Ended,
  reason_no: LiveStreamStatusChangedReason.InitiatedByUser,
})
  .then(res => console.log(res.data.status_code))
  .catch(console.log);

// Outputs:
// 0

See the live stream types for the complete request/response objects.

Resources

Legal

This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by TikTok or any of its affiliates or subsidiaries. This is an independent and unofficial API. Use at your own risk.

tiktok-api's People

Contributors

dependabot[bot] avatar szdc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.