Introducing a new way to review your PRs.

Read the announcement

iv-org / invidious

Invidious is an alternative front-end to YouTube

View on GitHub

19 documents · Updated Oct 12, 2026

docs / web-and-api-route-families

Web and API route families

Invidious groups incoming requests by user-facing page, embedded player, media proxy, companion service, and API version. A central registration pass maps each path and HTTP method to a feature handler, while the handler supplies the page, redirect, stream, or structured response.

Route registration model

The route registry is the boundary between the HTTP server and feature behavior. It keeps path shape and method selection in one place, then delegates request work to handlers that understand videos, channels, accounts, feeds, search, and media resources.

The request path moves from registration to a family-specific handler and then to a response format suited to the caller:

flowchart LR
    R[HTTP request] --> G[Route registration]
    G --> W[Browser routes]
    G --> E[Embed routes]
    G --> A[API v1 routes]
    G --> M[Manifest and playback routes]
    G --> C[Companion routes]
    W --> H[Feature handler]
    E --> H
    A --> J[JSON handler]
    M --> P[Proxy or manifest handler]
    C --> F[Companion forwarder]

Registration groups

/home/user/repo/src/invidious/routing.cr defines the registration pass. register_all adds the common route families and can omit browser-only routes in an API-only build. Each group is named for its product area, which makes the registry the best starting point when a new endpoint must be placed.

The route declaration also selects the HTTP method. A path can therefore expose separate read, write, delete, or preflight behavior without mixing those contracts in one handler.

References: src/…/routing.cr• L4-18 src/…/routing.cr• L18-50

Feature delegation

A route declaration names a handler and an action. The handler receives the request context and decides how to obtain feature data, enforce the feature’s access rules, and return HTML, JSON, a redirect, or a proxied response. This keeps route matching separate from feature behavior.

The same feature can have more than one public surface. For example, channel and video concepts have browser handlers under ordinary paths and JSON handlers under /api/v1, while playback has dedicated proxy paths for media clients.

References: src/…/routing.cr• L116-159 src/…/routing.cr• L242-329

Browser-facing route families

Browser routes provide the main Invidious site. They cover discovery, video and channel pages, feeds, playlists, search, account actions, and user preferences. Most browser route files are excluded from an API-only build because their purpose is to render or support the web experience.

Content and discovery pages

The content families turn YouTube concepts into Invidious pages. Channel routes support channel tabs and older channel URL forms. Watch routes accept common video URL forms and normalize them to the main watch page. They also resolve clips and selected download actions into the matching media or caption flow.

These routes are organized by the user’s task rather than by one shared data source. A handler can redirect an old or alternate URL before it loads content, then render the canonical page for that feature.

References: src/…/routing.cr• L116-159 src/…/routing.cr• L161-190 src/…/watch.cr• L4-61 src/…/channels.cr• L4-64

Account and personal pages

Account routes form the browser surface for login, preferences, subscriptions, watch history, tokens, and playlist actions. Feed routes add personal and public views such as subscriptions, history, trending, and RSS.

These routes rely on request state prepared before routing. A signed-in request can carry a user and session context to the handler. An anonymous request is redirected or shown an error when the feature needs an account.

References: src/…/routing.cr• L56-110 src/…/feeds.cr• L8-32 src/…/feeds.cr• L64-141

Video and embed pages

Watch and embed routes both present video playback, but they serve different callers. The watch family is the full site experience. The embed family is a restricted player surface that accepts playlist and live-stream forms and resolves them to a playable embedded page.

The embed handler can redirect alternate embed inputs to a canonical video path. It then prepares the video data and playback choices for the embedded view. Shared request policy controls which pages may be placed in a frame; see HTTP middleware and static assets.

References: src/…/routing.cr• L161-176 src/…/embed.cr• L4-35 src/…/embed.cr• L61-125 src/…/before_all.cr• L35-58

API route families

The API surface is grouped under /api/v1 and uses feature-oriented handlers. Its public endpoints expose video, channel, feed, search, playlist, mix, statistics, and related data. Its authenticated endpoints expose account state and account changes.

Public API v1

