Replace Avatar in Template

In this guide, we will learn how to replace avatars within a template and generate a video.

Avatar Properties:

  • type: Type of the avatar, options: avatar, talking_photo.
  • character_id: Supports any ID returned from /v2/avatars.

Creating Template through the Interface

To create templates, visit HeyGen Template page and click on Create Template. After selecting the avatar you can then set variable and change variable names using the toolbar's API icon.

As you can see, we have assigned a variable name to our avatar element. In this tutorial, we will demonstrate how to change this avatar using the Template API.

Get a List of Your Templates

See detailed API reference
Using the following API request you will get a list of your templates that you have created in the HeyGen interface, obtain your template_id from here.

curl --location 'https://api.heygen.com/v2/templates' \
     --header 'accept: application/json' \
     --header 'x-api-key: <your-api-key>'
{
  "error": null,
  "data": {
    "templates": [
      {
        "template_id": "<template_id>",
        "name": "<name>",
        "thumbnail_image_url": "<thumbnail_image_url>"
      },
      ...
    ]
  }
}

Retrieve Template Elements

See detailed API reference
To retrieve template elements, use this API request. Get the variable you want to change from here.

curl --location 'https://api.heygen.com/v2/template/<template_id>' \
     --header 'accept: application/json' \
     --header 'x-api-key: <your-api-key>'
{
  "error": null,
  "data": {
    "variables": {
      "avatar_scene1": {
        "name": "avatar_scene1",
        "type": "character",
        "properties": {
          "character_id": "Briana_public_2_20240110",
          "type": "avatar"
        }
      }
    }
  }
}

Modify Template Elements and Generate Video

See detailed API reference
To change template elements and generate videos, you will need to submit an API request as follows. In the request body, specify the elements you want to modify by providing the appropriate object within the variables section.

curl --location 'https://api.heygen.com/v2/template/<template_id>/generate' \
     --header 'X-Api-Key: <your-api-key>' \
     --header 'Content-Type: application/json' \
     --data '{
  "test": true,
  "caption": false,
  "title": "New Video",
  "variables": {
    "avatar_scene1": {
      "name": "avatar_scene1",
      "type": "character",
      "properties": {
        "type": "avatar",
        "character_id": "Tyler-insuit-20220721"
      }
    }
  }
}'
{
  "error": null,
  "data": {
    "video_id": "<video_id>"
  }
}

You will receive a video_id after your request. To check the video's status and retrieve further details, you can use the video_status API endpoint.

And using the Template API, we changed our avatar and generated our new video, here is the before and after:

Conclusion

This guide has walked you through the process of replacing avatars within a template video using HeyGen's Template API. By following these steps, you can seamlessly modify template elements to create customized videos that suit your specific needs.