Happy Pony API Get an API key →

Face swap

Pricing: Face swap

A base image and one to three reference faces in, one image out: the person in the base image with the reference face. The same job as a generation on www.happypony.io/face-swap.

POST https://api.happypony.io/api/v1/jobs/face-swap
GET  https://api.happypony.io/api/v1/jobs/:requestId/status
GET  https://api.happypony.io/api/v1/jobs/:requestId
PUT  https://api.happypony.io/api/v1/jobs/:requestId/cancel

1) Create a request

POST https://api.happypony.io/api/v1/jobs/face-swap with Content-Type: application/json.

Field Type Required Default Description
base_image_url string yes — The body, pose, clothes and scene to keep. See Image inputs.
face_image_urls string[] yes — 1 to 3 photos of the face to put in. More angles of the same face help.

There are no other settings: the output follows the base image.

Request body:

{
  "base_image_url": "https://resource.pipelet.net/images/test/selfie5.png",
  "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"]
}
curl -X POST https://api.happypony.io/api/v1/jobs/face-swap \
  -H "Authorization: Bearer $HP_KEY" -H "Content-Type: application/json" \
  -d '{"base_image_url": "https://resource.pipelet.net/images/test/selfie5.png", "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"]}'

Response 202 (fields: Create response):

{
  "request_id": "48340",
  "type": "face-swap",
  "status": "IN_QUEUE",
  "credits": 5,
  "status_url": "https://api.happypony.io/api/v1/jobs/48340/status",
  "response_url": "https://api.happypony.io/api/v1/jobs/48340",
  "cancel_url": "https://api.happypony.io/api/v1/jobs/48340/cancel"
}

2) Poll for status

Poll the status_url until status is COMPLETED, FAILED or CANCELLED (fields: Status response). A queued job can be cancelled with PUT on the cancel_url (Cancel a request).

curl https://api.happypony.io/api/v1/jobs/48340/status -H "Authorization: Bearer $HP_KEY"
{
  "request_id": "48340",
  "type": "face-swap",
  "status": "IN_PROGRESS",
  "created_at": "2026-10-05T10:00:00.000Z",
  "completed_at": null,
  "credits": 5,
  "status_url": "…", "response_url": "…", "cancel_url": "…"
}

3) Fetch the result

When status is COMPLETED, fetch the response_url (common fields: Result response):

curl https://api.happypony.io/api/v1/jobs/48340 -H "Authorization: Bearer $HP_KEY"
{
  "request_id": "48340",
  "type": "face-swap",
  "status": "COMPLETED",
  "created_at": "2026-10-05T10:00:00.000Z",
  "completed_at": "2026-10-05T10:00:41.000Z",
  "credits": 5,
  "images": [{ "url": "https://…signed…", "format": "png" }],
  "expires_in": 259200,
  "parameters": { "model": "qwen-image-edit-face-swap", "faces": 1 },
  "status_url": "…", "response_url": "…", "cancel_url": "…"
}
Field Type Description
images object[] The swapped image.
parameters object model (always qwen-image-edit-face-swap) and faces, the number of reference faces.

Full example

Create, poll until done, download:

curl (bash)

# Needs curl and jq. Usage: HP_KEY=hp_… ./swap.sh body.png face.png
up() { curl -s -X POST https://api.happypony.io/api/v1/jobs/uploads \
         -H "Authorization: Bearer $HP_KEY" -F "file=@$1" | jq -r .url; }
BASE=$(up "$1"); FACE=$(up "$2")
JOB=$(curl -s -X POST https://api.happypony.io/api/v1/jobs/face-swap \
  -H "Authorization: Bearer $HP_KEY" -H "Content-Type: application/json" \
  -d "{\"base_image_url\": \"$BASE\", \"face_image_urls\": [\"$FACE\"]}")
echo "$JOB" | jq .
STATUS_URL=$(echo "$JOB" | jq -r .status_url)
RESPONSE_URL=$(echo "$JOB" | jq -r .response_url)

while :; do
  STATUS=$(curl -s "$STATUS_URL" -H "Authorization: Bearer $HP_KEY" | jq -r .status)
  [ "$STATUS" = IN_QUEUE ] || [ "$STATUS" = IN_PROGRESS ] || break
  sleep 5
done
echo "$STATUS"
curl -s "$RESPONSE_URL" -H "Authorization: Bearer $HP_KEY" | jq -r '.images[] | "\(.format) \(.url)"' |
  while read -r FORMAT URL; do curl -s -o "swap-$((++i)).$FORMAT" "$URL"; done

Python

# pip install requests   |   HP_KEY=hp_… python swap.py
import os, time, requests

BASE = "https://api.happypony.io/api/v1/jobs"
H = {"Authorization": f"Bearer {os.environ['HP_KEY']}"}

job = requests.post(f"{BASE}/face-swap", headers=H, json={
    "base_image_url": "https://resource.pipelet.net/images/test/selfie5.png",
    "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"],
})
job.raise_for_status()
job = job.json()
print("job", job["request_id"], "costs", job["credits"], "credits")

while (s := requests.get(job["status_url"], headers=H).json())["status"] in ("IN_QUEUE", "IN_PROGRESS"):
    time.sleep(5)
result = requests.get(job["response_url"], headers=H).json()
if result["status"] != "COMPLETED":
    raise SystemExit(f"{result['status']}: {result.get('error', '')}")
for i, image in enumerate(result["images"]):
    with open(f"swap-{job['request_id']}-{i}.{image['format']}", "wb") as f:
        f.write(requests.get(image["url"]).content)

换脸

价格:换脸

