How to Scrape TikTok Tops with R

Extract top data from TikTok

📊 Using R

Overview

Learn how to scrape TikTok tops using R. This comprehensive guide will walk you through the entire process, from setup to implementation.

What You'll Learn

  • • Setting up your development environment
  • • Installing the required HTTP client
  • • Authenticating with the ScrapeCreators API
  • • Making requests to TikTok
  • • Handling responses and errors
  • • Best practices for production use

What You'll Get

  • • Access to tops data
  • • JSON formatted responses
  • • Real-time data access
  • • Scalable solution
  • • Error handling patterns
  • • Performance optimization tips

Prerequisites

1. API Key

First, you'll need a ScrapeCreators API key to authenticate your requests.

Sign up at app.scrapecreators.com to get your free API key with 100 requests.

2. Development Environment

Make sure you have the following installed:

  • R and its dependencies
  • • A code editor (VS Code, Sublime, etc.)
  • • Basic understanding of API requests
  • • Command line interface access

Step 1: Install HTTP Client

httr is a user-friendly HTTP client for R

R
install.packages("httr")

Step 2: API Implementation

Now let's make a request to the TikTok API using R. Replace YOUR_API_KEY with your actual API key.

R
library(httr)
library(jsonlite)

API_KEY <- "YOUR_API_KEY"
BASE_URL <- "https://api.scrapecreators.com"
ENDPOINT_PATH <- "/v1/tiktok/search/top"

