The HeyGen Hub
+00:00 GMT
Groups
/
API Builders
/
Content
API & Integrations
December 12, 2024 · Last updated on December 13, 2024

How to Use HeyGen’s API: A Step-by-Step Walkthrough

How to Use HeyGen’s API: A Step-by-Step Walkthrough
# API
# Personalized Video

Creating personalized birthday videos using templates and integrating OpenAI to generate dynamic scripts

How to Use HeyGen’s API: A Step-by-Step Walkthrough

HeyGen’s API empowers you to automate video creation, personalize content at scale, and integrate avatars into your projects. In this guide, we’ll demonstrate two examples based on Alec’s webinar:

1) Creating Personalized Birthday Videos Using Templates

2) Integrating OpenAI to Generate Dynamic Scripts


Let’s get started!

Step 1: Access the HeyGen API Documentation

Visit docs.heygen.com to access HeyGen’s official API documentation. This contains:

  1. Setup guides for beginners.
  2. Detailed API references for developers.

If you’re unfamiliar with APIs, don’t worry—this guide will walk you through the essentials.

Step 2: Create a Video Template

Templates allow you to design reusable videos with customizable elements (variables). These templates are central to automating video creation.

How to Create a Template:

  1. Log into HeyGen and navigate to the Templates section.
  2. Click Create Template and design your video:
  3. Add your avatar, background, and any static elements (e.g., text, images).
  4. Define Variables for dynamic content:
  5. Select an element (e.g., text or image).
  6. Click the API button to assign a variable name (e.g., name, message, background).
  7. Save the template and copy its Template ID by clicking the three dots next to the template.

Step 3: Generate Videos Using the Template API

Python Example:

Below is a corrected script to dynamically generate a personalized birthday video:

import requests

# API and Template Information
api_key = "YOUR_API_KEY"
template_id = "YOUR_TEMPLATE_ID"
generate_url = f"https://api.heygen.com/v2/template/{template_id}/generate"

# Request Headers
headers = {"Accept": "application/json", "X-API-KEY": api_key}

# Payload with Dynamic Variables
payload = {
"title": "Birthday Greeting for Alec",
"variables": {
"name": {"name": "name", "type": "text", "properties": {"content": "Alec"}},
"message": {
"name": "message",
"type": "text",
"properties": {"content": "Happy birthday, Alec! Wishing you a fantastic day."},
},
},
}

# API Request
response = requests.post(generate_url, json=payload, headers=headers)

# Handle Response
if response.status_code == 200:
video_data = response.json()
print("Video URL:", video_data["video_url"])
else:
print("Error:", response.json())


 Steps to Follow:

1.Replace YOUR_API_KEY with your API key (found under Settings > API in your HeyGen account).

2.Replace YOUR_TEMPLATE_ID with your Template ID from Step 2.

3.Customize the name and message fields in the payload.

4.Run the script to generate the video. The video URL will appear in the response.


Step 4: Add OpenAI for Dynamic Script Creation

Integrating OpenAI allows you to generate unique scripts for each video, making the content even more personalized.

Python Example with OpenAI:

import requests
import openai

# OpenAI API Key
openai.api_key = "YOUR_OPENAI_API_KEY"

# Generate Dynamic Script with OpenAI
response = openai.Completion.create(
engine="text-davinci-003",
prompt="Write a funny birthday message for Alec.",
max_tokens=50,
)
dynamic_message = response.choices[0].text.strip()

# HeyGen API Details
api_key = "YOUR_API_KEY"
template_id = "YOUR_TEMPLATE_ID"
generate_url = f"https://api.heygen.com/v2/template/{template_id}/generate"

# Request Headers
headers = {"Accept": "application/json", "X-API-KEY": api_key}

# Payload with OpenAI-Generated Script
payload = {
"title": "Dynamic Birthday Greeting",
"variables": {
"name": {"name": "name", "type": "text", "properties": {"content": "Alec"}},
"message": {
"name": "message",
"type": "text",
"properties": {"content": dynamic_message},
},
},
}

# API Request
response = requests.post(generate_url, json=payload, headers=headers)

# Handle Response
if response.status_code == 200:
video_data = response.json()
print("Video URL:", video_data["video_url"])
else:
print("Error:", response.json())


Steps to Follow:

1.Replace YOUR_OPENAI_API_KEY and YOUR_API_KEY with your respective API keys.

2.Update the OpenAI prompt for the type of message you’d like (e.g., professional, humorous).

3.Run the script to dynamically generate and personalize the video.


Step 5: Check Video Status and Download

After generating the video, you can check its status and download it.

Code Snippet:

import time

# Video Status Check
video_status_url = f"https://api.heygen.com/v1/video_status.get?video_id={video_id}"

while True:
response = requests.get(video_status_url, headers=headers)
status = response.json()["data"]["status"]
if status == "completed":
video_url = response.json()["data"]["video_url"]
print("Video Completed! URL:", video_url)
break
elif status in ["processing", "pending"]:
print("Video is still processing...")
time.sleep(5) # Wait for 5 seconds before checking again
elif status == "failed":
print("Video generation failed.")
break

Resources and Support

  1. API Documentation: HeyGen API Docs
  2. Community Hub: Join our community to ask questions, share ideas, and get inspired.
  3. Contact Support: Email us at [email protected] for technical assistance.

We hope this walkthrough empowers you to explore HeyGen’s API capabilities. Happy creating! 🎉

Table Of Contents
Dive in
Related
external
HeyGen API Postman collection
Dec 21st, 2024 Views 12
external
HeyGen API Postman collection
Dec 21st, 2024 Views 12
external
HeyGen API developer hub
Dec 11th, 2024 Views 64