Tutorial

How to Analyze TikTok Comment Sentiment with API + LLM

Extract TikTok comments via API and run LLM-based sentiment analysis. Python example using Scavio for extraction and OpenAI for analysis.

Analyze TikTok comment sentiment by extracting comments via Scavio API ($0.005/page) and classifying them with an LLM ($0.01-0.05 per 50 comments). Total cost per video analysis: $0.02-0.10 depending on comment volume.

Prerequisites

  • Scavio API key
  • OpenAI API key (or any LLM)
  • Python 3.8+ with requests and openai libraries

Walkthrough

Step 1: Extract comments from a video

Pull comments using the video/comments endpoint.

Python
import requests, os

SCAVIO_HEADERS = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
                  'Content-Type': 'application/json'}

def get_comments(video_id, pages=3):
    comments = []
    cursor = 0
    for _ in range(pages):
        data = requests.post('https://api.scavio.dev/api/v1/tiktok/video/comments',
            headers=SCAVIO_HEADERS,
            json={'aweme_id': video_id, 'count': 20, 'cursor': cursor}).json()['data']
        comments.extend(data.get('comments', []))
        if not data.get('has_more'):
            break
        cursor = data.get('cursor', cursor + 20)
    return comments

comments = get_comments('7123456789')

Step 2: Classify sentiment with LLM

Pass comment texts to an LLM for sentiment classification.

Python
from openai import OpenAI

client = OpenAI()
texts = [c['text'] for c in comments[:50]]

resp = client.chat.completions.create(
    model='gpt-4o-mini',
    messages=[{'role': 'user',
        'content': f'Classify each comment as positive, negative, or neutral. '
                   f'Return JSON array of objects with text and sentiment.\n'
                   f'Comments: {texts}'}],
    response_format={'type': 'json_object'})

results = resp.choices[0].message.content
print(results)

Python Example

Python
import requests, os, json
from openai import OpenAI

SCAVIO_H = {'Authorization': f'Bearer {os.environ["SCAVIO_API_KEY"]}',
            'Content-Type': 'application/json'}
client = OpenAI()

def analyze_sentiment(video_id):
    # Extract comments
    comments = []
    cursor = 0
    for _ in range(3):
        data = requests.post('https://api.scavio.dev/api/v1/tiktok/video/comments',
            headers=SCAVIO_H,
            json={'aweme_id': video_id, 'count': 20, 'cursor': cursor}).json()['data']
        comments.extend(data.get('comments', []))
        if not data.get('has_more'): break
        cursor = data.get('cursor', cursor + 20)
    # Classify with LLM
    texts = [c['text'][:100] for c in comments[:50]]
    resp = client.chat.completions.create(
        model='gpt-4o-mini',
        messages=[{'role': 'user',
            'content': f'Classify each as positive/negative/neutral. Return JSON with counts.\n{texts}'}],
        response_format={'type': 'json_object'})
    return json.loads(resp.choices[0].message.content)

result = analyze_sentiment('7123456789')
print(result)

JavaScript Example

JavaScript
const SH = {'Authorization': `Bearer ${process.env.SCAVIO_API_KEY}`, 'Content-Type': 'application/json'};
async function analyzeSentiment(videoId) {
  const comments = [];
  let cursor = 0;
  for (let i = 0; i < 3; i++) {
    const r = await fetch('https://api.scavio.dev/api/v1/tiktok/video/comments', {
      method: 'POST', headers: SH,
      body: JSON.stringify({aweme_id: videoId, count: 20, cursor})
    }).then(r => r.json());
    comments.push(...(r.data.comments || []));
    if (!r.data.has_more) break;
    cursor = r.data.cursor || cursor + 20;
  }
  const texts = comments.slice(0, 50).map(c => c.text.slice(0, 100));
  console.log(`Extracted ${comments.length} comments, classifying ${texts.length}`);
  return texts;
}
analyzeSentiment('7123456789');

Expected Output

JSON
Sentiment classification of TikTok comments: count of positive, negative, and neutral comments with key themes identified.

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Scavio API key. OpenAI API key (or any LLM). Python 3.8+ with requests and openai libraries. A Scavio API key gives you 250 free credits per month.

Yes. The free tier includes 250 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

Extract TikTok comments via API and run LLM-based sentiment analysis. Python example using Scavio for extraction and OpenAI for analysis.