# SwapFace换脸

{% hint style="danger" %}
此文档已停止维护，最新文档地址：<https://docs.ttapi.io>
{% endhint %}

<div align="left"><figure><img src="https://1799183090-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FglZ8aTwvA9oRr34hM7MK%2Fuploads%2F5XhdtYgm4HKf2Nqyl1GJ%2F3.png?alt=media&#x26;token=8948b27c-9ef7-4b6e-af7c-85e543d9f476" alt="" width="197"><figcaption></figcaption></figure></div>

### 创建换脸 Create SwapFace

<mark style="color:green;">`POST`</mark> `https://api.ttapi.io/insightFace/v1/swap`

创建换脸服务，此接口交互与 TTAPI Midjourney API 交互模式相同（Async）

**Headers**

| Name         | Value                  |
| ------------ | ---------------------- |
| Content-Type | `application/json`     |
| TT-API-KEY   | `用于请求授权 TT-API 的API密钥` |

**Body**

<table><thead><tr><th width="178">参数</th><th width="108">类型</th><th width="116">是否必须</th><th>描述</th></tr></thead><tbody><tr><td><code>targetImage</code></td><td>string</td><td>是</td><td>模板图像：换脸的模板图片url地址</td></tr><tr><td><code>swapImage</code></td><td>string</td><td>是</td><td>人脸图像：要换脸的图片url地址，会将该地址的图片中的人脸，换到<code>targetImage</code>的人脸中</td></tr><tr><td><code>hookUrl</code></td><td>string</td><td>否</td><td>回调地址，任务完成或失败将通过请地址进行通知，<a href="#huan-lian-jie-guo-shu-ju-jie-gou-hookurl-json">通知数据结构</a>与fetch结构一致。如果未设置，则需要请求<a href="#huo-qu-huan-lian-jie-guo-fetch-swapface">fetch</a>接口进行查询。</td></tr></tbody></table>

#### 请求示例

{% tabs %}
{% tab title="Python" %}

```
import requests

endpoint = "https://api.ttapi.io/insightFace/v1/swap"

headers = {
    "TT-API-KEY": your_key
}

data = {
    "targetImage": "https://cdnb.ttapi.io/2024-04-23/tar_image.jpg",
    "swapImage": "https://cdnb.ttapi.io/2024-04-23/face_image.jpg",
    "hookUrl": "https://webhook-test.com/10ffa7e6d1e6886e746d19a5458d167d"
}

response = requests.post(endpoint, headers=headers, json=data)

print(response.status_code)
print(response.json())
```

{% endtab %}

{% tab title="Node" %}

```
const axios = require('axios');

let config = {
  method: 'post',
  url: 'https://api.ttapi.io/insightFace/v1/swap',
  headers: { 
    'TT-API-KEY': 'your_key'
  },
  data : {
    "targetImage": "https://cdnb.ttapi.io/2024-04-23/tar_image.jpg",
    "swapImage": "https://cdnb.ttapi.io/2024-04-23/face_image.jpg",
    "hookUrl": "https://webhook-test.com/10ffa7e6d1e6886e746d19a5458d167d"
  }
};

axios(config).then(function (response) {
  console.log(JSON.stringify(response.status));
}).catch(function (error) {
  console.log(error);
});
```

{% endtab %}
{% endtabs %}

#### 响应示例

{% tabs %}
{% tab title="200" %}

```json
{
  "status": "SUCCESS",
  "message": "",
  "data": {
    "jobId": "SWAP-FACE-171349414479347616"
  }
}
```

{% endtab %}

{% tab title="400" %}

```json
{
  "status": "FAILED",
  "message": "'action' parameter error!",
  "data": null
}
```

{% endtab %}
{% endtabs %}

### 获取换脸结果  Fetch SwapFace

<mark style="color:green;">`POST/GET`</mark> `https://api.ttapi.io/insightFace/v1/fetch`

