Overview
Live Face Swap API provides real-time face swap functionality, supporting real-time face swap operations during live streaming.Get Access Token
Before calling other APIs, you need to obtain an access token first.| Parameter | Value | Description |
|---|---|---|
| Content-Type | application/json | Request content type |
| Parameter | Type | Description |
|---|---|---|
| clientId | String | Client ID |
| clientSecret | String | Client secret |
Face Detection
Detect faces in media files (image, video, or GIF) and get facial landmarks, face regions, and cropped face URLs.| Parameter | Value | Description |
|---|---|---|
| x-api-key | API Key | Your API Key used for request authorization. If both Authorization and x-api-key have values, Authorization will be used first and x-api-key will be discarded. |
| Authorization | Bearer {token} | Your API Key used for request authorization.Get Token. |
| Content-Type | application/json | Request content type |
| Parameter | Type | Description |
|---|---|---|
| url | String | Media URL for face detection (supports image, video, and GIF) |
| single_face | Boolean | Whether to detect single face only |
| return_face_url | Boolean | Whether to return the cropped face URL |
| num_frames | Integer | Number of frames to detect for videos (maximum 24 frames) |
| Parameter | Type | Description |
|---|---|---|
| error_code | int | Error code (0: success) |
| error_msg | String | Error message |
| faces_obj | Object | Object containing detected faces information, keyed by face index |
| ↳ landmarks | Array | Facial landmarks coordinates array |
| ↳ landmarks_str | Array | Facial landmarks coordinates string array |
| ↳ region | Array | Face region coordinates |
| ↳ frame_time | Float | Frame time in seconds (for video/gif) |
| ↳ crop_region | Array | Cropped face region coordinates |
| ↳ crop_landmarks | Array | Cropped facial landmarks coordinates string array |
| ↳ face_urls | Array | URLs of the cropped face images |
Create Real-time Face Swap Session
Create a new real-time face swap session.| Parameter | Value | Description |
|---|---|---|
| x-api-key | API Key | Your API Key used for request authorization. If both Authorization and x-api-key have values, Authorization will be used first and x-api-key will be discarded. |
| Authorization | Bearer {token} | Your API Key used for request authorization.Get Token. |
| Content-Type | application/json | Request content type |
| Parameter | Type | Description |
|---|---|---|
| sourceImage | Array | Source image information array, each element contains path and opts properties |
| Parameter | Type | Description |
|---|---|---|
| code | int | API response business status code (1000: success) |
| msg | String | API response status information |
| data | Object | Response data object |
faceswap_status:- 1: Queuing
- 2: Processing (when this value is 2, the frontend can connect to Agora’s server)
- 3: Success
- 4: Failed
Update Real-time Face Swap Session
Update existing real-time face swap session configuration.| Parameter | Value | Description |
|---|---|---|
| x-api-key | API Key | Your API Key used for request authorization. If both Authorization and x-api-key have values, Authorization will be used first and x-api-key will be discarded. |
| Authorization | Bearer {token} | Your API Key used for request authorization.Get Token. |
| Content-Type | application/json | Request content type |
| Parameter | Type | Description |
|---|---|---|
| _id | String | Session ID |
| sourceImage | Array | Source image information array, each element contains path and opts properties |
Close Real-time Face Swap Session
Close the specified real-time face swap session.| Parameter | Value | Description |
|---|---|---|
| x-api-key | API Key | Your API Key used for request authorization. If both Authorization and x-api-key have values, Authorization will be used first and x-api-key will be discarded. |
| Authorization | Bearer {token} | Your API Key used for request authorization.Get Token. |
| Content-Type | application/json | Request content type |
| Parameter | Type | Description |
|---|---|---|
| _id | String | Session ID |
Code Examples
Response Code Description
Note: If thecodevalue in the response is not1000, the request has failed or is incorrect.
| Parameter | Value | Description |
|---|---|---|
| code | 1000 | Success |
| code | 1003 | Parameter error or parameter cannot be empty |
| code | 1101 | Invalid authorization or the request token has expired |
| code | 1102 | Authorization cannot be empty |
| code | 1104 | Insufficient quota |
Important Notes
- Resource Validity: Generated resources are valid for 7 days, please save them promptly
- Face Detection: Use the face-detect API to get face landmarks coordinates before creating face swap sessions
- Status Monitoring: After creating a session, you need to monitor the
faceswap_statusstatus - Real-time Connection: When the status is 2, you can connect to Agora’s server for real-time face swap
- Session Management: Please close sessions promptly after use to release resources
- Error Handling: Please handle API error codes and error messages properly
Push and pull stream Demo References
For implementing real-time video communication with Agora SDK, you can refer to the following resources:Basic Video Calling Demo Parameter Description
As shown in the figure above, channel_id, front_user_id, and front_rtc_token correspond to the Channel, User ID, and Token input fields on the page respectively.
These parameters can be obtained after creating a session through the Live Face Swap API. After filling them in, you can experience push/pull streaming and real-time face swap effects.
Demo Page
Source Code
- GitHub Repository: https://github.com/AgoraIO/API-Examples-Web/tree/main/src/example/basic/basicVideoCall
Recommended Track Configuration
For optimal performance in live face swap scenarios, it’s recommended to use the following track configurations:Audio Track Configuration
Video Track Configuration
- Audio: Using
music_standardencoder for better audio quality - Video: Fixed frame rate at 20fps with controlled bitrate for stable performance
- Resolution: 640x480 resolution suitable for face swap processing