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-live.md
1# Stream Live Streaming API23Live input creation, status checking, simulcast, and WebRTC streaming.45## Create Live Input67### Using Cloudflare SDK89```typescript10import Cloudflare from 'cloudflare';1112const client = new Cloudflare({ apiToken: env.CF_API_TOKEN });1314const liveInput = await client.stream.liveInputs.create({15account_id: env.CF_ACCOUNT_ID,16recording: { mode: 'automatic', timeoutSeconds: 30 },17deleteRecordingAfterDays: 3018});1920// Returns: { uid, rtmps, srt, webRTC }21```2223### Raw fetch API2425```typescript26async function createLiveInput(accountId: string, apiToken: string) {27const response = await fetch(28`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/live_inputs`,29{30method: 'POST',31headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' },32body: JSON.stringify({33recording: { mode: 'automatic', timeoutSeconds: 30 },34deleteRecordingAfterDays: 3035})36}37);38const { result } = await response.json();39return {40uid: result.uid,41rtmps: { url: result.rtmps.url, streamKey: result.rtmps.streamKey },42srt: { url: result.srt.url, streamId: result.srt.streamId, passphrase: result.srt.passphrase },43webRTC: result.webRTC44};45}46```4748## Check Live Status4950```typescript51async function getLiveStatus(accountId: string, liveInputId: string, apiToken: string) {52const response = await fetch(53`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/live_inputs/${liveInputId}`,54{ headers: { 'Authorization': `Bearer ${apiToken}` } }55);56const { result } = await response.json();57return {58isLive: result.status?.current?.state === 'connected',59recording: result.recording,60status: result.status61};62}63```6465## Simulcast (Live Outputs)6667### Create Output6869```typescript70async function createLiveOutput(71accountId: string, liveInputId: string, apiToken: string,72outputUrl: string, streamKey: string73) {74return fetch(75`https://api.cloudflare.com/client/v4/accounts/${accountId}/stream/live_inputs/${liveInputId}/outputs`,76{77method: 'POST',78headers: { 'Authorization': `Bearer ${apiToken}`, 'Content-Type': 'application/json' },79body: JSON.stringify({80url: `${outputUrl}/${streamKey}`,81enabled: true,82streamKey // For platforms like YouTube, Twitch83})84}85).then(r => r.json());86}87```8889### Example: Simulcast to YouTube + Twitch9091```typescript92const liveInput = await createLiveInput(accountId, apiToken);9394// Add YouTube output95await createLiveOutput(96accountId, liveInput.uid, apiToken,97'rtmp://a.rtmp.youtube.com/live2',98'your-youtube-stream-key'99);100101// Add Twitch output102await createLiveOutput(103accountId, liveInput.uid, apiToken,104'rtmp://live.twitch.tv/app',105'your-twitch-stream-key'106);107```108109## WebRTC Streaming (WHIP/WHEP)110111### Browser to Stream (WHIP)112113```typescript114async function startWebRTCBroadcast(liveInputId: string) {115const pc = new RTCPeerConnection();116117// Add local media tracks118const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });119stream.getTracks().forEach(track => pc.addTrack(track, stream));120121// Create offer122const offer = await pc.createOffer();123await pc.setLocalDescription(offer);124125// Send to Stream via WHIP126const response = await fetch(127`https://customer-<CODE>.cloudflarestream.com/${liveInputId}/webRTC/publish`,128{129method: 'POST',130headers: { 'Content-Type': 'application/sdp' },131body: offer.sdp132}133);134135const answer = await response.text();136await pc.setRemoteDescription({ type: 'answer', sdp: answer });137}138```139140### Stream to Browser (WHEP)141142```typescript143async function playWebRTCStream(videoId: string) {144const pc = new RTCPeerConnection();145146pc.addTransceiver('video', { direction: 'recvonly' });147pc.addTransceiver('audio', { direction: 'recvonly' });148149const offer = await pc.createOffer();150await pc.setLocalDescription(offer);151152const response = await fetch(153`https://customer-<CODE>.cloudflarestream.com/${videoId}/webRTC/play`,154{155method: 'POST',156headers: { 'Content-Type': 'application/sdp' },157body: offer.sdp158}159);160161const answer = await response.text();162await pc.setRemoteDescription({ type: 'answer', sdp: answer });163164return pc;165}166```167168## Recording Settings169170| Mode | Behavior |171|------|----------|172| `automatic` | Record all live streams |173| `off` | No recording |174| `timeoutSeconds` | Stop recording after N seconds of inactivity |175176```typescript177const recordingConfig = {178mode: 'automatic',179timeoutSeconds: 30, // Auto-stop 30s after stream ends180requireSignedURLs: true, // Require token for VOD playback181allowedOrigins: ['https://yourdomain.com']182};183```184185## In This Reference186187- [README.md](./README.md) - Overview and quick start188- [api.md](./api.md) - On-demand video APIs189- [configuration.md](./configuration.md) - Setup and config190- [patterns.md](./patterns.md) - Full-stack flows, best practices191- [gotchas.md](./gotchas.md) - Error codes, troubleshooting192193## See Also194195- [workers](../workers/) - Deploy live APIs in Workers196