Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/stream/api.md
1# Stream API Reference23Upload, playback, live streaming, and management APIs.45## Upload APIs67### Direct Creator Upload (Recommended)89**Backend: Create upload URL (SDK)**10```typescript11import Cloudflare from 'cloudflare';1213const client = new Cloudflare({ apiToken: env.CF_API_TOKEN });1415const uploadData = await client.stream.directUpload.create({16account_id: env.CF_ACCOUNT_ID,17maxDurationSeconds: 3600,18requireSignedURLs: true,19meta: { creator: 'user-123' }20});21// Returns: { uploadURL: string, uid: string }22```2324**Frontend: Upload file**25```typescript26async function uploadVideo(file: File, uploadURL: string) {27const formData = new FormData();28formData.append('file', file);29return fetch(uploadURL, { method: 'POST', body: formData }).then(r => r.json());30}31```3233### Upload from URL3435```typescript36const video = await client.stream.copy.create({37account_id: env.CF_ACCOUNT_ID,38url: 'https://example.com/video.mp4',39meta: { name: 'My Video' },40requireSignedURLs: false41});42```4344## Playback APIs4546### Embed Player (iframe)4748```html49<iframe50src="https://customer-<CODE>.cloudflarestream.com/<VIDEO_ID>/iframe?autoplay=true&muted=true"51style="border: none;" height="720" width="1280"52allow="accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;"53allowfullscreen="true"54></iframe>55```5657### HLS/DASH Manifest URLs5859```typescript60// HLS61const hlsUrl = `https://customer-<CODE>.cloudflarestream.com/${videoId}/manifest/video.m3u8`;6263// DASH64const dashUrl = `https://customer-<CODE>.cloudflarestream.com/${videoId}/manifest/video.mpd`;65```6667### Thumbnails6869```typescript70// At specific time (seconds)71const thumb = `https://customer-<CODE>.cloudflarestream.com/${videoId}/thumbnails/thumbnail.jpg?time=10s`;7273// By percentage74const thumbPct = `https://customer-<CODE>.cloudflarestream.com/${videoId}/thumbnails/thumbnail.jpg?time=50%`;7576// Animated GIF77const gif = `https://customer-<CODE>.cloudflarestream.com/${videoId}/thumbnails/thumbnail.gif`;78```7980## Signed URLs8182```typescript83// Low volume (<1k/day): Use API84async function getSignedToken(accountId: string, videoId: string, apiToken: string) {85const response = await fetch(86`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/${videoId}/token`,87{88method: 'POST',89headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' },90body: JSON.stringify({91exp: Math.floor(Date.now() / 1000) + 3600,92accessRules: [{ type: 'ip.geoip.country', action: 'allow', country: ['US'] }]93})94}95);96return (await response.json()).result.token;97}9899// High volume: Self-sign with RS256 JWT (see "Self-Sign JWT" in patterns.md)100```101102## Captions & Clips103104### Upload Captions105106```typescript107async function uploadCaption(108accountId: string, videoId: string, apiToken: string,109language: string, captionFile: File110) {111const formData = new FormData();112formData.append('file', captionFile);113return fetch(114`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/${videoId}/captions/${language}`,115{116method: 'PUT',117headers: { 'Authorization': `Bearer ${apiToken}` },118body: formData119}120).then(r => r.json());121}122```123124### Generate AI Captions125126```typescript127// TODO: Requires Workers AI integration - see workers-ai reference128async function generateAICaptions(accountId: string, videoId: string, apiToken: string) {129return fetch(130`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/${videoId}/captions/generate`,131{132method: 'POST',133headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' },134body: JSON.stringify({ language: 'en' })135}136).then(r => r.json());137}138```139140### Clip Video141142```typescript143async function clipVideo(144accountId: string, videoId: string, apiToken: string,145startTime: number, endTime: number146) {147return fetch(148`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/clip`,149{150method: 'POST',151headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' },152body: JSON.stringify({153clippedFromVideoUID: videoId,154startTimeSeconds: startTime,155endTimeSeconds: endTime156})157}158).then(r => r.json());159}160```161162## Video Management163164```typescript165// List videos166const videos = await client.stream.videos.list({167account_id: env.CF_ACCOUNT_ID,168search: 'keyword' // optional169});170171// Get video details172const video = await client.stream.videos.get(videoId, {173account_id: env.CF_ACCOUNT_ID174});175176// Update video177await client.stream.videos.update(videoId, {178account_id: env.CF_ACCOUNT_ID,179meta: { title: 'New Title' },180requireSignedURLs: true181});182183// Delete video184await client.stream.videos.delete(videoId, {185account_id: env.CF_ACCOUNT_ID186});187```188189## In This Reference190191- [README.md](./README.md) - Overview and quick start192- [configuration.md](./configuration.md) - Setup and config193- [api-live.md](./api-live.md) - Live streaming APIs (RTMPS/SRT/WebRTC)194- [patterns.md](./patterns.md) - Full-stack flows, best practices195- [gotchas.md](./gotchas.md) - Error codes, troubleshooting196197## See Also198199- [workers](../workers/) - Deploy Stream APIs in Workers200