Answers

How do I add TikTok and YouTube data as tools in a Node.js agent?

Short answer

Wrap each endpoint in a tool with a zod schema and a fetch call: the model picks the tool, your code calls okdata and returns trimmed JSON. A TikTok search tool costs $0.002 per call for about 30 videos, and a YouTube transcript tool $0.004 per video; nothing runs on your server except the HTTP request.

A growth team's Next.js app has a chat box: "What are TikTok creators saying about our category this week, and what do the top YouTube reviews actually say?" To answer, the agent needs two tools, one that searches TikTok and one that reads a YouTube video's transcript. Both are a fetch away, which also means they run in a Vercel function or any Node 18+ server without a browser or a scraping library.

The tools

import { generateText, tool, stepCountIs } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';

const API = 'https://api.okdata.online';
const headers = { Authorization: `Bearer ${process.env.OKDATA_API_KEY}` };

async function call(path: string, params: Record<string, string>) {
  const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers });
  const body = await res.json();
  if (body.code !== 'ok') return { error: body.code, message: body.message, retry: body.retry };
  return body.data;
}

const tools = {
  searchTikTok: tool({
    description: 'Search public TikTok videos. One call returns one page of videos and is billed once.',
    inputSchema: z.object({
      query: z.string(),
      sort: z.enum(['relevance', 'latest', 'most_liked']).optional(),
    }),
    execute: ({ query, sort }) =>
      call('/v1/tiktok/videos/search', {
        query,
        ...(sort ? { sort } : {}),
        fields: 'id,url,text,published_at,metrics.views,metrics.likes,author.username',
      }),
  }),
  youtubeTranscript: tool({
    description: 'Plain-text transcript of one YouTube video, by its 11-character ID.',
    inputSchema: z.object({ id: z.string().length(11), lang: z.string().optional() }),
    execute: ({ id, lang }) =>
      call('/v1/youtube/videos/transcript/get', { id, ...(lang ? { lang } : {}), fields: 'text' }),
  }),
};

const { text } = await generateText({
  model: openai(process.env.OPENAI_MODEL ?? 'gpt-4.1-mini'),
  tools,
  stopWhen: stepCountIs(8),
  prompt: 'What are TikTok creators saying about standing desks this week? Read one top YouTube review too.',
});
console.log(text);

fields: 'text' on the transcript drops the timestamped segments and keeps only the full text, which is all a summary needs. The endpoints are TikTok video search and YouTube video transcript.

What it costs per question

Question Calls Cost
2 TikTok searches 2 $0.004
2 searches and 3 transcripts 5 {{cost:tiktok.videos.search2+youtube.videos.transcript.get3}}
The cap: 8 steps, all transcripts 8 $0.032

Gotchas

  • The transcript endpoint takes the video ID, not the URL. Tell the model to pass the 11 characters after v= or youtu.be/.
  • On Vercel, raise maxDuration for the route to 60 seconds or more; a slow platform response should not be cut off by your function.
  • Videos without captions return an error and cost nothing. The model sees the error and can pick another video.
  • There is no endpoint for a TikTok account's full video list or for TikTok's trending hashtags. Search by keyword or creator name instead.
  • Text from TikTok and YouTube is written by strangers. Keep it as tool output, never as system instructions.

Alternatives

The AI SDK's MCP client can load https://api.okdata.online/mcp with the same Authorization header, and the agent then gets every endpoint through find, describe and call instead of two fixed tools. For YouTube search metadata alone, the official YouTube Data API is free within Google's daily quota and worth using if you already have a Google Cloud project. npm packages that scrape transcripts directly are free too, though they tend to be blocked from cloud IPs. Retry and error handling are on the errors page.

Updated