# TikTok Music API

> Get a TikTok sound by id or link (title, artist, duration, cover, how many posts use it) and the posts that use it, as JSON. 1 credit per call.

**Get a TikTok sound** (a song or an original audio) and **the posts that use it**.

```
GET https://api.handlehaul.com/v1/tiktok/music/{music}
GET https://api.handlehaul.com/v1/tiktok/music/{music}/posts
```

- `GET /v1/tiktok/music/{music}`: title, artist, duration, cover, preview URL, number of posts using it
- `GET /v1/tiktok/music/{music}/posts`: posts that use the sound (up to 30 a page)

**Cost:** 1 credit per successful call (free from cache). An unknown sound answers `404` and is free.

## Parameters

| Parameter | In | Description |
|---|---|---|
| `music` | path | Numeric sound id (`music.id` on any post) or a URL-encoded `tiktok.com/music/<name>-<id>` link. |
| `cursor` | query | posts: `pagination.next_cursor` from the previous page. |
| `fresh` | query | `true` skips the cache. |
| `raw` | query | `true` returns TikTok's own JSON in `data`. |

## Response

```json
{
  "data": {
    "id": "7200000000000000001",
    "title": "Sample track",
    "artist": "Sample Artist",
    "is_original": false,
    "duration_seconds": 30,
    "cover_url": "https://p16-sign.tiktokcdn-us.com/…/music/7200000000000000001~c5_720x720.jpeg",
    "play_url": "https://v16-webapp-prime.tiktok.com/…/music/7200000000000000001.mp3",
    "post_count": 1200,
    "url": "https://www.tiktok.com/music/sample-track-7200000000000000001"
  },
  "meta": {
    "request_id": "req_…",
    "cached": false,
    "cache_age_seconds": 0,
    "credits_charged": 1,
    "credits_remaining": 26943
  }
}
```

## Examples

```bash
curl "https://api.handlehaul.com/v1/tiktok/music/7200000000000000001/posts" -H "X-Api-Key: $HANDLEHAUL_API_KEY"
```

```python
import os, requests
r = requests.get("https://api.handlehaul.com/v1/tiktok/music/7200000000000000001/posts",
                 headers={"X-Api-Key": os.environ["HANDLEHAUL_API_KEY"]}, timeout=60)
r.raise_for_status()
for p in r.json()["data"]:
    print(p["author"]["handle"], p["counts"]["views"])
```

```javascript
const res = await fetch("https://api.handlehaul.com/v1/tiktok/music/7200000000000000001/posts", {
  headers: { "X-Api-Key": process.env.HANDLEHAUL_API_KEY },
});
const { data, meta } = await res.json();
```

```php
$ch = curl_init("https://api.handlehaul.com/v1/tiktok/music/7200000000000000001/posts");
curl_setopt_array($ch, [
    CURLOPT_HTTPHEADER => ["X-Api-Key: " . getenv("HANDLEHAUL_API_KEY")],
    CURLOPT_RETURNTRANSFER => true,
]);
$body = json_decode(curl_exec($ch), true);
print_r($body["data"]);
```

```go
req, _ := http.NewRequest("GET", "https://api.handlehaul.com/v1/tiktok/music/7200000000000000001/posts", nil)
req.Header.Set("X-Api-Key", os.Getenv("HANDLEHAUL_API_KEY"))
res, err := http.DefaultClient.Do(req)
if err != nil {
    log.Fatal(err)
}
defer res.Body.Close()
var body map[string]any
json.NewDecoder(res.Body).Decode(&body)
fmt.Println(body["data"])
```

## FAQ

### How many videos use a TikTok sound?
`GET /v1/tiktok/music/{id}` returns it in `data.post_count`.