传入一张底图和 1 到 3 张参考人脸,输出一张图片:底图中的人物换上参考人脸。与在 www.happypony.io/face-swap 上生成的任务完全相同。

POST https://api.happypony.io/api/v1/jobs/face-swap
GET  https://api.happypony.io/api/v1/jobs/:requestId/status
GET  https://api.happypony.io/api/v1/jobs/:requestId
PUT  https://api.happypony.io/api/v1/jobs/:requestId/cancel

1) 创建请求

POST https://api.happypony.io/api/v1/jobs/face-swap,Content-Type: application/json。

字段 类型 必填 默认值 说明
base_image_url string 是 — 要保留的身体、姿势、服装和场景。见图片输入。
face_image_urls string[] 是 — 1 到 3 张要换上的人脸照片。同一张脸的多个角度效果更好。

没有其他参数:输出跟随底图。

请求体:

{
  "base_image_url": "https://resource.pipelet.net/images/test/selfie5.png",
  "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"]
}
curl -X POST https://api.happypony.io/api/v1/jobs/face-swap \
  -H "Authorization: Bearer $HP_KEY" -H "Content-Type: application/json" \
  -d '{"base_image_url": "https://resource.pipelet.net/images/test/selfie5.png", "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"]}'

响应 202(字段见创建响应):

{
  "request_id": "48340",
  "type": "face-swap",
  "status": "IN_QUEUE",
  "credits": 5,
  "status_url": "https://api.happypony.io/api/v1/jobs/48340/status",
  "response_url": "https://api.happypony.io/api/v1/jobs/48340",
  "cancel_url": "https://api.happypony.io/api/v1/jobs/48340/cancel"
}

2) 轮询状态

轮询 status_url,直到 status 变为 COMPLETED、FAILED 或 CANCELLED(字段见状态响应)。排队中的任务可以向 cancel_url 发送 PUT 取消(见取消请求)。

curl https://api.happypony.io/api/v1/jobs/48340/status -H "Authorization: Bearer $HP_KEY"
{
  "request_id": "48340",
  "type": "face-swap",
  "status": "IN_PROGRESS",
  "created_at": "2026-10-05T10:00:00.000Z",
  "completed_at": null,
  "credits": 5,
  "status_url": "…", "response_url": "…", "cancel_url": "…"
}

3) 获取结果

status 为 COMPLETED 后,获取 response_url(通用字段见结果响应):

curl https://api.happypony.io/api/v1/jobs/48340 -H "Authorization: Bearer $HP_KEY"
{
  "request_id": "48340",
  "type": "face-swap",
  "status": "COMPLETED",
  "created_at": "2026-10-05T10:00:00.000Z",
  "completed_at": "2026-10-05T10:00:41.000Z",
  "credits": 5,
  "images": [{ "url": "https://…signed…", "format": "png" }],
  "expires_in": 259200,
  "parameters": { "model": "qwen-image-edit-face-swap", "faces": 1 },
  "status_url": "…", "response_url": "…", "cancel_url": "…"
}
字段 类型 说明
images object[] 换脸后的图片。
parameters object model(始终为 qwen-image-edit-face-swap)和 faces(参考人脸数量)。

完整示例

创建、轮询直到完成、下载:

curl (bash)

# Needs curl and jq. Usage: HP_KEY=hp_… ./swap.sh body.png face.png
up() { curl -s -X POST https://api.happypony.io/api/v1/jobs/uploads \
         -H "Authorization: Bearer $HP_KEY" -F "file=@$1" | jq -r .url; }
BASE=$(up "$1"); FACE=$(up "$2")
JOB=$(curl -s -X POST https://api.happypony.io/api/v1/jobs/face-swap \
  -H "Authorization: Bearer $HP_KEY" -H "Content-Type: application/json" \
  -d "{\"base_image_url\": \"$BASE\", \"face_image_urls\": [\"$FACE\"]}")
echo "$JOB" | jq .
STATUS_URL=$(echo "$JOB" | jq -r .status_url)
RESPONSE_URL=$(echo "$JOB" | jq -r .response_url)

while :; do
  STATUS=$(curl -s "$STATUS_URL" -H "Authorization: Bearer $HP_KEY" | jq -r .status)
  [ "$STATUS" = IN_QUEUE ] || [ "$STATUS" = IN_PROGRESS ] || break
  sleep 5
done
echo "$STATUS"
curl -s "$RESPONSE_URL" -H "Authorization: Bearer $HP_KEY" | jq -r '.images[] | "\(.format) \(.url)"' |
  while read -r FORMAT URL; do curl -s -o "swap-$((++i)).$FORMAT" "$URL"; done

Python

# pip install requests   |   HP_KEY=hp_… python swap.py
import os, time, requests

BASE = "https://api.happypony.io/api/v1/jobs"
H = {"Authorization": f"Bearer {os.environ['HP_KEY']}"}

job = requests.post(f"{BASE}/face-swap", headers=H, json={
    "base_image_url": "https://resource.pipelet.net/images/test/selfie5.png",
    "face_image_urls": ["https://resource.pipelet.net/images/test/selfie3.png"],
})
job.raise_for_status()
job = job.json()
print("job", job["request_id"], "costs", job["credits"], "credits")

while (s := requests.get(job["status_url"], headers=H).json())["status"] in ("IN_QUEUE", "IN_PROGRESS"):
    time.sleep(5)
result = requests.get(job["response_url"], headers=H).json()
if result["status"] != "COMPLETED":
    raise SystemExit(f"{result['status']}: {result.get('error', '')}")
for i, image in enumerate(result["images"]):
    with open(f"swap-{job['request_id']}-{i}.{image['format']}", "wb") as f:
        f.write(requests.get(image["url"]).content)