TikTok API: How to Post to TikTok Using a Social Media API
Tags: javascript, python, programming
Published: 2022-01-17 22:43:59

You are probably familiar with the [official TikTok app](https://apps.apple.com/us/app/tiktok/id835599320) to create and share videos. But, did you know you can also directly post videos using the TikTok API? 

Using a social media API opens worlds of possibilities to create your own app, platform, or service for your users, such as scheduling TikTok posts directly from your platform. 

Ayrshare recently introduced direct TikTok video sharing and enhanced user profile data and analytics. Continue reading to learn about how to use the TikTok API for your own platform.

## TikTok’s Growth
If you haven’t heard, TikTok is big! TikTok has over 3.3B installs worldwide, [1B monthly active users](https://www.statista.com/statistics/1267892/tiktok-global-mau/#:~:text=TikTok%20global%20monthly%20active%20users%202018%2D2021&text=In%20September%202021%2C%20social%20video,the%20biggest%20social%20networks%20worldwide.), and in Q4 2021 had the most installs ever in a single quarter. Their growth seems unstoppable and continues to accelerate during the pandemic lockdowns.

TikTok also has a thriving [ecosystem](https://sensortower.com/solutions/store-intelligence) of apps that support video creation and downloads. Major new companies that build upon TikTok will emerge – as has happened for Facebook and Instagram. And how did developers create these Facebook and Instagram 3rd party apps? By using an API.

## Social Media APIs
An API, or Application Programming Interface, gives you, or your developers, the power to create your own app or platform and leverage the capabilities of 3rd parties. You build the experience (GUI) that exactly meets your business needs and on the backend harness the power of external datasets or functionality. 

For example, the popular weather app Dark Skies, now owned by Apple, has a unique interface and capabilities such as letting you know it will rain in 15 minutes. Dark Skies and all other weather apps need to get this weather data from somewhere and I guarantee they don’t have their own network of weather satellites. In the U.S. most weather apps get their data from the National Weather Service via their API and build their own GUI front-end and unique capabilities.

Social media schedulers do the same thing by using the social media APIs available from social networks such as LinkedIn, Facebook, or Twitter.

## Share Videos Using the TikTok API
TikTok recently introduced the ability to directly share videos via their [API](https://developers.tiktok.com/doc) – called the Video Kit. Previously you could only share via an iOS or Android app, but now you can post directly to their endpoint. Side note, there is also a [TikTok Marketing API](https://ads.tiktok.com/marketing_api/homepage) if you want to manage ads.

The TikTok social media endpoint uses typical OAuth with tokens. OAuth allows users to authenticate and grant permissions to your all. These grant tokens expire after 24 hours, but can be refreshed with an additional API call. However, after a year the user need to re-authenticate and give permissions again.

TikTok requires you to request approval and go through a review process for API access, which can take a few days to a few weeks. Since their API endpoint is so new, there are several missing features, such deleting posts, and bugs that might prevent quick development.

Finally, there are a few other interesting aspects of the TikTok API documentation you might want to explore, such as the [Sound Kit](https://developers.tiktok.com/doc/sound-kit-share-sound) for sharing sounds or Webhooks to get notified of actions.

## Alternative Integration
An alternative option to directly integrating with the TikTok API is to integrate with Ayrshare’s social media API which includes [TikTok integration](https://docs.ayrshare.com/gui-dashboard/connect-social-media/tiktok-linking). You no longer need approval or worry about the details of TikTok’s evolving API.

For example, to post a new TikTok video use the following Javascript code with the [/post](https://docs.ayrshare.com/rest-api/endpoints/post#send-a-post) endpoint. Be sure to replace API_KEY with your key from the [dashboard](https://app.ayrshare.com/):

```javascript
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer API_KEY");

var raw = JSON.stringify({
  "post": "Today is a great day!",
  "platforms": [
    "tiktok"
  ],
  "mediaUrls": [
    "https://images.ayrshare.com/imgs/test-video.mp4"
  ]
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://app.ayrshare.com/api/post", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
```

Here is a [TikTok Example](https://www.tiktok.com/@borneild/video/7042339075314961711)

Or if you prefer to call the Ayrshare TikTok API in Python:

```python
import requests
import json

url = "https://app.ayrshare.com/api/post"

payload = json.dumps({
  "post": "Today is a great day!",
  "platforms": [
    "tiktok"
  ],
  "mediaUrls": [
    "https://images.ayrshare.com/imgs/test-video.mp4"
  ]
})
headers = {
  'Authorization': 'Bearer API_KEY'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```

After posting, go to your TikTok app to complete the posting by selecting the video and following the instructions.

Enhanced TikTok User and Post Analytics
In addition to posting videos to TikTok, you also might want analytics information about the user or individual post, such as how many views, shares, or likes.

For example, to get the user level analytics across all their TikTok videos call the [/analytics](https://docs.ayrshare.com/rest-api/endpoints/analytics#analytics-on-a-social-network) endpoint. Here is the code in Javascript:

```javascript
var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer API_KEY");

var urlencoded = new URLSearchParams();
urlencoded.append("platforms[0]", "tiktok");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: urlencoded,
  redirect: 'follow'
};

fetch("https://app.ayrshare.com/api/analytics/social", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));
```

And the Python TikTok API code:

```python
import requests

url = "https://app.ayrshare.com/api/analytics/social"

payload='platforms%5B0%5D=tiktok'
headers = {
  'Authorization': 'Bearer API_KEY'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)
```

The user level analytics data returned includes the average video duration and total like, comments, share, and views.

```json
{
   "tiktok": {
        "analytics": {
            "durationAverage": 4.8,
            "likeCountTotal": 4,
            "commentCountTotal": 6,
            "shareCountTotal": 34,
            "viewCountTotal": 193
        }
}
```

If you want to get all past video posts and analytics on each, call the [/history](https://docs.ayrshare.com/rest-api/endpoints/history#get-past-posts-instagram-facebook-or-tiktok) endpoint. The post level analytics data returned includes the share link of the video, description, and counts of likes, comments, shares, and views. this feature is unique to Ayrshare.

```json
{
    "createTime": 1641604664,
    "shareUrl": "https://www.tiktok.com/@funtime/video/705063834032649?utm_campaign=tt4d_open_api&utm_source=wawnhyitaos7o7",
    "videoDescription": "Blah",
    "duration": 4,
    "id": "7050638340353264943",
    "title": "Blah",
    "embedLink": "https://www.tiktok.com/embed/v2/7050638340353264943",
    "likeCount": 0,
    "commentCount": 0,
    "shareCount": 0,
    "viewCount": 0
},
{
    "createTime": 1641603132,
    "shareUrl": "https://www.tiktok.com/@funtime/video/7050631761763536?utm_campaign=tt4d_open_api&utm_source=wawnhyictaos7o7",
    "videoDescription": "Yes",
    "duration": 4,
    "id": "705063176176353",
    "title": "Yes",
    "embedLink": "https://www.tiktok.com/embed/v2/705063176176353",
    "likeCount": 0,
    "commentCount": 0,
    "shareCount": 0,
    "viewCount": 0
}
```

This insights data can be integrated into your app or platform for your users.

## Start Sharing to TikTok
If you want to find out more about how to connect your TikTok account and post videos, check out our [guide](https://docs.ayrshare.com/gui-dashboard/connect-social-media/tiktok-linking). Also, we have several [integration packages](https://docs.ayrshare.com/packages/overview) to make things easier.

And of course, let us know if you have any questions.