scrape <- function() {
# Build query parameters
params <- list(
"query" = "funny",
"publish_time" = "all-time",
"sort_by" = "relevance",
"region" = "example_value",
"cursor" = "example_value"
)
tryCatch({
response <- GET(
url = paste0(BASE_URL, ENDPOINT_PATH),
add_headers(
"x-api-key" = API_KEY,
"Content-Type" = "application/json"
),
query = params
)
if (status_code(response) == 200) {
data <- fromJSON(rawToChar(response$content))
cat("Response:
")
print(data)
return(data)
} else {
stop(paste("HTTP", status_code(response), ":", rawToChar(response$content)))
}

Step 3: Testing Your Code

API Parameters

This endpoint accepts the following parameters:

queryRequired(string)

Keyword to search for

Example: funny

publish_timeOptional(select)

Time Frame TikTok was posted

Example: all-time

sort_byOptional(select)

Sort by

Example: relevance

regionOptional(string)

Note, this doesn't filter the tiktoks only in a specfic region, it puts the proxy there. Use it in case you want to scrape posts only available for some country. Use 2 letter country codes like US, GB, FR, etc

cursorOptional(number)

Cursor to get more videos. Get 'cursor' from previous response.

0

Run Your Code

Execute your script to test the API connection. You should see a JSON response with TikTok tops data.

✅ Success: You should receive a structured JSON response containing the requested data.

Expected Response

Here's an example of the JSON response you'll receive:

Sample Response
{
"success": true,
"items": [
{
"id": "7528150371680767263",
"desc": "Last two brain cells at 3 am 💀 (IB: @Marearo) #dance #trend #fun ",
"content_type": "video",
"create_time": "2025-07-17T20:28:28.000Z",
"desc_language": "en",
"region": "US",
"is_top": 0,
"statistics": {
"aweme_id": "7528150371680767263",
"collect_count": 101,
"comment_count": 83,
"digg_count": 1895,
"download_count": 3,
"forward_count": 0,
"lose_comment_count": 0,
"lose_count": 0,
"play_count": 17897,
"repost_count": 0,
"share_count": 27,
"whatsapp_share_count": 1
},
"video": {
"CoverTsp": 11.37871674085026,
"ai_dynamic_cover": {
"uri": "tos-useast8-p-0068-tx2/ogBjDEAfE9QAhZIOIE5AtqFAveLqXIfViuAqzq",
"url_list": [
"https://p16-pu-sign-useast8.tiktokcdn-us.com/tos-useast8-p-0068-tx2/ogBjDEAfE9QAhZIOIE5AtqFAveLqXIfViuAqzq~tplv-tiktokx-origin.image?dr=8606&refresh_token=51397adc&x-expires=1752876000&x-signature=RXrltd1boDTaIdiMMiMYHyAli5I%3D&t=bacd0480&ps=4f5296ae&shp=d05b14bd&shcp=0e6dc556&idc=useast5&biz_tag=tt_video&s=SEARCH&sc=dynamic_cover",
"https://p19-pu-sign-useast8.tiktokcdn-us.com/tos-useast8-p-0068-tx2/ogBjDEAfE9QAhZIOIE5AtqFAveLqXIfViuAqzq~tplv-tiktokx-origin.image?dr=8606&refresh_token=11f11f2b&x-expires=1752876000&x-signature=IeFi%2Fxm3eCB0AUQq2YhFyqijf%2Fg%3D&t=bacd0480&ps=4f5296ae&shp=d05b14bd&shcp=0e6dc556&idc=useast5&biz_tag=tt_video&s=SEARCH&sc=dynamic_cover",
"https://p16-pu-sign-useast8.tiktokcdn-us.com/tos-useast8-p-0068-tx2/ogBjDEAfE9QAhZIOIE5AtqFAveLqXIfViuAqzq~tplv-tiktokx-origin.jpeg?dr=8606&refresh_token=8a6d2055&x-expires=1752876000&x-signature=NRqus7CmX9CERlIA3A%2BeU4U2Tpc%3D&t=bacd0480&ps=4f5296ae&shp=d05b14bd&shcp=0e6dc556&idc=useast5&biz_tag=tt_video&s=SEARCH&sc=dynamic_cover"
],
"url_prefix": null
},

Verify Response Structure

Check that your response includes the expected fields:

  • success(boolean)
  • items(object)
  • cursor(number)

Best Practices

1

Error Handling

Implement comprehensive error handling and retry logic for failed requests. Log errors properly for debugging.

2

Caching

Cache responses when possible to reduce API calls and improve performance. Consider data freshness requirements.

3

Security

Never expose your API key in client-side code. Use environment variables and secure key management practices.

Performance Tips

Batch Requests

When scraping multiple tops, consider batching requests to maximize throughput while staying within rate limits.

Async Processing

Use asynchronous processing in R to handle multiple requests concurrently and improve overall performance.

Common Use Cases

Market Research

Analyze TikTok tops to understand market trends, competitor analysis, and audience insights.

Content Analytics

Track performance metrics, engagement rates, and content trends across TikTok tops.

Lead Generation

Identify potential customers and business opportunities throughTikTok data analysis.

Troubleshooting

Common Errors

401 Unauthorized

Check your API key is correct and properly formatted in the x-api-key header.

402 Payment Required

You ran out of credits and need to buy more.

404 Not Found

The resource might not exist or be private.

500 Server Error

Temporary server issue. Implement retry logic with exponential backoff.

Frequently Asked Questions

How much does it cost to scrape TikTok tops?

ScrapeCreators offers 100 free API calls to get started. After that, pricing starts at $10 for 5k requests with volume discounts available.

Is it legal to scrape TikTok data?

Scraping publicly available data is fair game, and we only collect public data. So anything that you can see in an incognito browser is what we collect.

How fast can I scrape TikTok tops?

There is no rate limit! So you can scrape as fast as you want!

What data format does the API return?

All API responses are returned in JSON format, making it easy to integrate with any programming language or application.

Can I use this with other R frameworks?

Yes! This tutorial focuses on core R HTTP concepts that work with any framework. The API calls remain the same regardless of your specific R setup.

How do I handle large datasets?

For large datasets, implement pagination, use streaming responses where available, and consider storing data in a database for efficient querying.

Related Tutorials

Ready to Start Scraping?

Get started with 100 free API calls. No credit card required.