The public API maps resource families to stable JSON actions. Video routes expose metadata and related media data such as captions, transcripts, comments, storyboards, annotations, and clips. Channel routes expose channel details, content tabs, community posts, and related channels. Feed and search routes expose discovery results. Miscellaneous routes cover playlists, mixes, URL resolution, and service statistics.

The route path supplies the resource identity, while query values refine the request, such as region, continuation, format, or local media proxying. The handler turns the result into the API response contract and returns a JSON error when the resource cannot be served.

For the response contract shared by these handlers, see API v1 response serialization. The channel resource model and its subscription-facing uses are described in Channel content and subscriptions.

References: src/…/routing.cr• L246-285 src/…/routing.cr• L323-329 src/…/videos.cr• L7-27 src/…/videos.cr• L29-175 src/…/channels.cr• L17-47 src/…/channels.cr• L399-545 src/…/feeds.cr• L2-44 src/…/search.cr• L2-23

Authenticated API

The authenticated family uses the /api/v1/auth prefix for operations that read or change a user’s state. It covers preferences, portable data, history, subscriptions, playlists, tokens, feeds, and optional notifications.

The route shape identifies the account operation, but the handler expects the request setup to provide a signed-in user. Write actions use the HTTP method to show intent: POST creates or changes state, PATCH changes playlist attributes, and DELETE removes state. The notification endpoint is a long-lived event stream that reads the requested topic set and supplies account updates to clients.

References: src/…/routing.cr• L288-321 src/…/authenticated.cr• L14-55 src/…/authenticated.cr• L57-114 src/…/authenticated.cr• L152-208 src/…/authenticated.cr• L485-493

Version boundary

The v1 segment is a contract boundary for programmatic clients. The registry keeps the current resource paths together so that a future API version can choose a different contract without changing browser paths or media proxy paths.

Some API v1 paths preserve older client forms through redirects. These compatibility paths still delegate to the channel handler so clients can move to the newer resource shape without a second data path.

References: src/…/routing.cr• L242-285 src/…/routing.cr• L274-280 src/…/channels.cr• L1-14

Embedded and companion route families

Embedded playback and companion access are separate from ordinary browser pages. Embedded playback serves a player-friendly page, while companion routes expose a controlled forwarding surface to configured Invidious companion services.

Media proxy routes

Manifest and playback paths support clients that cannot use YouTube media URLs directly. Manifest handlers provide DASH or HLS data and can redirect manifest work to a configured companion or rewrite media targets to the local playback proxy. Playback handlers then resolve the upstream media host, enforce media access limits, and serve the stream through local range-aware delivery. A separate latest-version path turns an expiring media choice back into a current media URL.

Image and storyboard routes follow the same proxy role for thumbnails and preview assets. These paths are registered outside the browser-only block because browser pages, embedded players, and API clients can all need the assets. The wider playback model is described in Video watch and playback.

References: src/…/routing.cr• L196-228 src/…/manifest.cr• L3-43 src/…/manifest.cr• L146-165 src/…/manifest.cr• L168-244 src/…/video_playback.cr• L4-123 src/…/video_playback.cr• L217-313 src/…/images.cr• L1-28

Companion forwarding

