← Back to blog Behind the scenes

How RandTube Works: Building a Random YouTube Video Generator

June 2026  ·  4 min read

Building a random YouTube video generator sounds simple — surely you just pick a random video ID and send the user there? In practice, it turns out to be a more interesting engineering problem than it first appears.

Why you can't just generate random video IDs

YouTube video IDs are eleven-character strings made up of letters, numbers, hyphens, and underscores. In theory, you could generate random combinations of those characters and check whether a valid video exists at that ID. In practice, this is almost completely useless. The space of possible IDs is enormous, and the fraction of those IDs that correspond to actual, publicly available videos is tiny. You'd generate thousands of invalid IDs for every valid one.

The YouTube Data API approach

The approach RandTube uses is different. We use the YouTube Data API v3 — Google's official interface for programmatically accessing YouTube's data — to search for videos matching a random search term within your chosen category.

Each category has a set of search terms associated with it. When you hit Spin, RandTube picks one of those terms at random, sends it to the YouTube Data API, and retrieves a set of results. It then picks one of those results at random and shows it to you.

User clicks Spin → Pick random search term from category → Call YouTube API: search?q="{term}"&maxResults=15 → Pick random result from the 15 returned → Call YouTube API: videos?id="{videoId}" for stats → Display to user

Keeping the API key secure

One of the trickier aspects of building a client-side web app that uses an API is keeping credentials secure. If you put an API key directly in your JavaScript code, anyone who views the page source can see and steal it.

RandTube solves this using Netlify Functions — small serverless functions that run on Netlify's servers rather than in the browser. When you hit Spin, your browser sends a request to our Netlify Function, which holds the API key securely as an environment variable. The function makes the actual call to YouTube's API and returns the results to your browser. Your browser never sees the API key.

Rate limiting and abuse prevention

The YouTube Data API has a daily quota of 10,000 units. Each search request costs 100 units, meaning we can serve roughly 50 spins per unit of quota. To prevent any single user from burning through the quota, RandTube implements rate limiting: each IP address is limited to 20 requests per minute. Inputs are also strictly validated — search queries are capped at 100 characters, and video IDs must match the exact format YouTube uses.

What's next

RandTube is a living project. Future improvements under consideration include filtering by video length, filtering by upload date, and a fully random mode that picks a category at random as well as the video. The goal is always the same: make it as easy as possible to stumble across something you'd never have found on your own.

See it in action

Head back to the homepage and give it a spin.

▶ Spin the wheel