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 / api-v1-response-serialization

API v1 response serialization

The API v1 response serialization layer turns Invidious domain data into stable, machine-readable responses for programmatic consumers. It gives videos, channels, feeds, playlists, account actions, and errors clear shapes while each route family keeps control of its own top-level contract. This page is part of Rendering, API serialization, and localization and relates to Web and API route families, Video watch and playback, and Channel content and subscriptions.

Serialization boundary

The API route handlers choose the data to return, while serializers and response builders define how that data appears on the wire. The boundary adds values that help clients identify, display, follow, and play resources without exposing the way Invidious fetched them.

Response pipeline

The response pipeline moves from a request through a feature route and domain data to a JSON document. Shared serializers handle repeated object shapes, while feature routes build envelopes when they must add channel details, pagination, account state, or action results.

flowchart LR
    Request[API v1 request] --> Route[Feature route]
    Route --> Data[Domain data]
    Data --> Serializer[Object serializer or response builder]
    Serializer --> Context[Locale and request options]
    Context --> JSON[JSON response]

References: src/…/routing.cr• L242-328 src/…/video_json.cr• L3-20 src/…/videos.cr• L7-27 src/…/channels.cr• L17-46

Domain representations

API v1 uses a small set of related representations rather than exposing internal objects as-is. A representation carries the data needed by a client for its task, so a full video response is richer than a video item inside search, a feed, or a playlist.

Video representations

The video representation is the main shared contract for playback and discovery. It identifies the video and author, supplies descriptions and time values, describes thumbnails and storyboards, and exposes playback, caption, music, and recommendation data when available. The same representation is used for a direct video response and for nested videos in clips, feeds, channel lists, and notifications.

Search and list results use smaller video representations when the caller does not need playback details. These objects keep the same core identity pattern, including type, videoId, author data, thumbnail data, and selected status or count fields. Consumers should use the type field and tolerate fields that are absent from compact results.

References: src/…/video_json.cr• L6-77 src/…/serialized_yt_data.cr• L14-153 src/…/videos.cr• L24-27 src/…/authenticated.cr• L117-150

Search and channel representations

Search results are a typed list. Video, channel, playlist, hashtag, category, and parse-error items identify their kind with a type value so a consumer can select the matching display or processing path. Channel and playlist items include stable IDs and links back to their related resources.

Channel endpoints add a channel envelope around lists and related resources. They use the same typed item forms for videos, playlists, and related channels, then add pagination through a continuation value when more data is available. A channel home response also combines channel identity, presentation data, and initial content in one object.

References: src/…/serialized_yt_data.cr• L155-291 src/…/serialized_yt_data.cr• L343-385 src/…/search.cr• L1-23 src/…/channels.cr• L17-131 src/…/channels.cr• L280-367 src/…/channels.cr• L477-532

Playlist and feed representations

Playlist responses describe the playlist and contain an ordered videos list. Each video carries an index so a client can keep the playlist order and request or remove a specific item. Authenticated feeds use the shared video representation inside separate notifications and videos lists, while subscription responses use compact author and ID records.

Authenticated playlist actions use small resource responses for creation and insertion, and empty responses for updates and deletion. Mix responses follow a similar list shape but add a mix identifier and item indexes. The API can also return a small HTML response for selected playlist and comment requests; that is an alternate response mode rather than a different JSON object contract.

References: src/…/playlists.cr• L49-77 src/…/playlists.cr• L101-147 src/…/misc.cr• L104-173 src/…/authenticated.cr• L117-235 src/…/authenticated.cr• L273-372

Authenticated representations

Authenticated routes represent both account data and state-changing actions. Preferences, history, subscriptions, playlists, tokens, and feed data use JSON when a client must read a result; subscription, history, and playlist mutations normally signal success with 204 and no body.

Some actions return a created resource with 201, a Location header, and a small JSON object. Token registration can instead return a redirect or a token string, and token authorization can use an HTML step before the JSON response. Consumers must therefore use the route's method, status, and content type together rather than assume that every authenticated route returns one JSON shape.

References: src/…/authenticated.cr• L14-55 src/…/authenticated.cr• L77-115 src/…/authenticated.cr• L153-235 src/…/authenticated.cr• L398-461

Shared response values

The layer makes values consistent across object types where a client commonly needs to join or display them. It also keeps raw and human-friendly forms when both are useful.

