Flux GPT Edit 功能 - AI 图像编辑 - TimRouter API 转发接口
▼
Authorization
请在 Header 中加入参数 Authorization,取值为 Bearer 之后拼接令牌
范例:
Authorization: Bearer ********************
概述
给定一个提示,该模型将返回一个或多个预测的完成,并且也还能够返回每个位置的替代词元的概率。 为带来的提示和配置项新建完成 官方说明文档:https://platform.openai.com/docs/api-reference/images/createEdit
请求参数项
请求头参数
Accept
string
必填
范例: application/json
Authorization
string
非必填
范例: Bearer {{YOUR_API_KEY}}
请求体参数 application/json
multipart/form-data
非必填
image
file
必填
要编辑的图片。必须是受兼容的图片文件或图片数组。对于 gpt-image-1,每张图片应为小于 25MB 的 png、webp 或 jpg 文件。对于 dall-e-2,您只能带来一张图片,并且也该图片应为小于 4MB 的方形 png 文件。 范例: file://C:\Users\Administrator\Desktop\87069cf1-8838-4acf-bb31-e02e6c9a11eb.png
prompt
string
必填
所需图像的文本说明。dall-e-2 的最大长度为 1000 个字符,gpt-image-1 的最大长度为 32000 个字符。 范例: 换成红色衣服
mask
string
非必填
一张附加图片,其完全透明区域(比如,alpha 值为零)指示应编辑 image 位置。若带来了多张图片,则遮罩将应用以第一张图片。必须是有效的 PNG 文件,小于 4MB,且尺寸与 image 相同。
model
string
非必填
用以产出图像的模型。仅 gpt-image-1, gpt-image-1-all , flux-kontext-pro , flux-kontext-max。 范例: flux-kontext-max
n
string
非必填
要产出的图像数量。必须介于 1 到 10 之间。 范例: 1
quality
string
非必填
产出图像的质量。只有 gpt-image-1 兼容 high、medium 和 low 质量。dall-e-2 仅兼容 standard 质量。默认取值为 auto。
response_format
string
非必填
返回产出图像的格式。必须是 url 或 b64_json 之一。URL 在图像产出后 60 分钟内有效。此配置项仅适合用于 dall-e-2,因为 gpt-image-1 始终返回 base64 编码的图像,请不要使用这个配置项。 范例:
b64_json
非必填
aspect_ratio
string
非必填
产出图像的尺寸。枚举值Possible enum values: 21:9, 16:9, 4:3, 3:2, 1:1, 2:3, 3:4, 9:16, 9:21 范例: 16:9
background
string
非必填
允许为产出的图像的背景配置透明度。此配置项仅在 gpt-image-1 中受兼容。其值必须为 “透明(transparent)”、“不透明(opaque)” 或 “自动(auto)”(缺省值)之一。当使用 “自动(auto)” 时,模型将自动为图像确定最佳背景。 范例:
transparent
非必填
moderation
string
非必填
控制由 gpt-image-1 产出的图像的内容审核级别。能够配置为 “low” 以进行限制较少的过滤,也能够配置为 “auto”(缺省值)。 范例:
low
非必填
请求
范例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
请求代码示例
{
"model": "gpt-image-1",
"image": "base64编码的图片",
"prompt": "将背景改为海滩"
}
curl --location --request POST 'https://api.timrouter.com/v1/images/edits' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer YOUR_API_KEY' \
--data-raw '{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}'
var myHeaders = new Headers();
myHeaders.append("Accept", "application/json");
myHeaders.append("Authorization", "Bearer YOUR_API_KEY");
var raw = JSON.stringify({
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://api.timrouter.com/v1/images/edits", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
import java.io.*;
import java.net.*;
import java.util.*;
URL url = new URL("https://api.timrouter.com/v1/images/edits");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "Bearer YOUR_API_KEY");
conn.setDoOutput(true);
String jsonInputString = "{
\"id\": \"chatcmpl-123\",
\"object\": \"chat.completion\",
\"created\": 1677652288,
\"choices\": [
{
\"index\": 0,
\"message\": {
\"role\": \"assistant\",
\"content\": \"\n\nHello there, how may I assist you today?\"
},
\"finish_reason\": \"stop\"
}
],
\"usage\": {
\"prompt_tokens\": 9,
\"completion_tokens\": 12,
\"total_tokens\": 21
}
}";
try(OutputStream os = conn.getOutputStream()) {
byte[] input = jsonInputString.getBytes("utf-8");
os.write(input, 0, input.length);
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
import Foundation
let urlString = "https://api.timrouter.com/v1/images/edits"
guard let url = URL(string: urlString) else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.addValue("Bearer YOUR_API_KEY", forHTTPHeaderField: "Authorization")
let httpBody = "{
\"id\": \"chatcmpl-123\",
\"object\": \"chat.completion\",
\"created\": 1677652288,
\"choices\": [
{
\"index\": 0,
\"message\": {
\"role\": \"assistant\",
\"content\": \"\n\nHello there, how may I assist you today?\"
},
\"finish_reason\": \"stop\"
}
],
\"usage\": {
\"prompt_tokens\": 9,
\"completion_tokens\": 12,
\"total_tokens\": 21
}
}"
request.httpBody = httpBody.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
print(String(data: data, encoding: .utf8)!)
}
}
task.resume()
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
body := strings.NewReader(`{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}`)
req, _ := http.NewRequest("POST", "https://api.timrouter.com/v1/images/edits", body)
req.Header.Set("Accept", "application/json")
req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
client := &http.Client{}
resp, _ := client.Do(req)
defer resp.Body.Close()
bodyBytes, _ := io.ReadAll(resp.Body)
fmt.Println(string(bodyBytes))
}
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.timrouter.com/v1/images/edits',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}',
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
"Authorization: Bearer YOUR_API_KEY",
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
import http.client
import json
conn = http.client.HTTPSConnection("api.timrouter.com")
payload = json.dumps({
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
})
headers = {
'Accept': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY',
}
conn.request("POST", "/v1/images/edits", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
POST https://api.timrouter.com/v1/images/edits HTTP/1.1
Accept: application/json
Authorization: Bearer YOUR_API_KEY
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://api.timrouter.com/v1/images/edits");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer YOUR_API_KEY");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{
\"id\": \"chatcmpl-123\",
\"object\": \"chat.completion\",
\"created\": 1677652288,
\"choices\": [
{
\"index\": 0,
\"message\": {
\"role\": \"assistant\",
\"content\": \"\n\nHello there, how may I assist you today?\"
},
\"finish_reason\": \"stop\"
}
],
\"usage\": {
\"prompt_tokens\": 9,
\"completion_tokens\": 12,
\"total_tokens\": 21
}
}");
CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://api.timrouter.com/v1/images/edits");
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Authorization", "Bearer YOUR_API_KEY");
request.AddParameter("application/json", @"{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
#import <Foundation/Foundation.h>
NSURL *url = [NSURL URLWithString:@"https://api.timrouter.com/v1/images/edits"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"Bearer YOUR_API_KEY" forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:[@"{
\"id\": \"chatcmpl-123\",
\"object\": \"chat.completion\",
\"created\": 1677652288,
\"choices\": [
{
\"index\": 0,
\"message\": {
\"role\": \"assistant\",
\"content\": \"\n\nHello there, how may I assist you today?\"
},
\"finish_reason\": \"stop\"
}
],
\"usage\": {
\"prompt_tokens\": 9,
\"completion_tokens\": 12,
\"total_tokens\": 21
}
}" dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
[task resume];
require "uri"
require "net/http"
require "json"
url = URI("https://api.timrouter.com/v1/images/edits")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Accept"] = "application/json"
request["Authorization"] = "Bearer YOUR_API_KEY"
request.body = '{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}'
response = http.request(request)
puts response.read_body
(* Requires cohttp and lwt *)
let url = "https://api.timrouter.com/v1/images/edits" in
let headers = Cohttp.Header.of_list [
("Accept", "application/json");
("Authorization", "Bearer YOUR_API_KEY");
] in
let body = Cohttp_lwt.Body.of_string '{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}' in
Lwt_main.run (
Cohttp_lwt_unix.Client.request ?body:(Some body) ~method_:`POST ~headers (Uri.of_string url)
>>= fun (resp, body) ->
Cohttp_lwt.Body.to_string body >|= fun s -> print_endline s
)
import 'package:http/http.dart' as http;
import 'dart:convert';
var headers = {
"Accept": "application/json",
"Authorization": "Bearer YOUR_API_KEY",
};
var body = json.encode({
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
});
var response = await http.post(Uri.parse("https://api.timrouter.com/v1/images/edits"), headers: headers, body: body);
print(response.body);
library(httr)
url <- "https://api.timrouter.com/v1/images/edits"
body <- '{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "
Hello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}'
response <- post(url, body = body, add_headers("Accept" = "application/json", "Authorization" = "Bearer YOUR_API_KEY"))
content(response, "text", encoding = "UTF-8")
返回
返回参数 🟢 200 OK · application/json
id
string
必填
object
string
必填
created
integer
必填
choices
array [object]
必填
index
integer
非必填
message
object
非必填
finish_reason
string
非必填
usage
object
必填
prompt_tokens
integer
必填
completion_tokens
integer
必填
total_tokens
integer
必填
{ "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "choices": [ { "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }
常见异常
- 401 鉴权未成功:检查 API Key 是否无误
- 429 请求限流:降低请求频率或升级套餐
- 500 服务器异常:稍后重试
重要提示
- 请确保 API Key 有足够的余额
- 请求频率受 API 限制,请参考定价页面