API reference

Universal functions

convertUrlToEmbedUrl(url: string): string

Converts any recognized media URL to its embeddable form. This is the main entry point for most use cases.

  • Detects the provider, extracts the ID, and builds the embed URL in one call
  • Returns "" if the URL is not recognized by any registered provider
  • Returns "" if the URL is empty or ID extraction fails
import { convertUrlToEmbedUrl } from "@social-embed/lib";
convertUrlToEmbedUrl("https://youtu.be/Bd8_vO5zrjo");
// "https://www.youtube.com/embed/Bd8_vO5zrjo"
convertUrlToEmbedUrl("https://example.com/unknown");
// ""

getProviderFromUrl(url: string): EmbedProvider | undefined

Returns the provider object that can parse the given URL, or undefined if no provider matches.

Useful for detecting which platform a URL belongs to, or for restricting embeds to known providers in server-side validation.

import { getProviderFromUrl } from "@social-embed/lib";
const provider = getProviderFromUrl("https://youtu.be/Bd8_vO5zrjo");
// { name: "YouTube", canParseUrl, getIdFromUrl, getEmbedUrlFromId }
getProviderFromUrl("https://example.com");
// undefined

isValidUrl(url: string): boolean

Checks if a string is a syntactically valid HTTP or HTTPS URL using new URL().

import { isValidUrl } from "@social-embed/lib";
isValidUrl("https://example.com"); // true
isValidUrl("not-a-url"); // false
isValidUrl("ftp://example.com"); // false (only http/https)

Types

EmbedProvider interface

Every provider implements this interface:

interface EmbedProvider {
readonly name: string;
canParseUrl(url: string): boolean;
getIdFromUrl(url: string): string | string[];
getEmbedUrlFromId(id: string, ...args: unknown[]): string;
}
MemberDescription
nameUnique identifier (e.g., "YouTube", "Spotify")
canParseUrl(url)Returns true if this provider can handle the URL
getIdFromUrl(url)Extracts the media ID (or [id, type] for Spotify)
getEmbedUrlFromId(id, ...args)Builds the embed-ready URL from the extracted ID

EmbedProviderRegistry class

A registry for managing multiple EmbedProvider instances.

import { EmbedProviderRegistry } from "@social-embed/lib";
const registry = new EmbedProviderRegistry();
MethodSignatureDescription
register(provider: EmbedProvider) => voidRegisters a provider. Overwrites if name exists.
listProviders() => EmbedProvider[]Returns all registered providers.
getProviderByName(name: string) => EmbedProvider | undefinedRetrieves a provider by its name.
findProviderByUrl(url: string) => EmbedProvider | undefinedReturns the first provider whose canParseUrl() returns true.

Instances

defaultRegistry

A pre-configured EmbedProviderRegistry with all 7 built-in providers registered. This is the registry used by convertUrlToEmbedUrl() and getProviderFromUrl().

import { defaultRegistry } from "@social-embed/lib";
// Register a custom provider on the shared registry
defaultRegistry.register(myCustomProvider);

defaultProviders

An array of all 7 built-in EmbedProvider objects: DailyMotion, EdPuzzle, Loom, Spotify, Vimeo, Wistia, YouTube.

import { defaultProviders } from "@social-embed/lib";
console.log(defaultProviders.map((p) => p.name));
// ["DailyMotion", "EdPuzzle", "Loom", "Spotify", "Vimeo", "Wistia", "YouTube"]

Per-provider exports

Each provider exports its implementation object, helper functions, and regex patterns.

YouTube

ExportTypeDescription
YouTubeProviderEmbedProviderProvider implementation
getYouTubeIdFromUrl(url)(string) => stringExtract video ID from URL
getYouTubeEmbedUrlFromId(id)(string) => stringBuild youtube.com/embed/{id}
isYouTubeShortsUrl(url)(string) => booleanDetect YouTube Shorts URLs
youTubeUrlRegexRegExpMatches standard YouTube URLs
youTubeShortsRegexRegExpMatches YouTube Shorts URLs
YOUTUBE_SHORTS_DIMENSIONS{ width, height }Default Shorts dimensions (347×616)

Spotify

ExportTypeDescription
SpotifyProviderEmbedProviderProvider implementation
getSpotifyIdAndTypeFromUrl(url)(string) => [string, string]Extract [id, type] from URL
getSpotifyEmbedUrlFromIdAndType(id, type)(string, string) => stringBuild embed URL with content type
spotifyUrlRegexRegExpMatches open.spotify.com URLs
spotifySymbolRegexRegExpMatches spotify:type:id URIs
SPOTIFY_TYPESstring[]Valid content types: track, album, playlist, artist, show, episode

Vimeo

ExportTypeDescription
VimeoProviderEmbedProviderProvider implementation
getVimeoIdFromUrl(url)(string) => stringExtract video ID
getVimeoEmbedUrlFromId(id)(string) => stringBuild player.vimeo.com/video/{id}
vimeoUrlRegexRegExpMatches Vimeo URLs

DailyMotion

ExportTypeDescription
DailyMotionProviderEmbedProviderProvider implementation
getDailyMotionIdFromUrl(url)(string) => stringExtract video ID
getDailyMotionEmbedFromId(id)(string) => stringBuild embed URL
dailyMotionUrlRegexRegExpMatches DailyMotion URLs

Loom

ExportTypeDescription
LoomProviderEmbedProviderProvider implementation
getLoomIdFromUrl(url)(string) => stringExtract share ID
getLoomEmbedUrlFromId(id)(string) => stringBuild loom.com/embed/{id}
loomUrlRegexRegExpMatches Loom URLs

EdPuzzle

ExportTypeDescription
EdPuzzleProviderEmbedProviderProvider implementation
getEdPuzzleIdFromUrl(url)(string) => stringExtract media ID
getEdPuzzleEmbedUrlFromId(id)(string) => stringBuild embed URL
edPuzzleUrlRegexRegExpMatches EdPuzzle URLs

Wistia

ExportTypeDescription
WistiaProviderEmbedProviderProvider implementation
getWistiaIdFromUrl(url)(string) => stringExtract media ID
getWistiaEmbedUrlFromId(id)(string) => stringBuild fast.wistia.net/embed/iframe/{id}
wistiaUrlRegexRegExpMatches Wistia URLs

For usage patterns and examples, see the Examples page. To add custom providers, see Adding a new provider.