Discussions

Ask a Question
Back to all

Keep getting 404 in spite of correct endpoint and payload. Time wasterd for simple jobs

import requests
import time
BASE_URL = "https://api.heygen.com"
url = "https://api.heygen.com/v2/video/generate"

API_KEY = "xxx"

HEADERS = {
"accept": "application/json",
"content-type": "application/json",
"x-api-key": API_KEY
}

AVATAR_ID = "a549a2caa95a4289adeb5959cba8f061"
VOICE_ID = "ff3c1339e5f3481c9dd60ab7efd281e9"

def create_avatar_video(avatar_id: str, voice_id: str, script: str) -> str:
"""
Calls /v2/video/generate and returns video_id.
"""
payload = {
"video_inputs": [
{
"character": {
"type": "avatar",
"avatar_id": AVATAR_ID,
"avatar_style": "normal",
},
"voice": {
"type": "text",
"input_text": script,
"voice_id": VOICE_ID,
},
# Simplest background: solid color
"background": {
"type": "color",
"value": "#FFFFFF"
},
}
],
# Optional: set resolution (720p is usually the free-tier limit)
"dimension": {
"width": 1280,
"height": 720
},
# Optional title, folder, etc. can be added here
}

resp = requests.post(
    url,
    headers=HEADERS,
    json=payload,
    timeout=60,
)
resp.raise_for_status()
data = resp.json()
# Typical response: {"code":100,"data":{"video_id":"..."}, "message":"Success"}
video_id = data["data"]["video_id"]
print("Created video_id:", video_id)
return video_id

def wait_for_video(video_id: str, poll_interval: int = 10) -> dict:
"""
Polls /v1/video_status.get until status is 'completed' or 'failed'.
Returns the final JSON.
"""
status_url = f"{BASE_URL}/v1/video_status.get"

while True:
    resp = requests.get(
        status_url,
        headers=HEADERS,
        params={"video_id": video_id},
        timeout=60,
    )
    resp.raise_for_status()
    data = resp.json()
    status = data.get("data", {}).get("status")
    print("Status:", status)

    if status in {"completed", "failed"}:
        return data

    time.sleep(poll_interval)

def main():
# TODO: replace with IDs from your account

SCRIPT = (
    "Welcome to FN6007 for derivatives modeling. "
    "In this course, we will talk about how derivatives are used in the financial industry."
)

video_id = create_avatar_video(AVATAR_ID, VOICE_ID, SCRIPT)
result = wait_for_video(video_id)

# The status response includes temporary URLs (valid ~7 days) :contentReference[oaicite:1]{index=1}
video_data = result.get("data", {})
video_url = video_data.get("video_url") or video_data.get("video", {}).get("url")

print("\nFinal response:", result)
if video_url:
    print("\nDownload URL:", video_url)
else:
    print("\nNo video_url field found in response – check JSON structure above.")

if name == "main":
main()

HTTPError: 404 Client Error: NOT FOUND for url: https://api.heygen.com/v2/video/generate

It still gives an error.