Skip to content

Node.js & TypeScript Integration ​

End-to-end guide demonstrating video publishing using direct URLs or local binary file uploads via Presigned URLs with TypeScript.


1. Installation ​

bash
npm install axios
# or
pnpm add axios

If your video is already hosted on a CDN, Amazon S3, Google Cloud, or your private server, call the publishing API directly:

typescript
import axios from 'axios';

const API_KEY = process.env.STACKVID_API_KEY || 'tk_live_YOUR_KEY';

async function publishWithDirectUrl() {
  const res = await axios.post(
    'https://api.stackvid.dev/api/tiktok/upload',
    {
      videoUrl: 'https://cdn.yourdomain.com/videos/product-demo.mp4',
      text: 'Exciting new product launch! 🔥 #launch #trending',
      targetChannel: 'US Fashion Channel',
      privacyLevel: 0,
    },
    {
      headers: {
        'X-API-Key': API_KEY,
        'Content-Type': 'application/json',
      },
    }
  );

  console.log('✅ Video published successfully! Item ID:', res.data.itemId);
}

3. Method 2: Local Binary File Upload (publish-local-video.ts) ​

typescript
import axios from 'axios';
import fs from 'fs';
import path from 'path';

const API_BASE_URL = 'https://api.stackvid.dev/api';
const API_KEY = process.env.STACKVID_API_KEY || 'tk_live_YOUR_KEY';

const client = axios.create({
  baseURL: API_BASE_URL,
  headers: {
    'X-API-Key': API_KEY,
    'Content-Type': 'application/json',
  },
});

async function publishLocalVideoWorkflow(videoFilePath: string, caption: string, channelName?: string) {
  try {
    const stats = fs.statSync(videoFilePath);
    console.log(`📁 Video file: ${path.basename(videoFilePath)} (${stats.size} bytes)`);

    // 1. Request Presigned Upload URL
    console.log('⚡ 1. Requesting Presigned Upload URL...');
    const presignedRes = await client.post('/uploads/presigned-url', {
      fileType: 'VIDEO',
      contentType: 'video/mp4',
      fileSizeBytes: stats.size,
    });

    const { uploadUrl, publicUrl } = presignedRes.data;

    // 2. Direct Binary PUT to uploadUrl
    console.log('🚀 2. Uploading video stream directly...');
    const fileStream = fs.createReadStream(videoFilePath);
    await axios.put(uploadUrl, fileStream, {
      headers: {
        'Content-Type': 'video/mp4',
        'Content-Length': stats.size,
      },
      maxBodyLength: Infinity,
      maxContentLength: Infinity,
    });

    // 3. Publish to TikTok
    console.log('📱 3. Submitting publish request to TikTok...');
    const publishRes = await client.post('/tiktok/upload', {
      videoUrl: publicUrl,
      caption: caption,
      targetChannel: channelName,
      privacyLevel: 0,
      allowComment: true,
      allowDuet: true,
      allowStitch: true,
    });

    console.log('🎉 PUBLISH SUCCESSFUL!');
    console.log('Share URL:', publishRes.data.shareUrl);
    console.log('Remaining Credits:', publishRes.data.remainingBalance);
    return publishRes.data;
  } catch (error: any) {
    console.error('❌ Publish error:', error.response?.data || error.message);
    throw error;
  }
}

publishVideoWorkflow('./sample.mp4', 'Automated video with StackVid! #automation');

StackVid - Multi-Platform Video & Social Commerce Automation Platform.