Objects expose explicit IDs for videos, channels, playlists, and posts. Related URLs use the instance route space, which lets a client follow an object without rebuilding paths from internal data. Thumbnail fields use arrays of quality records with a URL and dimensions rather than one fixed image.

Playback data follows the same principle. A video can expose manifest links and stream records with media type, quality, size, and timing information. The local request option can change stream URLs to use the instance proxy without changing the rest of the video shape.

References: src/…/common.cr• L3-17 src/…/video_json.cr• L15-20 src/…/video_json.cr• L72-207 src/…/videos.cr• L12-25 src/…/channels.cr• L46-130

Time and locale values

Stored times are represented as Unix values for machine use. Many objects also provide a publishedText or similar field for direct display. The display form comes from the request locale, so the same object can have different human-readable text while its machine value stays the same.

The locale is passed into shared object serializers by feed, search, channel, and video routes. This keeps relative dates and count text aligned with the request preferences instead of with the process default. Channel metadata also keeps machine values such as Unix join time beside text and presentation fields.

References: src/…/video_json.cr• L22-30 src/…/serialized_yt_data.cr• L114-135 src/…/search.cr• L2-20 src/…/feeds.cr• L2-20 src/…/channels.cr• L92-111

Response envelopes and modes

Top-level response shape belongs to the endpoint family. The shared serializers define object members, while routes decide whether to return an object, an array, or an envelope with fields such as videos, playlists, notifications, relatedChannels, and continuation.

Collection envelopes

Collection endpoints use envelopes when they need metadata or a continuation token. Direct search and trending responses can be arrays, while channel content, channel playlists, feeds, and related-channel responses wrap their arrays so the response can carry related information. The key can also reflect the resource type: channel playlist-like tabs use playlists, while video tabs use videos.

Consumers should read the endpoint contract before assuming that every API v1 response is an object or that every list uses the same member name. The object forms are consistent within a route family, not across every endpoint.

References: src/…/channels.cr• L175-185 src/…/channels.cr• L267-309 src/…/channels.cr• L495-505 src/…/search.cr• L16-23 src/…/search.cr• L75-85 src/…/feeds.cr• L2-44 src/…/authenticated.cr• L117-150

Alternate resource formats

Some API v1 routes are resource adapters rather than JSON object endpoints. Caption and transcript selections can return WebVTT, storyboard selections can return WebVTT, legacy annotations return XML, and authenticated notifications use server-sent events. These routes still sit beside the JSON layer, but their content type and client contract are different.

The comments and community routes can also select an HTML response through a format option. The JSON form remains the source data used to build that HTML response, but clients that need programmatic data should request and handle the JSON mode.

References: src/…/videos.cr• L29-175 src/…/videos.cr• L182-255 src/…/videos.cr• L440-524 src/…/misc.cr• L32-101 src/…/authenticated.cr• L465-493

Errors and incomplete data

The API keeps failures inside the response contract when it can. Route-level failures use an error object and an HTTP status, while a bad item inside a larger parsed list can become a typed item so the rest of the list remains useful.

Error objects

An API error is a JSON object with an error message. Unexpected exceptions can also include errorBacktrace, and selected errors can add fields such as a replacement author ID. The HTTP status remains the primary signal for clients; the JSON body gives the reason and optional context.

The route layer maps common conditions to different statuses. Invalid input usually produces a client error, missing content produces a not-found response, authorization or feature restrictions can produce forbidden or conflict responses, and upstream or parse failures can produce a server error. Channel lookup can also return a redirect status with a replacement author ID when a channel has moved.

References: src/…/errors.cr• L134-175 src/…/videos.cr• L16-26 src/…/channels.cr• L4-14 src/…/authenticated.cr• L77-106 src/…/channels.cr• L477-505

Parse-error items

A parse-error item is an API-only placeholder for one list item that could not be parsed. It has a type of parse-error and carries diagnostic text, which allows a search or playlist response to preserve the position of the other items instead of failing as a whole.

This behavior is important for clients that process mixed result lists. A client should branch on the item type before reading video, channel, or playlist fields.

References: src/…/serialized_yt_data.cr• L294-340 src/…/playlists.cr• L442-453 src/…/playlists.cr• L484-541

Consumer contract

