WebRTC doesn't have a dedicated SDK. On the web, connect directly with the browser's native JavaScript API. On other clients, connect through an open source WebRTC library or a third-party RTC service that supports the standard WebRTC protocol. The following example uses JavaScript on the web.
# pip install aiortc aiohttp certifiimport asyncio, aiohttp, ssl, certififrom aiortc import RTCPeerConnection, RTCConfiguration, RTCSessionDescriptionfrom aiortc.mediastreams import AudioStreamTrackAPI_KEY = "your-api-key"MODEL = "target-model"SIGNALING_URL = f"https://{{endpoint}}/api/v1/webrtc/realtime?model={MODEL}"async def connect(): pc = RTCPeerConnection(RTCConfiguration(iceServers=[])) # Add an audio track so that the Offer SDP contains m=audio (required by the server) pc.addTrack(AudioStreamTrack()) # Create a DataChannel to trigger SDP negotiation (the name is customizable; the server pushes events through a channel named "txt") pc.createDataChannel("oai-events") # SDP exchange: create an Offer and send it to the server offer = await pc.createOffer() await pc.setLocalDescription(offer) async with aiohttp.ClientSession() as session: async with session.post( SIGNALING_URL, ssl=ssl.create_default_context(cafile=certifi.where()), data=offer.sdp.encode("utf-8"), headers={ "Content-Type": "application/sdp", "Authorization": f"Bearer {API_KEY}", }, ) as resp: if not resp.ok: raise Exception(f"SDP exchange failed: {resp.status} {await resp.text()}") answer_sdp = await resp.text() print("=== Offer SDP ===") print(offer.sdp) print("=== Answer SDP ===") print(answer_sdp) # ICE connection setup completes automatically await pc.setRemoteDescription(RTCSessionDescription(sdp=answer_sdp, type="answer")) print("WebRTC connection established") return pc
A modern browser that supports WebRTC (such as Chrome, Edge, Firefox, or Safari).
Microphone permission granted to the browser.
The browser can't send the connection request directly to the server because of cross-origin security policies. Run the curl command in a terminal to establish the connection.