File size: 3,331 Bytes
6d9f36a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import axios from "axios";
import { sendSuccess, ErrorResponses } from "../../lib/response-helper.js";

/** @type {import("../../types/plugin").ApiPluginHandler} */
const handler = {
  name: "TikTok Downloader",
  description: "Download videos or slide photos from TikTok URLs. Supports both standard and HD quality downloads.",
  version: "1.0.0",
  method: "GET",
  category: ["downloader"],
  alias: ["tiktok", "tt"],
  tags: ["social-media", "video", "downloader"],

  parameters: {
    query: [
      {
        name: "url",
        type: "string",
        required: true,
        description: "TikTok video URL to download",
        example: "https://www.tiktok.com/@username/video/1234567890",
        pattern: "^https?:\\/\\/(www\\.|vm\\.)?tiktok\\.com\\/.+$"
      }
    ],
    body: [],
    headers: []
  },

  responses: {
    200: {
      status: 200,
      description: "Successfully retrieved TikTok video data",
      example: {
        status: 200,
        author: "Ditzzy",
        note: "Thank you for using this API!",
        results: {
          id: "1234567890",
          title: "Video Title",
          author: {
            nickname: "Username",
            unique_id: "username"
          },
          play: "https://video-url.com/video.mp4",
          wmplay: "https://video-url.com/video-watermark.mp4",
          hdplay: "https://video-url.com/video-hd.mp4",
          music: "https://music-url.com/audio.mp3",
          duration: 15,
          create_time: 1234567890
        }
      }
    },
    400: {
      status: 400,
      description: "Invalid TikTok URL provided",
      example: {
        status: 400,
        message: "Invalid URL - must be a valid TikTok URL"
      }
    },
    404: {
      status: 404,
      description: "Missing required parameter",
      example: {
        status: 404,
        message: "Missing required parameter: url"
      }
    },
    500: {
      status: 500,
      description: "Server error or TikTok API unavailable",
      example: {
        status: 500,
        message: "An error occurred, please try again later."
      }
    }
  },

  exec: async (req, res) => {
    const { url } = req.query;
    
    if (!url) {
      return ErrorResponses.missingParameter(res, "url");
    }

    if (!url.match(/tiktok/gi)) {
      return ErrorResponses.invalidUrl(res, "Invalid URL - must be a valid TikTok URL");
    }

    try {
      const videoData = await fetchTikTokVideo(url);
      return sendSuccess(res, videoData);
    } catch (error) {
      console.error("TikTok download error:", error);
      return ErrorResponses.serverError(res);
    }
  }
};

export default handler;

async function fetchTikTokVideo(url) {
  const encodedParams = new URLSearchParams();
  encodedParams.set("url", url);
  encodedParams.set("hd", "1");

  const response = await axios({
    method: "POST",
    url: "https://tikwm.com/api/",
    headers: {
      "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
      "Cookie": "current_language=en",
      "User-Agent": "Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"
    },
    data: encodedParams
  });

  if (!response.data || !response.data.data) {
    throw new Error("Invalid response from TikTok API");
  }

  return response.data.data;
}