The /companion/* family forwards supported GET, POST, and OPTIONS requests to a configured companion service. The forwarding handler keeps the request path and query string, passes the request data onward, and copies the companion response back to the caller.

The registry adds this family only when a companion is configured. This makes companion access an optional deployment capability rather than a route that is present but unusable on every instance.

References: src/…/routing.cr• L230-236 src/…/companion.cr• L1-59

Route availability and request context

Route availability depends on build mode and selected runtime features. Request preprocessing runs before feature handlers and supplies preferences, optional user state, security headers, and route-specific policy; see HTTP middleware and static assets.

Build and feature gates

An API-only build omits the browser route set. Notification routes are also added only when notifications are enabled. Companion routes depend on companion configuration. These gates change the public route set and must be considered when clients or deployment checks rely on a path.

The media and API families remain registered in the common pass. They support non-browser clients and are not treated as part of the browser page set.

References: src/…/routing.cr• L18-49 src/…/routing.cr• L40-43 src/…/routing.cr• L230-235 src/…/embed.cr• L1-3

Handler context

Before a feature handler runs, shared request setup may load preferences and a session-backed user. It also marks paths such as media proxies and companions as paths that do not need normal browser session work. This lets route families keep different response and access needs without duplicating all request setup.

The route registry does not replace that shared setup. It selects the handler after the request has passed through the common policy layer.

References: src/…/before_all.cr• L14-31 src/…/before_all.cr• L70-107

Change checklist

When these files or sections are changed, remember to consider these:

  • When adding or moving a route, check both the registration group and the handler’s expected path parameters. A mismatch can select the right feature with the wrong resource identity.
  • When changing a browser route, check the API-only build split and the compile-time exclusions in browser route files.
  • When changing an authenticated API path, check that its /api/v1/auth contract still matches the session context prepared before the handler.
  • When changing manifest or companion paths, check their matching OPTIONS routes and cross-origin response behavior.
  • When changing a redirect or compatibility path, check the canonical path and the client-visible status and response format.
  • When adding a route that uses embedded media, check the frame and media policy described in HTTP middleware and static assets.
  • When changing a video or channel API response, check the shared resource serializer and the related browser or subscription surface described in API v1 response serialization and Channel content and subscriptions.
  • When changing local manifest URLs or playback redirects, check that the manifest, playback, and latest-version paths still form one usable media chain.

Important failure modes

TriggerConsequenceGuard
A catch-all channel route is registered before specific channel tabsRequests for known tabs can be redirected to the channel homeKeep the catch-all route after the specific channel routes
A browser route is added without the browser-build exclusion used by its route fileAn API-only build can fail to compile or expose an unintended web surfaceKeep browser-only registration and route files under the same build guard
An authenticated API handler is reached without a session-backed userThe handler cannot fulfill the account operation and can fail while reading request stateKeep authenticated paths under the shared session setup and use the /api/v1/auth family
A manifest or playback path lacks its matching preflight behaviorBrowser media clients can fail before they request the streamPreserve the paired OPTIONS route and cross-origin response contract
Companion configuration is absent while a companion path is assumed to existThe route is not registered and callers receive a missing pathCheck companion configuration before publishing or using the route
A local manifest rewrite does not match the playback route’s accepted URL shapeThe player receives media URLs that cannot be resolved by the local proxyKeep local DASH and HLS rewrites aligned with the /videoplayback query and greedy paths
A video or channel handler changes its JSON shape without the shared serializer or client contractAPI clients can receive valid JSON with missing or renamed fieldsTreat the v1 serializer and resource-specific routes as one response contract

Key files

FileWhy to read it
src/invidious/routing.crPrimary map of route families, methods, feature gates, and handler delegation
src/invidious/routes/watch.crExample of a browser video flow with redirects, user state, playback choices, and rendering
src/invidious/routes/embed.crEmbedded player flow and alternate embed URL normalization
src/invidious/routes/api/v1/videos.crPublic API resource handling and JSON response boundary
src/invidious/routes/api/v1/channels.crPublic channel tabs, community content, related channels, and compatibility redirects
src/invidious/routes/api/v1/feeds.crPublic feed endpoints and administrator-controlled availability
src/invidious/routes/api/v1/authenticated.crAuthenticated API contract for user state, account changes, and notifications
src/invidious/routes/api/manifest.crMedia manifest, playback redirect, and client-facing proxy behavior
src/invidious/routes/video_playback.crLocal stream delivery, media URL refresh, and playback access checks
src/invidious/routes/companion.crOptional companion request forwarding boundary
src/invidious/routes/before_all.crShared request context and path-specific preprocessing that route handlers rely on
Application bootstrap and request routingParent overview of startup and the point where route registration enters the server
HTTP middleware and static assetsRelated overview of request preprocessing, policy headers, and asset delivery
Video watch and playbackRelated overview of the browser video and media delivery flow
Channel content and subscriptionsRelated overview of channel data and user subscription behavior
API v1 response serializationRelated overview of the shared API response contract