Replace Avatar in Template
Learn how to replace avatars within HeyGen templates using the Template API
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: Includes both Avatar and Talking Photo IDs. Supports any ID returned from List All Avatars (V2) endpoint.
Creating Template through the Interface
To create a new template:
- Go to the HeyGen Dashboard and open the Templates tab.
- Click Create Template. This opens the template editor.
- Click the Avatar tab and select any avatar of your choice.
- Use the API icon in the toolbar to assign an API variable to the image.
As you can see, we have assigned a variable name, i.e., avatar_scene_1 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
For instructions on listing your templates that you have created in the HeyGen interface and retrieving the template_id, see Get a List of Templates.
Retrieve Template Elements
To retrieve template elements you will need to submit an API request as follows. In the response, you’ll receive details of all variables defined in the template, in this case, the avatar element details, which can later be modified when creating videos.
curl --request GET \
--url https://api.heygen.com/v3/template/<template_id> \
--header 'accept: application/json' \
--header 'x-api-key: <your-api-key>'
{
"error": null,
"data": {
"version": "4",
"variables": {
"avatar_scene1": {
"name": "avatar_scene1",
"type": "character",
"properties": {
"character_id": "Briana_public_2_20240110",
"type": "avatar"
}
}
},
"scenes": [
{
"id": "<scene_id>",
"script": "",
"variables": {
"avatar_scene1": {
"name": "avatar_scene1",
"type": "character"
}
}
]
}
}
For a full list of parameters and complete response details, see detailed API Reference: Retrieve Template Details (V3).
Modify Template Elements and Generate Video
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 '{
"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>"
}
}If you want to explore all configurable options - including how to update multiple elements or adjust video metadata, see detailed API Reference: Generate Video from Template
You will receive a video_id after your request. To check the video's status and retrieve further details, you can use the Retrieve Video Status/Details API endpoint. You can view complete status response examples on the right-side Response panel of the API Reference page - open the Examples dropdown to view responses for different video statuses. You can select any example to preview the corresponding response body.
Status Descriptions
pending
The video is queued and waiting to start rendering.
waiting
The video is in waiting state.
processing
The video is currently rendering.
completed
The video has successfully rendered and is ready for download.
failed
The video could not be rendered due to an error (for example, if the duration exceeds plan limits).
And using the Template API, we changed our avatar and generated our new video.
Before:
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 modify template elements to create customized videos that suit your specific needs.
Updated about 4 hours ago
