Create Image FaceSwap

FaceSwap API can be used to generate convincing videos or images by swapping faces or altering visual elements. This technology can be applied in various fields, such as entertainment, advertising and education.

Upload an Image for FaceSwap

Start by uploading a local image file to HeyGen using the upload endpoint. Use the following API call:

curl -XPOST https://upload.heygen.com/v1/asset
     -H 'x-api-key: <your-api-key>'
     --data-binary '@<local file path>'
     -H 'Content-Type: image/jpeg'
{
  "code": 100,
  "data": {
    "id": "<asset_id>",
    "name": "<name>",
    "file_type": "image",
    "folder_id": "",
    "meta": null,
    "created_ts": 1686919825
  },
  "msg": null
}

The response will provide you with an id, representing the asset ID of the uploaded image.

You only need to specify the content type of the video, which should be image/jpeg for JPEG files or image/png for PNG files. The API will respond with an id representing your asset.

File typeContent type
JPEGimage/jpeg
PNGimage/png

Analyze Your Image

Kick things off by analyzing your base image. You'll need to use the POST method at the following endpoint:‍

curl -XPOST https://api.heygen.com/v1/image/analyze
     -H 'x-api-key: <your-api-key>'
     -d '{"asset_id": "<asset_id>"}'
     -H 'Content-Type: application/json'
{
  "code": 100,
  "data": {
    "faces": {
      "<the returned face id in the asset>": "<the face preview image url>"
    }
  },
  "msg": null
}

In the request body, input the asset_id, which represents the image you want to analyze. This API call returns a mapping of face IDs to face thumbnails in the image.

Upload a Replacement Face

Next, upload the face you want to swap into the image. In the request body, specify the local file path of the image you want to upload. Use the following API call:

curl -XPOST https://upload.heygen.com/v1/face
     -H 'x-api-key: <your-api-key>'
     --data-binary '@<local_file_path>'
     -H 'Content-Type: image/jpeg'
{
  "code": 100,
  "data": {
    "face_id": "<face_id>",
    "face_url": "<face_url>"
  },
  "msg": null
}

The response will provide a unique face_id and the face_url of the uploaded face.

Execute FaceSwap

See detailed API reference
With the faces ready, it's time to perform the actual face swap. In the request body, provide the asset_id of your initial image and the faceswap_mapping, which is a JSON mapping from the face in the image to the newly uploaded face. Make a POST call to:

curl -XPOST https://api.heygen.com/v1/image/faceswap
     -H 'x-api-key: <your-api-key>'
     -d '{"asset_id":<the asset id>, "faceswap_mapping": {<face id in the asset>: <face id of the uploaded face in the previous step>}}'
     -H 'Content-Type: application/json'
{"code": 100,
 "data": {
   "asset_id": <faceswapped asset id>,
   "output_url": <output asset image url>
 },
 "msg": null
}

The call returns a new asset_id and the output_url of the image with the swapped face.

Conclusion

By following the steps outlined in this guide, you can harness the power of the HeyGen Face Swap API to create captivating videos and images by swapping faces and altering visual elements. This technology opens up a world of possibilities for various applications in entertainment, advertising and education.