Skip to main content
The main function of the Face Swap API is to swap the face in the target image with the face in the source image by inputting one source image and one target image. This document will provide detailed instructions for integrating the Face Swap API, helping you easily leverage the powerful features of this API. With the Face Swap API, you can easily achieve the face swap from the target image to the source image.

Application Process

To use the Face Swap API, you need to first apply for the corresponding service on the application page Face Swap API. After entering the page, click the “Acquire” button, as shown in the image below: Application Page If you are not logged in or registered, you will be automatically redirected to the login page inviting you to register and log in. After logging in or registering, you will be automatically returned to the current page. The first application will grant you a free quota, allowing you to use the API for free.

Request Example

We will use two images as examples to demonstrate how to use the API. Assume the source image is as shown below:

The target image is:

Next, we will demonstrate how to swap the face in the target image with the face in the source image.

Setting Request Headers and Request Body

Request Headers include:
  • accept: Specifies that the response result should be in JSON format, set to application/json.
  • authorization: The key to call the API, which can be selected directly after application.
Request Body includes:
  • source_image_url: The link to the uploaded source image.
  • target_image_url: The link to the uploaded target image.
  • timeout: Optional, the processing timeout (in seconds), will return directly on timeout.
Set as shown in the image below:

Code Example

You can see that various language codes have been automatically generated on the right side of the page, as shown in the image below:

Some code examples are as follows:

CURL

curl -X POST 'https://api.acedata.cloud/face/swap' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "source_image_url": "https://cdn.acedata.cloud/n1lmd8.png",
  "target_image_url": "https://cdn.acedata.cloud/3np95r.png"
}'

Python

import requests

url = "https://api.acedata.cloud/face/swap"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "source_image_url": "https://cdn.acedata.cloud/n1lmd8.png",
    "target_image_url": "https://cdn.acedata.cloud/3np95r.png"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

Response Example

Upon successful request, the API will return the result information of the swapped face image. For example:
{
  "image_url": "https://platform.cdn.acedata.cloud/face/4b13bdeb-1b19-4ea5-bddf-c2da14ba72e3.png",
  "image_width": 2008,
  "image_height": 1942,
  "image_size": 4006213,
  "task_id": "4b13bdeb-1b19-4ea5-bddf-c2da14ba72e3"
}
You can see that the result contains an image_url field, which includes the link to the image after swapping the face in the target image with the face in the source image. Other information is shown in the image below:
  • image_url, the link to the generated image.
  • image_width, the width of the generated image.
  • image_height, the height of the generated image.
  • image_size, the size of the generated image.
  • task_id, the ID of the current generation task.
The generated image result is:

It is clear that the image has successfully swapped the face in the target image with the face in the source image.

Asynchronous Callback

Since the generation time for Face Swap is relatively long, approximately 1-2 minutes, if the API does not respond for a long time, the HTTP request will keep the connection open, leading to additional system resource consumption. Therefore, this API also provides support for asynchronous callbacks. The overall process is: when the client initiates a request, an additional callback_url field is specified. After the client initiates the API request, the API will immediately return a result containing a task_id field, representing the current task ID. When the task is completed, the generated Face Swap result will be sent to the client-specified callback_url in the form of a POST JSON, which also includes the task_id field, allowing the task result to be associated by ID. Let’s understand how to operate specifically through an example. First, the Webhook callback is a service that can receive HTTP requests, and developers should replace it with the URL of their own HTTP server. For convenience, we will use a public Webhook sample site https://webhook.site/, where you can open the site to get a Webhook URL, as shown in the image below: Copy this URL, and it can be used as a Webhook. The sample here is https://webhook.site/3b76eba5-4573-432a-b607-3000b87afc06. Next, we can set the callback_url field to the above Webhook URL and fill in the corresponding parameters, as shown in the image below:

Clicking run, you will immediately receive a result, as follows:
{
  "task_id": "9cba9d36-3b14-43c9-85b6-86f6dfc3b096"
}
After a moment, you can observe the generated Face Swap result at https://webhook.site/3b76eba5-4573-432a-b607-3000b87afc06, as shown in the image below: The content is as follows:
{
  "success": true,
  "task_id": "9cba9d36-3b14-43c9-85b6-86f6dfc3b096",
  "image_url": "https://platform.cdn.acedata.cloud/face/9cba9d36-3b14-43c9-85b6-86f6dfc3b096.png",
  "image_width": 2008,
  "image_height": 1942,
  "image_size": 4006481
}
You can see that the result contains a task_id field, and the other fields are similar to those mentioned above, allowing the task to be associated through this field.

Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:
  • 400 token_mismatched: Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented: Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token: Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests: Too many requests, you have exceeded the rate limit.
  • 500 api_error: Internal server error, something went wrong on the server.

Error Response Example

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

Conclusion

Through this document, you have learned how to use the Face Swap API to swap the face of the target image with the face of the source image. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.