TEST SITE: not indexed, not the live website. Placeholder button URLs in use: signup, login, dashboard, playground, api_base.

How to Get a TikTok Transcript in Python

requests, errors, subtitles, batches.

  • How-to guide
  • Published
  • Last verified
  • 3 min read
  • By The team
  • Get API key
Overview

This tutorial gets a TikTok transcript in Python with the requests library, handles typed errors, and saves subtitle files. Every script was run against a local mock.

  • requests, a timeout, typed errors.
  • Save SRT and VTT with a check.
  • Many videos: workers and retries.
  • Runs against a mock; contract to be confirmed.

Python needs only requests and your key. The steps: one call, error handling, subtitle files, then many videos. Related: the Python guide and the workflows.

What do you need before you start?

Python 3.9 or later, pip install requests, an API key in TTA_API_KEY, and a public TikTok video link. Set TTA_API_BASE to your API host.

How do you make the first call?

quickstart.py
import os, requests

API = os.environ.get("TTA_API_BASE", "https://api.example.com")
r = requests.get(
    f"{API}/api/v1/tiktok/transcript",
    params={"url": "https://www.tiktok.com/@gardenclub/video/7000000000000000001", "lang": "en"},
    headers={"Authorization": f"Bearer {os.environ['TTA_API_KEY']}"},
    timeout=60,
)
data = r.json()
if not data["success"]:
    raise SystemExit(f"{data['error']['code']}: {data['error']['message']}")
print(data["transcript_source"], data["language_code"], len(data["segments"]), "segments")
print(data["transcript"])
print("credits left:", data["credits_remaining"])
Captured output from the local mock (not the real API)
captions en 5 segments
Okay, three tips for a small garden. Tip one: measure the sun before you buy seeds. Tip two: raised beds drain better. Tip three: water in the morning. Follow for the full guide.
credits left: 14999

How do you handle errors?

Check success first. On failure read error.code. A private video looks like this:

Captured output from the local mock (not the real API)
error private_video: This video is private.

How do you save SRT and VTT files?

Request format=srt and format=vtt and write the text. The subtitle workflow has the full script.

Captured output from the local mock (not the real API)
wrote subs/7000000000000000001.en.srt: 5 cues, header ok=True
wrote subs/7000000000000000001.en.vtt: 5 cues, header ok=True

How do you fetch many videos?

Use workers, retries and a results file: the batch workflow.

How do you keep the key and base URL out of code?

Read both from environment variables, as the samples do: TTA_API_KEY and TTA_API_BASE. In production use your host’s secret store. In tests point TTA_API_BASE at the local mock so no credit is spent.

How do you test without spending credits?

The repository for this site ships a mock server that follows the assumed contract and has fixture videos for every case: captions, no captions, private, removed, rate limited and a job. Run your code against it, then switch the base URL to the real API and compare. Where the two differ, the real API is right and the docs here need fixing.

What should you log?

Log the video ID, the HTTP status, the error code and the request ID header if the API sends one (to be confirmed). Do not log the key. Those four fields answer most support questions.

What can go wrong?

  • No timeout set: always pass timeout.
  • Retrying errors that are not retryable wastes time.
  • Key committed to a repository: use environment variables.

Final word

Start with ten lines, add retries when you scale.

Frequently asked questions

Do I need an SDK?

No. The API is plain HTTP and requests is enough.

How do I avoid blocking my code on long jobs?

Use the job flow and poll, rather than a very long timeout.

Does this work on a server?

Yes. Keep the key in an environment variable.

Where is the real endpoint?

Set TTA_API_BASE to your host. The path shown is illustrative and to be confirmed.

Get API keyDocs