Quick Start
Connecting
Getting Camera Access
Request access to the user’s camera using your platform’s camera API:Use the model’s
fps
, width
, and height
properties to ensure optimal performance.Establishing Connection
Connect to the Realtime API with your media stream:stream
(required) - MediaStream from get_camera_streammodel
(required) - Realtime model frommodels.realtime()
on_remote_stream
(required) - Callback that receives the transformed video streaminitial_state.prompt
(required) - Initial style prompttext
- Style descriptionenrich
- Whether to auto-enhance the prompt (default: True)
initial_state.mirror
(optional) - Enable mirror mode for front-facing cameras
Managing Prompts
Change the transformation style dynamically without reconnecting:prompt
(required) - Text description of desired styleoptions.enrich
(optional) - Whether to enhance the prompt (default: True)
Prompt enhancement uses Decart’s AI to expand simple prompts for better results. Disable it if you want full control over the exact prompt.
Camera Mirroring
Toggle horizontal mirroring, useful for front-facing cameras:Connection State
Monitor and react to connection state changes:Error Handling
Handle errors with the error event:INVALID_API_KEY
- API key is invalid or missingWEB_RTC_ERROR
- WebRTC connection failedMODEL_NOT_FOUND
- Specified model doesn’t existINVALID_INPUT
- Invalid input parameters
Session Management
Access the current session ID:Cleanup
Always disconnect when done to free up resources:Failing to disconnect can leave WebRTC connections open and waste resources.
Complete Example
Here’s a full application with all features:Best Practices
Use model properties for video constraints
Use model properties for video constraints
Always use the model’s
fps
, width
, and height
properties when requesting camera access to ensure optimal performance and compatibility.Enable prompt enrichment
Enable prompt enrichment
For best results, keep
enrich: True
(default) to let Decart’s AI enhance your prompts. Only disable it if you need exact prompt control.Handle connection state changes
Handle connection state changes
Always listen to
connection_change
events to update your UI and handle reconnection logic gracefully.Clean up properly
Clean up properly
Always call
disconnect()
and stop media streams when done to avoid memory leaks and unnecessary resource usage.Use mirror mode for front cameras
Use mirror mode for front cameras
Enable mirror mode when using front-facing cameras to match user expectations from selfie/video call experiences.
API Reference
client.realtime.connect(stream, options)
Connects to the realtime transformation service.
Parameters:
stream: MediaStream
- MediaStream from get_camera_streamoptions.model: ModelDefinition
- Realtime model frommodels.realtime()
options.on_remote_stream: Callable[[MediaStream], None]
- Callback for transformed video streamoptions.initial_state.prompt: dict
- Initial transformation prompttext: str
- Style description (required)enrich: bool
- Whether to enhance the prompt (default: True)
options.initial_state.mirror: bool
- Enable mirror mode (default: False)
RealtimeClient
- Connected realtime client instance
realtime_client.set_prompt(prompt, options?)
Changes the transformation style.
Parameters:
prompt: str
- Text description of desired styleoptions.enrich: bool
- Whether to enhance the prompt (default: True)
realtime_client.set_mirror(enabled)
Toggles video mirroring.
Parameters:
enabled: bool
- Whether to enable mirror mode
realtime_client.is_connected()
Check if currently connected.
Returns: bool
realtime_client.get_connection_state()
Get current connection state.
Returns: Literal["connected", "connecting", "disconnected"]
realtime_client.session_id
The ID of the current realtime inference session.
Type: str
realtime_client.disconnect()
Closes the connection and cleans up resources.
Events
connection_change
Fired when connection state changes.
Callback: (state: Literal["connected", "connecting", "disconnected"]) -> None
error
Fired when an error occurs.
Callback: (error: DecartSDKError) -> None