Programmatic consumers should treat field names, type markers, nesting, status codes, and content types as the contract. They should not infer a response shape from the source object or from one endpoint and apply it to all other API v1 endpoints.

Contract boundaries

The shared video and search serializers provide the main consistency points. Direct route builders remain responsible for special envelopes such as channel metadata, community posts, URL resolution, and authenticated account actions. A change to one of those builders can affect only its route family even when it uses the same domain object as another route.

The API also exposes both public and authenticated route families. Authenticated routes add user state and mutation results, and successful mutations often return 204 with no JSON body or 201 with a small resource object. Notification delivery is a separate event-stream contract rather than a JSON feed. Clients must use the HTTP method, status, and content type together with the body contract.

References: src/…/routing.cr• L246-328 src/…/channels.cr• L422-475 src/…/channels.cr• L478-533 src/…/authenticated.cr• L117-235 src/…/authenticated.cr• L273-372 src/…/authenticated.cr• L465-493

Change checklist

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

  • A change to the shared video serializer affects direct video responses, clips, feeds, channel lists, notifications, and nested recommendations. Check whether the field belongs in all of those contexts.
  • A change to thumbnail generation affects every object that emits videoThumbnails, including compact search results, playlist items, mixes, and full video responses.
  • A change to locale-aware fields must preserve both the machine value and the localized display value. Check routes that pass a locale and object serializers that accept one.
  • A change to a top-level envelope must be checked against continuation handling and against clients that expect videos, playlists, relatedChannels, or notifications at a specific path.
  • A change to error handling must preserve the link between the HTTP status, the error JSON field, and any additional fields used by redirect or recovery flows.
  • A change to a route that supports format=html or a non-JSON content type must not silently change the JSON mode used by programmatic consumers.
  • A change to channel tabs must preserve the relation between the tab's resource kind and its envelope key, especially videos, playlists, and relatedChannels.
  • A change to authenticated actions must preserve the status and body pairing for 204 mutations, 201 resource creation, token redirects, and event-stream notifications.

Important failure modes

TriggerConsequenceGuard
A consumer assumes every result item is a videoChannel, playlist, hashtag, category, or parse-error items cause missing-field errorsBranch on the type field before reading item-specific fields
A consumer treats publishedText as a stable timestampLocale changes or relative-time wording break sorting and storageUse the Unix published value for machine logic
A client assumes every successful mutation returns JSON204 responses have no body to parseUse the HTTP status and only parse a body for response types that define one
A parser fails for one item in a larger listThe response can contain a parse-error item instead of the expected objectHandle typed error items and continue processing valid items
A client sends a JSON request to a route serving WebVTT, XML, or eventsJSON parsing fails even though the route succeededSelect behavior from the response content type
A caller assumes all lists use the same envelope keyChannel, search, feed, playlist, and hashtag responses expose different top-level keysBind the client model to the specific endpoint contract
A client treats a channel home response as a plain video listChannel metadata and related channels are lost or fields are read at the wrong levelModel channel home separately from channel content tabs
A client parses authenticated notifications as JSONThe notification route returns an event streamSelect the parser from the response content type

Key files

FileWhy to read it
src/invidious/jsonify/api_v1/video_json.crDefines the main full-video JSON contract, nested playback data, thumbnails, captions, storyboards, and recommendations.
src/invidious/jsonify/api_v1/common.crDefines the shared thumbnail record shape used across API v1 objects.
src/invidious/helpers/serialized_yt_data.crDefines typed search, playlist, channel, hashtag, category, and parse-error representations.
src/invidious/routes/api/v1/videos.crShows how direct video and clip routes select shared serialization and how related resource formats differ.
src/invidious/routes/api/v1/channels.crShows channel home objects, content envelopes, continuation fields, and reuse of typed object serializers.
src/invidious/routes/api/v1/search.crShows array and result-envelope forms for discovery responses.
src/invidious/routes/api/v1/feeds.crShows public feed responses that reuse the shared localized video representation.
src/invidious/playlists.crDefines playlist and playlist-video JSON shapes, including item indexes and ordered lists.
src/invidious/routes/api/v1/authenticated.crShows authenticated response shapes and the distinction between body-bearing 201 responses, empty 204 responses, and event streams.
src/invidious/helpers/errors.crDefines the common JSON error body and its relationship to HTTP status codes.
src/invidious/routing.crMaps API v1 endpoint families to the handlers that own their response contracts.