Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Embedding TikTok Videos with the oEmbed API: A Lightweight Click-to-Load Player in JavaScript

If you run a blog, a portfolio or a small CMS, sooner or later someone asks for TikTok videos on a page. Pasting TikTok's embed snippet by hand works for one video, but it gets messy when you want a grid of them, consist

If you run a blog, a portfolio or a small CMS, sooner or later someone asks for TikTok videos on a page. Pasting TikTok's embed snippet by hand works for one video, but it gets messy when you want a grid of them, consistent sizing, or a lightweight page that doesn't load a dozen players before anyone presses play.

TikTok exposes a public oEmbed endpoint that handles most of this. It takes a video URL and returns JSON with the embed HTML and the video's ID. No API key, no login.

The endpoint

GET https://www.tiktok.com/oembed?url=<video URL>

A response today looks like this (trimmed):

{
  "version": "1.0",
  "type": "video",
  "width": "325",
  "height": "578",
  "html": "<iframe height=\"578\" width=\"325\" src=\"https://www.tiktok.com/player/v1/6718335390845095173?embed_from=oembed&music_info=1&description=1&autoplay=1&muted=1\" ... loading=\"lazy\"></iframe>",
  "provider_name": "TikTok",
  "provider_url": "https://www.tiktok.com",
  "embed_product_id": "6718335390845095173",
  "embed_type": "video"
}

Two things are worth noticing. First, the embed is now a plain <iframe> pointing at TikTok's player, not the older blockquote plus embed.js script, so it plays nicely with lazy loading. Second, embed_product_id is the video ID, which is all you need to build your own player URL. Older tutorials show title, author_name and thumbnail_url fields; don't rely on them, because the current response doesn't include them.

Only public videos work. Private or deleted videos return an error, which is exactly what you want.

Fetching it server-side

Call it from your server or build step rather than the browser, so you can cache results and avoid CORS issues. A small Node 18+ helper using the built-in fetch:

// tiktok-oembed.js
const cache = new Map();

export async function getTikTokEmbed(videoUrl) {
  if (cache.has(videoUrl)) return cache.get(videoUrl);

  const endpoint = new URL('https://www.tiktok.com/oembed');
  endpoint.searchParams.set('url', videoUrl);

  const res = await fetch(endpoint);
  if (!res.ok) throw new Error(`oEmbed failed: ${res.status}`);

  const data = await res.json();
  const result = {
    id: data.embed_product_id,
    width: Number(data.width),
    height: Number(data.height),
    html: data.html,
  };

  cache.set(videoUrl, result);
  return result;
}

In a real project, swap the Map for Redis, a JSON file written at build time, or your framework's data cache. Since the response is small and rarely changes, caching for days is fine.

A click-to-load player

Even with loading="lazy", a grid of iframes is heavy and every one autoplays muted. A nicer pattern is to show a simple placeholder and only insert the player when the visitor clicks. Because you have the ID, you can build the player URL yourself and choose the player options:

function playerUrl(id) {
  const u = new URL(`https://www.tiktok.com/player/v1/${id}`);
  u.searchParams.set('autoplay', '1');   // they clicked, so play
  u.searchParams.set('description', '1');
  u.searchParams.set('music_info', '1');
  return u.toString();
}

function renderCard(el, embed) {
  const btn = document.createElement('button');
  btn.className = 'tt-play';
  btn.textContent = 'Play TikTok video';
  btn.style.aspectRatio = `${embed.width} / ${embed.height}`;

  btn.addEventListener('click', () => {
    const frame = document.createElement('iframe');
    frame.src = playerUrl(embed.id);
    frame.width = embed.width;
    frame.height = embed.height;
    frame.allow = 'autoplay; encrypted-media; fullscreen; picture-in-picture';
    frame.allowFullscreen = true;
    el.replaceChildren(frame);
  }, { once: true });

  el.replaceChildren(btn);
}

Building the iframe with createElement instead of injecting embed.html also means you control exactly which attributes end up on the page.

Things to watch out for

  • Short links. Links like vm.tiktok.com/... redirect to the full URL. Resolve them server-side (follow the redirect) before calling oEmbed.
  • Rate limits. There are no published limits, so cache aggressively and never call the endpoint on every page view.
  • Removed videos. Handle errors gracefully and hide the card rather than showing a broken player.
  • Credit. The player shows the creator's name, but also link to their profile next to the video. The creator owns the content; you're only embedding it.

Wrapping up

oEmbed covers the common "show some TikToks on my site" request with a few lines of code and no credentials. For things it doesn't cover, like browsing a public profile's stats or recent uploads without writing any code, a web tool such as TikTk Viewer is quicker than building your own scraper, and it sticks to what's already public.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.