tiktok-signature
v3.0.0
Published
Generate TikTok Signature
Readme
TikTok Signature Generator
A production-ready TikTok signature generator service that provides API signature generation for TikTok API requests. This service uses Playwright to generate valid signatures required for TikTok API authentication.
Features
- Production-Ready: Comprehensive error handling, logging, and monitoring
- High Performance: Optimized browser context reuse for faster signature generation
- REST API: Simple HTTP API for signature generation
- Docker Support: Easy deployment with Docker and Docker Compose
- Health Monitoring: Built-in health check and metrics endpoints
- Comprehensive Tests: Full test coverage with unit and integration tests
- TypeScript Support: Full ES modules support
Table of Contents
- Installation
- Quick Start
- API Documentation
- Configuration
- Development
- Testing
- Docker Deployment
- Production Deployment
- API Endpoints
- Examples
- Contributing
- License
Installation
npm install tiktok-signatureOr clone the repository:
git clone https://github.com/carcabot/tiktok-signature.git
cd tiktok-signature
npm installQuick Start
Start the Server
npm startThe server will start on port 8080 by default.
Generate a Signature
curl -X POST \
-H "Content-type: text/plain" \
-d 'https://m.tiktok.com/api/post/item_list/?aid=1988&count=30' \
http://localhost:8080/signatureAPI Documentation
POST /signature
Generate a signature for a TikTok API URL.
Request:
- Method:
POST - Content-Type:
text/plain - Body: TikTok API URL (string)
Response:
{
"status": "ok",
"data": {
"signature": "_02B4Z6wo00f01...",
"verify_fp": "verify_knvz9j2k_...",
"signed_url": "https://m.tiktok.com/api/...",
"x-tt-params": "1BLhm+0j/AG2Dlsz3v4u4w==",
"x-bogus": "1BLhm+0j/AG2Dlsz3v4u4w==",
"navigator": {
"deviceScaleFactor": 3,
"user_agent": "Mozilla/5.0...",
"browser_language": "en-US",
"browser_platform": "MacIntel",
"browser_name": "Mozilla",
"browser_version": "5.0..."
}
},
"metadata": {
"processingTime": 234,
"timestamp": "2024-01-01T00:00:00.000Z"
}
}GET /health
Health check endpoint for monitoring.
Response:
{
"status": "healthy",
"uptime": 3600,
"requestCount": 150,
"timestamp": "2024-01-01T00:00:00.000Z"
}GET /metrics
Get server metrics and performance data.
Response:
{
"requests": {
"total": 150,
"rate": 0.042
},
"uptime": {
"seconds": 3600,
"startTime": "2024-01-01T00:00:00.000Z"
},
"memory": {
"rss": 123456789,
"heapTotal": 123456789,
"heapUsed": 123456789
},
"timestamp": "2024-01-01T00:00:00.000Z"
}Configuration
Configuration can be set through environment variables:
| Variable | Description | Default |
|----------|-------------|---------|
| PORT | Server port | 8080 |
| LOG_LEVEL | Logging level (ERROR, WARN, INFO, DEBUG) | INFO |
| BROWSER_HEADLESS | Run browser in headless mode | true |
| DEFAULT_URL | Default TikTok page for initialization | https://www.tiktok.com/@rihanna?lang=en |
| USER_AGENT | Browser user agent string | Chrome user agent |
| CORS_ORIGIN | CORS allowed origin | * |
| CORS_HEADERS | CORS allowed headers | * |
| TIMEOUT | Server timeout in milliseconds | 3600000 |
Example with Environment Variables
PORT=3000 LOG_LEVEL=DEBUG npm startDevelopment
Project Structure
tiktok-signature/
├── src/
│ ├── config.js # Configuration management
│ ├── signer.js # Main signature generation logic
│ ├── server.js # HTTP server implementation
│ ├── validators.js # Input validation
│ └── logger.js # Logging utility
├── javascript/ # Browser scripts
│ ├── signer.js
│ ├── webmssdk.js
│ └── xbogus.js
├── examples/ # Usage examples
├── __tests__/ # Test files
├── index.js # Package entry point
└── listen.js # Server entry pointDevelopment Mode
npm run devTesting
Run All Tests
For unit tests only:
npm test -- --testPathIgnorePatterns="integration.test.js|server.test.js"For all tests (requires server running):
# Terminal 1: Start the server
npm start
# Terminal 2: Run all tests
npm testRun Tests with Coverage
npm run test:coverageRun Tests in Watch Mode
npm run test:watchTest Structure
__tests__/utils.test.js- Utility function tests__tests__/signer.test.js- Signer class unit tests__tests__/server.test.js- Server endpoint tests__tests__/integration.test.js- End-to-end integration tests
Docker Deployment
Build Docker Image
docker build -t tiktok-signature .Run with Docker
docker run -p 8080:8080 tiktok-signatureDocker Compose
docker-compose up -dDocker Compose Configuration
version: '3.8'
services:
tiktok-signature:
build: .
ports:
- "8080:8080"
environment:
- PORT=8080
- LOG_LEVEL=INFO
- BROWSER_HEADLESS=true
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health"]
interval: 30s
timeout: 10s
retries: 3Production Deployment
Performance Considerations
- Memory Management: The service automatically manages browser contexts to prevent memory leaks
- Concurrency: The server can handle multiple concurrent signature requests
- Health Monitoring: Use
/healthendpoint for load balancer health checks - Logging: Configure
LOG_LEVEL=WARNorLOG_LEVEL=ERRORin production - Process Management: Use PM2 or systemd for process management
PM2 Configuration
{
"name": "tiktok-signature",
"script": "./listen.js",
"instances": 1,
"exec_mode": "fork",
"env": {
"PORT": 8080,
"LOG_LEVEL": "WARN",
"NODE_ENV": "production"
},
"max_memory_restart": "1G",
"error_file": "./logs/error.log",
"out_file": "./logs/out.log",
"log_date_format": "YYYY-MM-DD HH:mm:ss Z"
}Nginx Reverse Proxy
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Examples
The /examples directory contains comprehensive examples for different TikTok API endpoints:
Available Examples
Core Examples
hashtag.js- Fetch videos for specific hashtags/challengesuser-info.js- Get detailed user profile informationvideos.js- Fetch user's video feedtrending.js- Get trending/discover contentcomments.js- Fetch video comments and repliesmusic.js- Fetch music/audio videossimple-test.js- Basic signature generation test
TypeScript Support
typescript-example.js- Compiled TypeScript exampletypescript-example.ts- TypeScript source with type definitions
Quick Start with Examples
# Run basic hashtag example
node examples/hashtag.js [CHALLENGE_ID]
# Run user info example
node examples/user-info.js [USERNAME] --videos --search --relationship
# Run user videos example
node examples/videos.js [SEC_UID]
# Run trending content example
node examples/trending.js
# Run comments example
node examples/comments.js [VIDEO_ID]
# Run music example
node examples/music.js [MUSIC_ID]
# Run simple test
node examples/simple-test.jsNode.js Module Usage
import Signer from "tiktok-signature";
const signer = new Signer();
await signer.init();
const signature = await signer.sign("https://m.tiktok.com/api/post/item_list/?aid=1988");
const navigator = await signer.navigator();
console.log(signature);
console.log(navigator);
await signer.close();API Patterns
The examples demonstrate three main patterns for TikTok API requests:
1. Permanent URL + x-tt-params Only
// Used for: hashtags, music videos
const response = await axios({
method: "GET",
url: PERMANENT_URL, // Static TikTok URL
headers: {
"user-agent": userAgent,
"x-tt-params": xTtParams, // Encrypted parameters
"referer": "https://www.tiktok.com/"
}
});2. Dynamic URL + signed_url
// Used for: trending content
const response = await axios({
method: "GET",
url: signedUrl, // Generated signed URL
headers: {
"user-agent": userAgent,
"referer": "https://www.tiktok.com/"
}
});3. Permanent URL Merging + Both Headers
// Used for: user info, user videos, comments
const response = await axios({
method: "GET",
url: signedUrl, // Merged permanent + custom parameters
headers: {
"user-agent": userAgent,
"x-tt-params": xTtParams,
"referer": "https://www.tiktok.com/" // Or video-specific referer
}
});Python Client Example
import requests
def get_signature(url):
response = requests.post(
'http://localhost:8080/signature',
data=url,
headers={'Content-Type': 'text/plain'}
)
return response.json()
# Example usage
result = get_signature('https://m.tiktok.com/api/post/item_list/?aid=1988&count=30')
print(result['data']['signed_url'])JavaScript Client Example
async function getSignature(url) {
const response = await fetch('http://localhost:8080/signature', {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: url,
});
return await response.json();
}
// Example usage
const result = await getSignature('https://m.tiktok.com/api/post/item_list/?aid=1988');
console.log(result.data.signed_url);Error Handling
The service provides detailed error messages:
{
"status": "error",
"error": {
"message": "URL must be a TikTok domain",
"code": 400,
"timestamp": "2024-01-01T00:00:00.000Z"
}
}Common error codes:
400- Bad Request (invalid URL or parameters)404- Endpoint not found500- Internal Server Error
Security Considerations
- Input Validation: All URLs are validated and sanitized
- CORS: Configure CORS appropriately for your use case
- Rate Limiting: Implement rate limiting in production
- HTTPS: Use HTTPS in production environments
- Authentication: Add API key authentication if needed
Troubleshooting
Common Issues
Browser fails to launch
- Ensure Chromium dependencies are installed
- Check available system memory
Signature generation fails
- Verify the TikTok URL format
- Check server logs for detailed error messages
High memory usage
- Restart the service periodically
- Configure memory limits in Docker or PM2
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Guidelines
- Write tests for new features
- Follow ES module conventions
- Update documentation for API changes
- Ensure all tests pass before submitting PR
Support
- GitHub Issues: https://github.com/carcabot/tiktok-signature/issues
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Original author: CarcaBot
- Contributors: See contributors list
Note: This service is for educational and research purposes. Ensure compliance with TikTok's Terms of Service when using this tool.
