> ## Documentation Index
> Fetch the complete documentation index at: https://docs.tikfly.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Working with Cursor in TikTok API for Pagination

> Learn how TikTok's cursor-based pagination works and how to implement it using Python

## Working with Cursor with TikTok API for Pagination

TikTok's API uses a **cursor-based pagination system** to handle large datasets.\
Each response includes a `cursor` value that indicates where to continue retrieving the next set of data.

<Card title="What is a Tiktok Cursor?" icon="circle-info" horizontal>
  A Tiktok Cursor is a token returned by TikTok API that points to the next page of results.
</Card>

***

### 1. Initial Request

Make an initial API request to retrieve the first page of data. The `cursor` value will be set to `0` by default on the first request. The response will typically include a list of results and a cursor field for the next page.

#### Example API request to [Get User Posts](https://docs.tikfly.io/api-reference/user/get-user-posts)

```python theme={null}
import requests

url = "https://tiktok-api23.p.rapidapi.com/api/user/posts"

params = {
  "secUid":"MS4wLjABAAAAqB08cUbXaDWqbD6MCga2RbGTuhfO2EsHayBYx08NDrN7IE3jQuRDNNN6YwyfH6_6",
  "count":"35",
  "cursor":"0"
}

headers = {
	"x-rapidapi-key": "YOUR-X-RAPIDAPI-KEY",
	"x-rapidapi-host": "tiktok-api23.p.rapidapi.com"
}

response = requests.get(url, headers=headers, params=params)

print(response.json()) 
```

> TikTok will return
>
> * `itemList`: list of posts
> * `cursor`: pointer for the next request
> * `hasMore`: whether more pages exist

Example response

```json theme={null}
{
  "data": {
    "cursor": "1729260489000",
    "extra": {
      "fatal_item_ids": [],
      "logid": "2025021310472335DA5E9DB25F2C2A587A",
      "now": 1739414843000
    },
    "hasMore": true,
    "itemList": [...],
    "log_pb": {
      "impr_id": "2025021310472335DA5E9DB25F2C2A587A"
    },
    "statusCode": 0,
    "status_code": 0,
    "status_msg": ""
  }
}
```

### 2. Paginate with Cursor

To retrieve the next page of results, make another API request, but this time include the `cursor` parameter from the previous response.

```python theme={null}
params = {
  "secUid":"MS4wLjABAAAAqB08cUbXaDWqbD6MCga2RbGTuhfO2EsHayBYx08NDrN7IE3jQuRDNNN6YwyfH6_6",
  "count":"35",
  "cursor":"1729260489000"
}

```

### 3. Repeat

Continue making requests using the new cursor value from each response to paginate through all available data. Each response will provide a new cursor value for the subsequent request.

### 4. End of Results

When the response no longer contains a cursor field or it is `null/empty/-1`, that means you've reached the end of the data and there are no more results to fetch.

### Full Working Python Example

```python theme={null}

import requests

X_RAPIDAPI_KEY = 'YOUR-X-RAPIDAPI-KEY'
USER_SEC_UID = 'MS4wLjABAAAAqB08cUbXaDWqbD6MCga2RbGTuhfO2EsHayBYx08NDrN7IE3jQuRDNNN6YwyfH6_6'

def get_tiktok_user_posts(user_sec_uid, count=35, cursor='0'):
  url = "https://tiktok-api23.p.rapidapi.com/api/user/posts"

  params = {
    "secUid": user_sec_uid,
    "count": count,
    "cursor": cursor
  }

  headers = {
    "x-rapidapi-key": X_RAPIDAPI_KEY,
    "x-rapidapi-host": "tiktok-api23.p.rapidapi.com"
  }

  response = requests.get(url, headers=headers, params=params)
  data = response.json()

  posts = data.get('data', {}).get('itemList', [])
  cursor = data.get('data', {}).get('cursor', None)

  if cursor == '-1':
    cursor = None
  
  return posts, cursor


def fetch_all_user_posts(user_sec_uid, count=35):
  all_posts = []
  cursor = '0'  # Initial cursor

  while cursor is not None:
    posts, cursor = get_tiktok_user_posts(user_sec_uid, count, cursor)
    all_posts.extend(posts)

  return all_posts


# Example usage
posts = fetch_all_user_posts(USER_SEC_UID)
print(f"Fetched {len(posts)} posts.")

```

<Note>
  **Need help?** Check our <a href="/support">support page</a> or contact our team.
</Note>
