$npx -y skills add calesthio/OpenMontage --skill faceswapSwap faces in a video using AI via the HeyGen API. Use when: (1) Replacing a face in a video with another face, (2) Face swapping from a source image onto a target video, (3) Creating personalized videos by swapping in a person's face, (4) Working with HeyGen's /v1/workflows/exec
| 1 | # Face Swap (HeyGen API) |
| 2 | |
| 3 | Swap a face from a source image into a target video using GPU-accelerated AI processing. The source image provides the face to swap in, and the target video receives the new face. |
| 4 | |
| 5 | ## Authentication |
| 6 | |
| 7 | All requests require the `X-Api-Key` header. Set the `HEYGEN_API_KEY` environment variable. |
| 8 | |
| 9 | ```bash |
| 10 | curl -X POST "https://api.heygen.com/v1/workflows/executions" \ |
| 11 | -H "X-Api-Key: $HEYGEN_API_KEY" \ |
| 12 | -H "Content-Type: application/json" \ |
| 13 | -d '{"workflow_type": "FaceswapNode", "input": {"source_image_url": "https://example.com/face.jpg", "target_video_url": "https://example.com/video.mp4"}}' |
| 14 | ``` |
| 15 | |
| 16 | ## Default Workflow |
| 17 | |
| 18 | 1. Call `POST /v1/workflows/executions` with `workflow_type: "FaceswapNode"`, a source face image, and a target video |
| 19 | 2. Receive a `execution_id` in the response |
| 20 | 3. Poll `GET /v1/workflows/executions/{id}` every 10 seconds until status is `completed` |
| 21 | 4. Use the returned `video_url` from the output |
| 22 | |
| 23 | ## Execute Face Swap |
| 24 | |
| 25 | ### Endpoint |
| 26 | |
| 27 | `POST https://api.heygen.com/v1/workflows/executions` |
| 28 | |
| 29 | ### Request Fields |
| 30 | |
| 31 | | Field | Type | Req | Description | |
| 32 | |-------|------|:---:|-------------| |
| 33 | | `workflow_type` | string | Y | Must be `"FaceswapNode"` | |
| 34 | | `input.source_image_url` | string | Y | URL of the face image to swap in | |
| 35 | | `input.target_video_url` | string | Y | URL of the video to apply the face swap to | |
| 36 | |
| 37 | ### curl |
| 38 | |
| 39 | ```bash |
| 40 | curl -X POST "https://api.heygen.com/v1/workflows/executions" \ |
| 41 | -H "X-Api-Key: $HEYGEN_API_KEY" \ |
| 42 | -H "Content-Type: application/json" \ |
| 43 | -d '{ |
| 44 | "workflow_type": "FaceswapNode", |
| 45 | "input": { |
| 46 | "source_image_url": "https://example.com/face-photo.jpg", |
| 47 | "target_video_url": "https://example.com/original-video.mp4" |
| 48 | } |
| 49 | }' |
| 50 | ``` |
| 51 | |
| 52 | ### TypeScript |
| 53 | |
| 54 | ```typescript |
| 55 | interface FaceswapInput { |
| 56 | source_image_url: string; |
| 57 | target_video_url: string; |
| 58 | } |
| 59 | |
| 60 | interface ExecuteResponse { |
| 61 | data: { |
| 62 | execution_id: string; |
| 63 | status: "submitted"; |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | async function faceswap(input: FaceswapInput): Promise<string> { |
| 68 | const response = await fetch("https://api.heygen.com/v1/workflows/executions", { |
| 69 | method: "POST", |
| 70 | headers: { |
| 71 | "X-Api-Key": process.env.HEYGEN_API_KEY!, |
| 72 | "Content-Type": "application/json", |
| 73 | }, |
| 74 | body: JSON.stringify({ |
| 75 | workflow_type: "FaceswapNode", |
| 76 | input, |
| 77 | }), |
| 78 | }); |
| 79 | |
| 80 | const json: ExecuteResponse = await response.json(); |
| 81 | return json.data.execution_id; |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | ### Python |
| 86 | |
| 87 | ```python |
| 88 | import requests |
| 89 | import os |
| 90 | |
| 91 | def faceswap(source_image_url: str, target_video_url: str) -> str: |
| 92 | payload = { |
| 93 | "workflow_type": "FaceswapNode", |
| 94 | "input": { |
| 95 | "source_image_url": source_image_url, |
| 96 | "target_video_url": target_video_url, |
| 97 | }, |
| 98 | } |
| 99 | |
| 100 | response = requests.post( |
| 101 | "https://api.heygen.com/v1/workflows/executions", |
| 102 | headers={ |
| 103 | "X-Api-Key": os.environ["HEYGEN_API_KEY"], |
| 104 | "Content-Type": "application/json", |
| 105 | }, |
| 106 | json=payload, |
| 107 | ) |
| 108 | |
| 109 | data = response.json() |
| 110 | return data["data"]["execution_id"] |
| 111 | ``` |
| 112 | |
| 113 | ### Response Format |
| 114 | |
| 115 | ```json |
| 116 | { |
| 117 | "data": { |
| 118 | "execution_id": "node-gw-f1s2w3p4", |
| 119 | "status": "submitted" |
| 120 | } |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | ## Check Status |
| 125 | |
| 126 | ### Endpoint |
| 127 | |
| 128 | `GET https://api.heygen.com/v1/workflows/executions/{execution_id}` |
| 129 | |
| 130 | ### curl |
| 131 | |
| 132 | ```bash |
| 133 | curl -X GET "https://api.heygen.com/v1/workflows/executions/node-gw-f1s2w3p4" \ |
| 134 | -H "X-Api-Key: $HEYGEN_API_KEY" |
| 135 | ``` |
| 136 | |
| 137 | ### Response Format (Completed) |
| 138 | |
| 139 | ```json |
| 140 | { |
| 141 | "data": { |
| 142 | "execution_id": "node-gw-f1s2w3p4", |
| 143 | "status": "completed", |
| 144 | "output": { |
| 145 | "video_url": "https://resource.heygen.ai/faceswap/output.mp4" |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | ``` |
| 150 | |
| 151 | ## Polling for Completion |
| 152 | |
| 153 | ```typescript |
| 154 | async function faceswapAndWait( |
| 155 | input: FaceswapInput, |
| 156 | maxWaitMs = 600000, |
| 157 | pollIntervalMs = 10000 |
| 158 | ): Promise<string> { |
| 159 | const executionId = await faceswap(input); |
| 160 | console.log(`Submitted face swap: ${executionId}`); |
| 161 | |
| 162 | const startTime = Date.now(); |
| 163 | while (Date.now() - startTime < maxWaitMs) { |
| 164 | const response = await fetch( |
| 165 | `https://api.heygen.com/v1/workflows/executions/${executionId}`, |
| 166 | { headers: { "X-Api-Key": process.env.HEYGEN_API_KEY! } } |
| 167 | ); |
| 168 | const { data } = await response.json(); |
| 169 | |
| 170 | switch (data.status) { |
| 171 | case "completed": |
| 172 | return data.output.video_url; |
| 173 | case "failed": |
| 174 | throw new Error(data.error?.message || "Face swap failed"); |
| 175 | case "not_found": |
| 176 | throw new Error("Workflow not found"); |
| 177 | default: |
| 178 | await new Promise((r) => setTimeout(r, pollIntervalM |