Appearance
Class: YoutubeService
YouTube integration service: fetch the information of a video or a playlist, and embed a YouTube player in the overlay page.
Remarks
Available everywhere via k.youtube. You never have to instantiate this service yourself — it is created by the SDK when the app/widget starts.
Example
ts
// Embed a video and play it automatically
await k.youtube.embed('dQw4w9WgXcQ', { autoplay: true });Implements
Methods
embed()
ts
embed(video_id, options?): Promise<YoutubePlayer>;Loads the YouTube iframe API and embeds a player in the overlay page (element appended to the body).
Parameters
video_id
string
The identifier of the YouTube video to play.
options?
YoutubeEmbedOptions = {}
Player options.
Returns
Promise<YoutubePlayer>
The native YT.Player instance, typed as YoutubePlayer.
Remarks
The player is created with no visible controls (controls: 0) and takes up 100% of its container's width/height. API loading is shared by successive and concurrent calls in the same page. A failed API load rejects within 15 seconds and can be retried. The promise resolves when the player is constructed; use onReady for playback commands.
Example
ts
await k.youtube.embed('dQw4w9WgXcQ', {
autoplay: true,
onStateChange: (e) => console.log('state', e.data),
});playlistVideos()
ts
playlistVideos(playlist_id): Promise<PlaylistVideosData>;Fetches the videos of a YouTube playlist from the backend.
Parameters
playlist_id
string
The YouTube playlist identifier.
Returns
Promise<PlaylistVideosData>
Up to 50 playlist entries (PlaylistVideosData), without pagination.
See
video for a single video.
video()
ts
video(video_id): Promise<VideoData>;Fetches the information of a YouTube video from the backend.
Parameters
video_id
string
The YouTube video identifier.
Returns
Promise<VideoData>
The video data (VideoData), or null if the video is unavailable.
See
playlistVideos for a whole playlist.