Skip to content

Music

Music is another very important part of the TikTok ecosystem. The EnsembleData API has several endpoints that allow you to interact with music data. Let's start by looking at how we can find music using a keyword.

Music Search API

API Documentation

Aside from supplying a keyword, you'll need to specify how you want the results to be filtered and sorted. Let's take a look at the options:

Sorting

  • 0: sort by relevance (best keyword match, according to TikTok)
  • 1: sort by most used music
  • 2: sort by most recent music
  • 3: sort by shortest music
  • 4: sort by longest music

Filtering

  • 0: No filter applied
  • 1: only search music
  • 2: only search music creators
import requests

result = requests.get(
    "https://ensembledata.com/apis/tt/music/info",
    params={
        "name": "classical",
        "sorting": "0",
        "filter_by": "0",
        "cursor": 0,
        "token": "API_TOKEN",
    }
).json()["data"]

music = result["music"]
next_cursor = result.get("nextCursor", None)

# Show the first music result
print("Title:", music[0]["title"])
print("Author:", music[0]["author"])
print("Music ID:", music[0]["mid"])
from ensembledata.api import EDClient

client = EDClient("API_TOKEN")
result = client.tiktok.music_search(
    keyword="classical",
    sorting="0",
    filter_by="0",
)

music = result.data["music"]
next_cursor = result.data.get("nextCursor")

# Show the first music result
print("Title:", music[0]["title"])
print("Author:", music[0]["author"])
print("Music ID:", music[0]["mid"])
import { EDClient } from "ensembledata";

const client = new EDClient({ token: "API_TOKEN" });
let result = await client.tiktok.music_search({
    keyword: "classical",
    sorting: "0",
    filter_by: "0",
});

const music = result.data.music;
const nextCursor = result.data.nextCursor;

// Show the first music result
console.log("Title:", music[0].title);
console.log("Author:", music[0].author);
console.log("Music ID:", music[0].mid);

Fetch more results

To search for more music, simply add the cursor parameter to the request, using the nextCursor value you got from the previous request.

What is a cursor?

if next_cursor is None:
    print("There are no more results")
else:
    result = requests.get(
        "https://ensembledata.com/apis/tt/music/info",
        params={
            "name": "classical",
            "sorting": "0",
            "filter_by": "0",
            "cursor": next_cursor,
            "token": "API_TOKEN",
        }
    ).json()["data"]
if next_cursor is None:
    print("There are no more results")
else:
    result = client.tiktok.music_search(
        keyword="classical",
        sorting="0",
        filter_by="0",
        cursor=next_cursor,
    )
if (nextCursor == undefined) {
    console.log("There are no more results");
} else {
    result = await client.tiktok.music_search({
        keyword: "classical",
        sorting: "0",
        filter_by: "0",
        cursor: nextCursor,
    });
}

Music Posts API

API Documentation

Once you've got your hands on a music ID, you can use it to fetch posts that use that music.

result = requests.get(
    "https://ensembledata.com/apis/tt/music/posts",
    params={
        "music_id": "7063948643480488709",
        "token": "API_TOKEN",
    }
).json()["data"]

posts = result["aweme_list"]
next_cursor = result.get("nextCursor", None)
result = client.tiktok.music_posts(
    music_id="7063948643480488709",
)

posts = result.data["aweme_list"]
next_cursor = result.data.get("nextCursor")
let result = await client.tiktok.music_posts({
    music_id: "7063948643480488709",
});

const posts = result.data.aweme_list;
const nextCursor = result.data.nextCursor;

To fetch more posts, use the cursor parameter.

if next_cursor is None:
    print("There are no more results")
else:
    result = requests.get(
        "https://ensembledata.com/apis/tt/music/posts",
        params={
            "music_id": "7063948643480488709",
            "cursor": next_cursor,
            "token": "API_TOKEN",
        }
    ).json()["data"]
if next_cursor is None:
    print("There are no more results")
else:
    result = client.tiktok.music_posts(
        music_id="7063948643480488709",
        cursor=next_cursor,
    )
if (nextCursor == undefined) {
    console.log("There are no more results");
} else {
    result = await client.tiktok.music_posts({
        music_id: "7063948643480488709",
        cursor: nextCursor,
    });
}

Music Details API

API Documentation

If you've got a music ID and want to get more information about it, you can use the Music Details endpoint.

result = requests.get(
    "https://ensembledata.com/apis/tt/music/details",
    params={
        "id": "7063948643480488709",
        "token": "API_TOKEN",
    }
).json()["data"]
result = client.tiktok.music_details(
    music_id="7063948643480488709",
)

# The number of videos which use this music item
print("Usage count", result.data["user_count"])
const result = await client.tiktok.music_details({
    music_id: "7063948643480488709",
});

// The number of videos which use this music item
console.log("Usage count", result.data.user_count);

In the response payload you can find links to the music itself, artist/author information, duration etc. We recommend perusing the response payload to see what else you can get from this endpoint.