获取换脸任务结果查询，返回数据结构与[hookUrl](#huan-lian-jie-guo-shu-ju-jie-gou-hookurl-json)中返回一致

**Headers**

| Name         | Value                  |
| ------------ | ---------------------- |
| Content-Type | `application/json`     |
| TT-API-KEY   | `用于请求授权 TT-API 的API密钥` |

**Query / Body**

<table><thead><tr><th width="178">参数</th><th width="108">类型</th><th width="116">是否必须</th><th>描述</th></tr></thead><tbody><tr><td>jobId</td><td>string</td><td>是</td><td>创建换脸接口返回的jobId</td></tr></tbody></table>

#### 请求示例

{% tabs %}
{% tab title="Python" %}

```
import requests

endpoint = "https://api.ttapi.io/insightFace/v1/fetch"

headers = {
    "TT-API-KEY": your_key
}

data = {
    "jobId": "SWAP-FACE-171349414479347616"
}

response = requests.post(endpoint, headers=headers, json=data)

print(response.status_code)
print(response.json())
```

{% endtab %}

{% tab title="Node" %}

```
const axios = require('axios');

let config = {
  method: 'post',
  url: 'https://api.ttapi.io/insightFace/v1/fetch',
  headers: { 
    'TT-API-KEY': 'your_key'
  },
  data : {
     "jobId": "SWAP-FACE-171349414479347616"
  }
};

axios(config).then(function (response) {
  console.log(JSON.stringify(response.status));
}).catch(function (error) {
  console.log(error);
});
```

{% endtab %}
{% endtabs %}

### 换脸结果数据结构  HookUrl  JSON

{% tabs %}
{% tab title="SUCCESS" %}

```
{
  "status": "SUCCESS",
  "message": null,
  "jobId": "SWAP-FACE-171349414479347616",
  "data": {
    "jobId": "SWAP-FACE-171349414479347616",
    "targetImage": "https://pics1.baidu.com/feed/a5c27d1ed21b0ef42316cbd385d68fd780cb3e64.jpeg@f_auto?token=0d9295cb14265da251460e7e8918db0a",
    "swapImage": "https://img2.baidu.com/it/u=2626318104,3413060045&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=625",
    "discordImage": "https://cdn.discordapp.com/attachments/1225427359679909939/1230708397020811325/2aa3ab1495b844d084aa6eaaa4fb9bcf_ins.jpg?ex=66344d83&is=6621d883&hm=a0ac9fbb17cf52302662e2f50b40a9894977624d55bcc4f32d6ed09e7c56a415&",
    "cdnImage": "https://mjcdn.ttapi.io/attachments/1225427359679909939/1230708397020811325/2aa3ab1495b844d084aa6eaaa4fb9bcf_ins.jpg?ex=66344d83&is=6621d883&hm=a0ac9fbb17cf52302662e2f50b40a9894977624d55bcc4f32d6ed09e7c56a415&",
    "hookUrl": null,
    "quota": 3
  }
}
```

{% endtab %}

{% tab title="ON\_QUEUE " %}

```
{
  "status": "ON_QUEUE",
  "message": null,
  "jobId": "SWAP-FACE-171349414479347616",
  "data": null
}
```

{% endtab %}

{% tab title="FAILED " %}

```
{
  "status": "FAILED",
  "message": null,
  "jobId": "SWAP-FACE-171349414479347616",
  "data": null
}
```

{% endtab %}
{% endtabs %}

### 换脸返回参数详解

<table><thead><tr><th width="189">参数</th><th>详解</th></tr></thead><tbody><tr><td>status</td><td>换脸结果状态，<br>ON_QUEUE：队列中<br>SUCCESS：成功<br>FAILED：失败</td></tr><tr><td>targetImage</td><td>用户上传的模板图像地址</td></tr><tr><td>swapImage</td><td>用户上传的人脸图像地址</td></tr><tr><td>discordImage</td><td>任务成功返回结果的discord地址，国内不可直接访问</td></tr><tr><td>cdnImage</td><td>任务成功返回结果的ttapi代理地址，国内可直接访问</td></tr><tr><td>quota</td><td>实际消耗额度</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-zh.mjapiapp.com/api/swapface-huan-lian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
