Video Downloader Guides
How to Automate TikTok Downloads With a Discord Bot
Want to automate TikTok downloads with a Discord bot so anyone can drop a link and get a watermark-free video right in the channel? This guide walks you through building one with Python and the Tiklocker API, from bot setup and the full code to security hardening and multi-platform upgrades. Post a link, get the video, never leave Discord.
If your server shares a lot of TikToks, the manual dance gets old fast: copy link, open a downloader, save the file, re-upload it to chat. A Discord bot collapses all of that into a single action. Someone pastes a TikTok URL, the bot detects it, fetches a clean copy through the Tiklocker API, and drops the finished video straight into the channel. Here's how to build it.
What We're Building
A Discord bot that does three things on autopilot:
- Watches every message for TikTok links.
- Downloads each video through the Tiklocker API, watermark removed.
- Posts the finished file back into the same channel.
No browser, no copy-pasting, no watermark. It runs quietly in the background and reacts whenever a link shows up.
Prerequisites
Before you write any code, gather these:
- Python 3.8+ installed on the machine that will host the bot.
- A Discord bot token from the Discord developer portal.
- A Tiklocker API key. Register free and grab a key from your account.
- The libraries: run
pip install discord.py requests.
That's it. You can run this on a spare laptop, a Raspberry Pi, or any cheap cloud VM.
The Bot Code
This single file handles detection, downloading, and posting. Drop it into tiktok_bot.py.
import discord
import requests
import re
import os
DISCORD_TOKEN = os.environ["DISCORD_TOKEN"]
TIKLOCKER_API_KEY = os.environ["TIKLOCKER_API_KEY"]
API_URL = "https://tiklocker.com/api/batch-download"
# Regex to match TikTok URLs
TIKTOK_PATTERN = re.compile(
r'https?://(?:www\.|vm\.)?tiktok\.com/[^\s]+'
)
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
@client.event
async def on_ready():
print(f"Bot is online as {client.user}")
@client.event
async def on_message(message):
if message.author == client.user:
return
urls = TIKTOK_PATTERN.findall(message.content)
if not urls:
return
for url in urls:
await message.channel.send("Downloading video...")
try:
headers = {
"Authorization": f"Bearer {TIKLOCKER_API_KEY}",
"Content-Type": "application/json"
}
response = requests.post(
API_URL,
json={"urls": [url]},
headers=headers
)
if response.status_code == 200:
filename = "tiktok_video.mp4"
with open(filename, "wb") as f:
f.write(response.content)
await message.channel.send(file=discord.File(filename))
os.remove(filename)
else:
await message.channel.send(
"Couldn't download that one. "
"Make sure the link is valid and public."
)
except Exception as e:
await message.channel.send(f"Error: {str(e)}")
client.run(DISCORD_TOKEN)
Notice the code reads both secrets from environment variables rather than hardcoding them. That's deliberate, and it's covered in the security section below.
Setup Instructions
Follow these steps in order and the bot will be live in a few minutes.
Create a Discord application.
- Open the developer portal and create a new application.
- Under the "Bot" tab, create a bot and enable "Message Content Intent" in Privileged Gateway Intents.
- Copy the bot token.
Invite the bot to your server.
- Open the OAuth2 URL Generator.
- Select the
botscope plus the "Send Messages" and "Attach Files" permissions. - Open the generated URL and add the bot to your server.
Set your secrets and run it.
export DISCORD_TOKEN="your_discord_bot_token" export TIKLOCKER_API_KEY="your_tiklocker_api_key" python tiktok_bot.pyTest it. Paste any public TikTok link into a channel the bot can see. Within seconds it should reply with the downloaded video.
Security Tips
A bot with an API key is a small piece of production software, so treat it like one.
- Never commit your tokens. Keep them in environment variables (as the code above already does) and add your
.envfile to.gitignore. - Limit the bot to specific channels if you don't want it reacting to links across the whole server. Check
message.channel.idagainst an allowlist before processing. - Add rate limiting so a flood of links can't burn through your API credits. A simple per-user cooldown goes a long way.
- Run with least privilege. Grant only "Send Messages" and "Attach Files." The bot never needs admin.
Advanced Features to Add
Once the basics work, the bot is easy to extend.
- Support more platforms. Widen the regex to catch Instagram, YouTube, Twitter/X, Reddit, and Facebook links, then route them through the same API. See the Instagram video downloader and YouTube video downloader for the platforms you can add.
- Add a queue. Process downloads through an async queue so several links resolve without one slow video blocking the rest.
- Track credits. Add a
/creditsslash command that reports how many API credits remain so the channel knows when to top up. - Auto-clean the chat. Delete the original link message after posting the video for a tidier channel.
Frequently Asked Questions
How many credits does each download use?
Each video download uses one API credit, whether it was triggered by the bot or done manually. Multi-link messages use one credit per link.
Can the bot handle multiple links in one message?
Yes. The loop in the code finds every TikTok URL in a single message and downloads each one in turn. Add the optional queue if you expect heavy bursts.
What's Discord's file size limit?
Standard servers allow uploads up to roughly 8 MB, with higher limits on boosted servers. Most TikTok clips land well under that, but for long videos you may want to post a link instead of the raw file.
Do I need the user to log into Tiklocker for this to work?
No. The bot authenticates with your single API key, so individual users never log in. They just paste links. For occasional manual saves outside Discord, anyone can also use the TikTok video downloader with no account at all.
Ready to build it? Grab your free key from the Tiklocker homepage, wire up the bot above, and your server will be downloading clean TikToks on autopilot.
Ready to download?
Grab any TikTok, Instagram, YouTube, or X video in seconds — no watermark, no app.
Open the downloader →