# TTAPI格式

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

## Flux 生成图像

<mark style="color:green;">`POST`</mark> `https://api.ttapi.io/flux/generate`

根据文本提示使用Flux.1模型生成图像。注意，该接口与Midjourney的区别是单次只能生成一张图片

**Headers**

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

**Body**

<table><thead><tr><th width="166">参数</th><th width="107">类型</th><th width="169">是否必须</th><th>描述</th></tr></thead><tbody><tr><td><code>prompt</code></td><td>string</td><td>是</td><td>生成图像描述词<br>注意：Flux支持多语言理解能力，但实测英文最佳。</td></tr><tr><td><code>size</code></td><td>string</td><td>否</td><td><p>图像尺寸，可选范围：</p><p>1024x1024 </p><p>1024x1792</p><p>1792x1024<br>（即将弃用）</p></td></tr><tr><td><code>aspect_ratio</code></td><td>string</td><td>否</td><td><p>图像比例，可选范围：</p><p>21:9, 16:9, 4:3, 3:2, 1:1, 2:3, 3:4, 9:16, 9:21（默认为1:1）</p></td></tr><tr><td><code>mode</code></td><td>string</td><td>是</td><td><p>模型版本，可选范围：<br>flux1-dev <br>flux1-schnell <br>flux1-pro</p><p>flux-kontext-pro</p><p>flux-kontext-max<br>flux-2-pro<br>flux-2-flex<br>flux-2-max</p></td></tr><tr><td><code>hookUrl</code></td><td>string</td><td>否</td><td>生成图像结果异步通知地址</td></tr></tbody></table>

{% hint style="info" %}
注意：Flux生成图像接口同步返回的并不是任务的最终结果，任务的最终返回结果需要使用`hookUrl`参数接收异步通知，或者通过[Flux查询任务结果](#flux-huo-qu-tu-xiang-jie-guo)接口进行查询。
{% endhint %}

#### 请求示例

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

```
import requests

endpoint = "https://api.ttapi.io/flux/generate"

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

data = {
    "prompt": "a cute cat",
    "mode": "flux1-dev"
}

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/flux/generate',
  headers: { 
    'TT-API-KEY': 'your_key'
  },
  data : {
    "prompt": "a cute cat",
    "mode": "flux1-dev"
  }
};

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": "success",
    "data": {
        "jobId": "fac30d6c-d12b-465c-a580-8b1b3ae726e3"
    }
}
```

{% endtab %}
{% endtabs %}

## Flux 修改图像

<mark style="color:green;">`POST`</mark> `https://api.ttapi.io/flux/edits`

根据文本提示和上传垫图生成新图像

**Headers**

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

**Body**  multipart/form-data

<table><thead><tr><th width="166">参数</th><th width="107">类型</th><th width="169">是否必须</th><th>描述</th></tr></thead><tbody><tr><td><code>image</code></td><td>file</td><td>是</td><td>垫图，支持多张</td></tr><tr><td><code>prompt</code></td><td>string</td><td>是</td><td>生成图像描述词<br>注意：Flux支持多语言理解能力，但实测英文最佳。</td></tr><tr><td><code>aspect_ratio</code></td><td>string</td><td>否</td><td><p>图像比例，可选范围：</p><p>21:9, 16:9, 4:3, 3:2, 1:1, 2:3, 3:4, 9:16, 9:21（默认为1:1）</p></td></tr><tr><td><code>mode</code></td><td>string</td><td>是</td><td><p>模型版本，可选范围：<br>flux1-dev <br>flux1-schnell <br>flux1-pro</p><p>flux-kontext-pro</p><p>flux-kontext-max<br>flux-2-pro<br>flux-2-flex<br>flux-2-max</p></td></tr><tr><td><code>hookUrl</code></td><td>string</td><td>否</td><td>生成图像结果异步通知地址</td></tr></tbody></table>

#### 请求示例

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

```
import requests

url = "https://api.ttapi.io/flux/edits"

data={
    "prompt": "a cute cat",
    "mode": "flux1-dev"
}
files=[
   ('image',('',open('','rb'),'application/octet-stream'))
]
headers = {
   'TT-API-KEY': 'Your Key'
}

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

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

{% endtab %}
{% endtabs %}

#### 返回示例

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

```json
{
    "status": "SUCCESS",
    "message": "success",
    "data": {
        "jobId": "fac30d6c-d12b-465c-a580-8b1b3ae726e3"
    }
}
```

{% endtab %}
{% endtabs %}

## Flux 获取图像结果

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

根据生成图像接口返回的`jobId`获取任务的最终结果，**此接口免费**。

**Headers**

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

**Body**

<table><thead><tr><th width="166">参数</th><th width="107">类型</th><th width="169">是否必须</th><th>描述</th></tr></thead><tbody><tr><td><code>jobId</code></td><td>string</td><td>是</td><td>生成图像接口同步返回的jobId</td></tr></tbody></table>

#### 请求示例

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

```
import requests

endpoint = "https://api.ttapi.io/flux/fetch"

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

data = {
    "jobId": "bf165abb-a7fa-4c77-a2e4-138baac1130d"
}

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/flux/fetch',
  headers: { 
    'TT-API-KEY': 'your_key'
  },
  data : {
     "jobId": "bf165abb-a7fa-4c77-a2e4-138baac1130d"
  }
};

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": "success",
    "jobId": "bf165abb-a7fa-4c77-a2e4-138baac1130d",
    "data": {
        "mode": "flux-dev",
        "jobId": "bf165abb-a7fa-4c77-a2e4-138baac1130d",
        "prompt": "漂亮的亚洲女孩和一只猫的合影",
        "quota": "1",
        "size": "1024x1024",
        "imageUrl": "https://cdnb.ttapi.io/2024-09-10/9rk3gmw3m6sxm4r75my02r48jizyli1l.webp",
        "hookUrl": "https://webhook-test.com/d47a918bcf231bfd4dcd1f63c2cb4c80"
    }
}
```

{% endtab %}
{% endtabs %}

## 返回JSON结构详解

| name     | value                                                        |
| -------- | ------------------------------------------------------------ |
| status   | <p>ON\_QUEUE  //执行中</p><p>SUCCESS   //成功<br>FAILED  //失败</p> |
| imageUrl | 生成图像地址，有效期一个月，请自行转存                                          |
| quota    | 实际消耗额度                                                       |
