# TikTok Scraper (`salmon_redai/tiktok-scraper`) Actor

Scrape TikTok search results, hashtags, and profiles using session cookies with network interception. Captures 100+ items per query via scroll pagination.

- **URL**: https://apify.com/salmon\_redai/tiktok-scraper.md
- **Developed by:** [Salmon\_RedAI](https://apify.com/salmon_redai) (community)
- **Stats:** 2 total users, 1 monthly users, 0.0% runs succeeded, 0 bookmarks
- **User rating**: No ratings yet

## Pricing

Pay per usage

This Actor is paid per platform usage. The Actor is free to use, and you only pay for the Apify platform usage, which gets cheaper the higher subscription plan you have.

Learn more: https://docs.apify.com/actors/running/actors-in-store.md#pay-per-usage

## What's an Apify Actor?

An Actor is a serverless cloud program that runs on the Apify platform. It has two run modes.
In Batch mode, an Actor accepts a well-defined JSON input, performs an action which can take anything from a few seconds to a few hours,
and optionally produces a well-defined JSON output, datasets with results, or files in key-value store.
In Standby mode, an Actor provides a web server which can be used as a website, API, or an MCP server.

Apify vocabulary and the platform model are defined once, in the agent quickstart at https://apify.com/agents.md.

## How to integrate an Actor?

If asked about integration, you help developers integrate Actors into their projects.
You adapt to their stack and deliver integrations that are safe, well-documented, and production-ready.

Do not guess an integration path. Every one of them is in the agent quickstart at https://apify.com/agents.md: the Apify MCP server, Agent Skills with the Apify CLI, the JavaScript and Python clients, the REST API, and the account-free path for an agent with no human to sign in. It also carries the rule on stating cost before the first paid run.

For examples already wired to this Actor's own input schema, see the [API](#api) section below.

Each client library has reference documentation the quickstart does not restate: [JavaScript/TypeScript](https://docs.apify.com/api/client/js/docs.md) (`npm install apify-client`) and [Python](https://docs.apify.com/api/client/python/docs.md) (`pip install apify-client`).

# README

## TikTok Scraper Actor

An Apify actor that scrapes TikTok data using session cookies and network interception. No DOM parsing required — data is captured directly from TikTok's internal API.

### Features

- **Network Interception**: Captures responses from TikTok's API endpoints without parsing HTML
- **Session Cookie Support**: Accepts cookies in multiple formats (raw header, JSON array, Netscape cookies.txt)
- **Multiple Modes**: Search, hashtag, and profile scraping
- **Efficient Resource Usage**: Blocks heavy resources (images, media, fonts) while keeping CSS for proper scroll behavior
- **Anti-Bot Handling**: Captcha detection, exponential backoff, and session rotation
- **Deduplication**: Automatic deduplication by item ID
- **Flexible Output**: Compat (Threads-style), native (full TikTok data), or both

### Input

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `mode` | string | `search` | Scraping mode: `search`, `hashtag`, or `profile` |
| `queries` | array | `[]` | List of search queries, hashtags (#), or usernames (@) |
| `maxItems` | integer | `200` | Maximum items per query |
| `sessionCookies` | string | `""` | Session cookies (raw header, JSON, or cookies.txt) |
| `cookiePool` | array | `[]` | Multiple cookie sets for rotation |
| `sortBy` | string | `relevance` | Sort order: `relevance` or `latest` |
| `publishedWithin` | string | `all` | Server-side time filter: `all`, `1d`, `7d`, `30d`, `90d`, `180d` |
| `dateFrom` | string | `""` | Keep posts published on/after this date (`YYYY-MM-DD` or ISO datetime). Client-side, all modes |
| `dateTo` | string | `""` | Keep posts published on/before this date (inclusive). Client-side, all modes |
| `includeComments` | boolean | `false` | Whether to scrape comments |
| `commentsPerPost` | integer | `20` | Max comments per video |
| `downloadMedia` | boolean | `false` | Download media to KV store |
| `outputSchema` | string | `compat` | Output format: `compat`, `native`, or `both` |
| `proxyConfiguration` | object | `{"useApifyProxy": false}` | Apify Proxy or custom external proxy configuration |

#### Cookie Format

Cookies can be provided in any of these formats:

1. **Raw Cookie header** (copy from browser DevTools):
   ```
   sessionid=abc123; sessionid_ss=abc123; sid_tt=xyz789; sid_guard=xyz789; msToken=...
   ```

2. **JSON array** (EditThisCookie/Playwright format):
   ```json
   [{"name": "sessionid", "value": "abc123", "domain": ".tiktok.com"}, ...]
   ```

3. **Netscape cookies.txt**:
   ```
   # Netscape HTTP Cookie File
   .tiktok.com	TRUE	/	TRUE	1234567890	sessionid	abc123
   ```

#### Required Cookies

At minimum, these cookies must be present:

- `sessionid`
- `sessionid_ss`
- `sid_tt`
- `sid_guard`

Additional cookies that improve results:

- `ttwid`, `msToken`, `tt-target-idc`, `uid_tt`

#### Proxy Configuration

Proxies can be configured in two ways:

1. **Via Actor Input (`proxyConfiguration`)**:
   - In Apify UI, configure under the **Proxy** section (select Apify Residential/Datacenter proxy or provide custom external proxy URLs).
   - In JSON input:
     ```json
     "proxyConfiguration": {
       "useApifyProxy": false,
       "proxyUrls": ["http://username:password@proxy-host.com:8000"]
     }
     ```

2. **Via Environment Variables**:
   - Set `CUSTOM_PROXY_URL`, `PROXY_URL`, or `APIFY_PROXY_URL` in Actor Settings or local `.env`:
     ```bash
     CUSTOM_PROXY_URL="http://username:password@proxy-host.com:8000"
     ```
   - Multiple proxy endpoints can be comma-separated or newline-separated. When multiple proxies are passed, the scraper deterministically pins each session to a proxy using the cookie hash.

### Output Schema

Each item in the dataset includes:

```json
{
  "post_id": "7123456789012345678",
  "shortcode": "7123456789012345678",
  "post_url": "https://www.tiktok.com/@username/video/7123456789012345678",
  "text": "Video caption text...",
  "timestamp": "2024-01-15T10:30:00.000Z",
  "user": {
    "user_id": "1234567890",
    "username": "username",
    "full_name": "Display Name",
    "profile_url": "https://www.tiktok.com/@username",
    "profile_pic_url": "https://p16-sign.tiktokcdn.com/...",
    "is_verified": true,
    "follower_count": 100000
  },
  "likes": 50000,
  "replies": 1200,
  "reposts": 500,
  "quotes": null,
  "reshares": 3000,
  "views": 500000,
  "images": [],
  "videos": [{"url": "https://v16-webapp.tiktok.com/...", "duration": 30}],
  "is_reply": false,
  "source": "api",
  "hashtags": [{"id": "123", "name": "fyp"}],
  "mentions": [],
  "music": {"id": "123", "title": "Original Sound", "author": "Creator", "is_original": true},
  "video_duration": 30,
  "video_width": 1080,
  "video_height": 1920,
  "is_ad": false,
  "region": "US",
  "engagement_rate": 10.5
}
```

### Usage

#### On Apify Platform

1. Create a new actor on [Apify Console](https://console.apify.com)
2. Set the build to use the Dockerfile
3. Configure input:
   ```json
   {
     "mode": "search",
     "queries": ["SamsungGalaxyS25Ultra"],
     "maxItems": 100,
     "sessionCookies": "sessionid=...; sessionid_ss=...; sid_tt=...; sid_guard=..."
   }
   ```

#### Locally

```bash
## Install dependencies
npm install

## Run with input
npm start -- --input '{"queries":["fyp"],"sessionCookies":"..."}'
```

#### With Apify CLI

```bash
apify login
apify create tiktok-scraper --template project_empty
## Copy files to the new project
cd tiktok-scraper
apify run
```

### Architecture

```
src/
  main.js              # Actor entry point, input validation, crawler setup
  cookies.js           # Cookie parsing/normalization/validation
  intercept.js         # Network response interception + endpoint routing
  paginate.js          # Scroll-based pagination with stall detection
  antibot.js           # Captcha detection, backoff, session rotation
  normalize/
    shared.js          # Shared video item normalization
    searchItem.js      # Search endpoint normalizers
    challengeItem.js   # Hashtag/challenge normalizer
    comment.js         # Comment normalizer
```

### How It Works

1. **Browser Launch**: Playwright launches a headless Chrome instance
2. **Cookie Injection**: Session cookies are injected into the browser context
3. **Resource Blocking**: Heavy resources (images, media, fonts) are blocked to reduce bandwidth
4. **Navigation**: The page navigates to TikTok search/hashtag/profile URL
5. **Network Interception**: Responses from TikTok API endpoints are intercepted
6. **Scroll Pagination**: The page scrolls to trigger infinite scroll, loading more results
7. **Normalization**: Raw API responses are normalized to the output schema
8. **Dataset Output**: Items are pushed to the Apify dataset

### Known Limitations

- **Search depth**: ~300-450 items max per query due to TikTok's internal limits
- **Media URLs**: Video URLs expire in ~2 hours; download during the run if needed
- **Personalization**: Results are personalized per account (logged-in vs anonymous differ)
- **Account risk**: Heavy scraping may rate-limit or ban the account; use throwaway accounts

### Cost Optimization

- Resource blocking reduces bandwidth by ~90%
- Network interception avoids DOM parsing overhead
- Session rotation distributes load across multiple accounts
- Configurable max items and pagination limits

### License

ISC

# Actor input Schema

## `mode` (type: `string`):

The type of scraping to perform. 'search' for keyword search, 'hashtag' for hashtag feeds, 'profile' for user profile feeds.

## `queries` (type: `array`):

List of search queries, hashtags (with #), or usernames (with @) to scrape.

## `maxItems` (type: `integer`):

Maximum number of items to scrape per query. Default is 200.

## `sessionCookies` (type: `string`):

TikTok session cookies in one of these formats: 1) Raw Cookie header string, 2) JSON array (EditThisCookie/Playwright format), 3) Netscape cookies.txt format. Required fields: sessionid, sessionid\_ss, sid\_tt, sid\_guard.

## `cookiePool` (type: `array`):

Multiple cookie sets for rotation. Useful for scaling and avoiding rate limits.

## `sortBy` (type: `string`):

Sort search results by relevance or latest posts.

## `publishedWithin` (type: `string`):

Server-side time filter for search mode: all, 1 day, 7 days, 30 days, 90 days, or 180 days.

## `dateFrom` (type: `string`):

Only keep posts published on or after this date. Accepts 'YYYY-MM-DD' or an ISO datetime string (e.g. '2025-01-01' or '2025-01-01T00:00:00Z'). Applied client-side to all modes; can be combined with dateTo.

## `dateTo` (type: `string`):

Only keep posts published on or before this date. Accepts 'YYYY-MM-DD' (inclusive, through end of day UTC) or an ISO datetime string. Applied client-side to all modes; can be combined with dateFrom.

## `language` (type: `string`):

ISO language code used to scope search results (sets the lang URL parameter and the browser locale). Default is 'id' (Indonesian).

## `includeComments` (type: `boolean`):

Whether to scrape comments for each video.

## `commentsPerPost` (type: `integer`):

Maximum number of comments to scrape per video.

## `downloadMedia` (type: `boolean`):

Whether to download media files to KV store (video URLs expire in ~2 hours).

## `outputSchema` (type: `string`):

'compat' matches the Threads-style schema, 'native' is full TikTok data, 'both' includes all fields.

## `proxyConfiguration` (type: `object`):

Proxy configuration for the actor. You can enable Apify Proxy (Residential recommended) or provide custom external proxy URLs.

## Actor input object example

```json
{
  "mode": "search",
  "queries": [
    "SamsungGalaxyS25Ultra"
  ],
  "maxItems": 200,
  "sessionCookies": "",
  "cookiePool": [],
  "sortBy": "relevance",
  "publishedWithin": "all",
  "dateFrom": "",
  "dateTo": "",
  "language": "id",
  "includeComments": false,
  "commentsPerPost": 20,
  "downloadMedia": false,
  "outputSchema": "compat",
  "proxyConfiguration": {
    "useApifyProxy": false
  }
}
```

# Actor output Schema

## `results` (type: `string`):

All posts scraped for the given queries. Each dataset item is a single TikTok post with post\_id, shortcode, post\_url, text, timestamp, user (id, username, full\_name, profile\_url, is\_verified, follower\_count), likes, replies, reposts, quotes, reshares, views, images, videos, is\_reply, and source. When includeComments is enabled, items may also include a comments array.

## `runMetadata` (type: `string`):

JSON record with scraped\_at (ISO timestamp), queries (list of scraped queries/search terms), mode (search, hashtag, or profile), and total\_items (number of items pushed to the dataset).

# API

You can run this Actor programmatically using our API. Below are code examples in JavaScript, Python, and CLI, as well as the OpenAPI specification and MCP server setup.

## JavaScript example

```javascript
import { ApifyClient } from 'apify-client';

// Initialize the ApifyClient with your Apify API token
// Replace the '<YOUR_API_TOKEN>' with your token
const client = new ApifyClient({
    token: '<YOUR_API_TOKEN>',
});

// Prepare Actor input
const input = {};

// Run the Actor and wait for it to finish
const run = await client.actor("salmon_redai/tiktok-scraper").call(input);

// Fetch and print Actor results from the run's dataset (if any)
console.log('Results from dataset');
console.log(`💾 Check your data here: https://console.apify.com/storage/datasets/${run.defaultDatasetId}`);
const { items } = await client.dataset(run.defaultDatasetId).listItems();
items.forEach((item) => {
    console.dir(item);
});

// 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/js/docs

```

## Python example

```python
from apify_client import ApifyClient

# Initialize the ApifyClient with your Apify API token
# Replace '<YOUR_API_TOKEN>' with your token.
client = ApifyClient("<YOUR_API_TOKEN>")

# Prepare the Actor input
run_input = {}

# Run the Actor and wait for it to finish
run = client.actor("salmon_redai/tiktok-scraper").call(run_input=run_input)

# Fetch and print Actor results from the run's dataset (if there are any)
print(f"💾 Check your data here: https://console.apify.com/storage/datasets/{run.default_dataset_id}")
for item in client.dataset(run.default_dataset_id).iterate_items():
    print(item)

# 📚 Want to learn more 📖? Go to → https://docs.apify.com/api/client/python/docs/quick-start

```

## CLI example

```bash
echo '{}' |
apify call salmon_redai/tiktok-scraper --silent --output-dataset

```

## MCP server setup

```json
{
    "mcpServers": {
        "apify": {
            "type": "http",
            "url": "https://mcp.apify.com/?tools=fetch-actor-details,salmon_redai/tiktok-scraper"
        }
    }
}
```

The hosted server signs you in with OAuth on first connect, so no API token belongs in this config. Clients without OAuth support can send an `Authorization: Bearer <APIFY_API_TOKEN>` header instead, using a token from API & Integrations in Apify Console (https://console.apify.com/settings/integrations).

## OpenAPI specification

Download the OpenAPI definition: https://api.apify.com/v2/actors/I8cHmGjAPeXGyTWSf/builds/RNrBxGPjfzDrUFcil/openapi.json
