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/realtime-sfu/api.md
1# API Reference23## Authentication45```bash6curl -X POST 'https://rtc.live/v1/apps/${CALLS_APP_ID}/sessions/new' \7-H "Authorization: Bearer ${CALLS_APP_SECRET}"8```910## Core Concepts1112**Sessions:** PeerConnection to Cloudflare edge13**Tracks:** Media/data channels (audio/video/datachannel)14**No rooms:** Build presence via track sharing1516## Client Libraries1718**PartyTracks (Recommended):** Observable-based client library for production use. Handles device changes, network switches, ICE restarts automatically. Push/pull API with React hooks. See patterns.md for full examples.1920```bash21npm install partytracks @cloudflare/calls22```2324**Raw API:** Direct HTTP + WebRTC for custom requirements (documented below).2526## Endpoints2728### Create Session29```http30POST /v1/apps/{appId}/sessions/new31→ {sessionId, sessionDescription}32```3334### Add Track (Publish)35```http36POST /v1/apps/{appId}/sessions/{sessionId}/tracks/new37Body: {38sessionDescription: {sdp, type: "offer"},39tracks: [{location: "local", trackName: "my-video"}]40}41→ {sessionDescription, tracks: [{trackName}]}42```4344### Add Track (Subscribe)45```http46POST /v1/apps/{appId}/sessions/{sessionId}/tracks/new47Body: {48tracks: [{49location: "remote",50trackName: "remote-track-id",51sessionId: "other-session-id"52}]53}54→ {sessionDescription} (server offer)55```5657### Renegotiate58```http59PUT /v1/apps/{appId}/sessions/{sessionId}/renegotiate60Body: {sessionDescription: {sdp, type: "answer"}}61```6263### Close Tracks64```http65PUT /v1/apps/{appId}/sessions/{sessionId}/tracks/close66Body: {tracks: [{trackName}]}67→ {requiresImmediateRenegotiation: boolean}68```6970### Get Session71```http72GET /v1/apps/{appId}/sessions/{sessionId}73→ {sessionId, tracks: TrackMetadata[]}74```7576## TypeScript Types7778```typescript79interface TrackMetadata {80trackName: string;81location: "local" | "remote";82sessionId?: string; // For remote tracks83mid?: string; // WebRTC mid84}85```8687## WebRTC Flow8889```typescript90// 1. Create PeerConnection91const pc = new RTCPeerConnection({92iceServers: [{urls: 'stun:stun.cloudflare.com:3478'}]93});9495// 2. Add tracks96const stream = await navigator.mediaDevices.getUserMedia({video: true, audio: true});97stream.getTracks().forEach(track => pc.addTrack(track, stream));9899// 3. Create offer100const offer = await pc.createOffer();101await pc.setLocalDescription(offer);102103// 4. Send to backend → Cloudflare API104const response = await fetch('/api/new-session', {105method: 'POST',106body: JSON.stringify({sdp: offer.sdp})107});108109// 5. Set remote answer110const {sessionDescription} = await response.json();111await pc.setRemoteDescription(sessionDescription);112```113114## Publishing115116```typescript117const offer = await pc.createOffer();118await pc.setLocalDescription(offer);119120const res = await fetch(`/api/sessions/${sessionId}/tracks`, {121method: 'POST',122body: JSON.stringify({123sdp: offer.sdp,124tracks: [{location: 'local', trackName: 'my-video'}]125})126});127128const {sessionDescription, tracks} = await res.json();129await pc.setRemoteDescription(sessionDescription);130const publishedTrackId = tracks[0].trackName; // Share with others131```132133## Subscribing134135```typescript136const res = await fetch(`/api/sessions/${sessionId}/tracks`, {137method: 'POST',138body: JSON.stringify({139tracks: [{location: 'remote', trackName: remoteTrackId, sessionId: remoteSessionId}]140})141});142143const {sessionDescription} = await res.json();144await pc.setRemoteDescription(sessionDescription);145146const answer = await pc.createAnswer();147await pc.setLocalDescription(answer);148149await fetch(`/api/sessions/${sessionId}/renegotiate`, {150method: 'PUT',151body: JSON.stringify({sdp: answer.sdp})152});153154pc.ontrack = (event) => {155const [remoteStream] = event.streams;156videoElement.srcObject = remoteStream;157};158```159