项目背景
众所周知,midjourney并没有提供任何的api服务,但是基于midjourney目前的行业龙头位置,很多企业以及个人会有相关的需求。ttapi平台基于midjourney现有功能整理出一套完整的可集成性高的服务,如果你有类似的需求,那么恭喜你找到了正确的使用方式。
新用户注册免费送 100 配额,最多可以免费请求 50 次imagine接口
出图效果
imagine接口示例
java请求示例
import cn.hutool.http.httprequest;
import cn.hutool.json.jsonutil;
import java.util.hashmap;
import java.util.map;
/**
* 参考文档
* (中文版):https://docs-zh.mjapiapp.com/midjourney/midjourney-api
* (英文版):https://docs.mjapiapp.com/reference/midjourney-api
*/
public class ttapimain {
//tt-api-key
private static final string tt_api_ket = "your_key";
//提交绘图申请
private static final string imagine_url = "https://api.ttapi.io/midjourney/v1/imagine";
//查询绘图结果
private static final string fetch_result = "https://api.ttapi.io/midjourney/v1/fetch";
public static void main(string[] args) {
//发送imagine绘图请求
string result = imagine("dog");
//查询绘图结果
fetch(jsonutil.parseobj(result).getjsonobject("data").getstr("jobid"));
}
/**
* 发送 imagine 绘图请求
* @param prompt
*/
public static string imagine(string prompt){
map<string, object> map = new hashmap<>();
//提示词
map.put("prompt", prompt);
string result = httprequest.post(imagine_url)
.body(jsonutil.tojsonstr(map))
.header("tt-api-key", tt_api_ket)
.execute().body();
system.out.println("绘图请求响应:" + result);
//{"status":"success","message":"success","data":{"jobid":"******************************"}}
return result;
}
/**
* 查询绘图结果
* @param jobid 任务id
*/
public static void fetch(string jobid){
string result = httprequest.get(fetch_result + "?jobid=" + jobid)
.header("tt-api-key", tt_api_ket)
.execute().body();
system.out.println("绘图结果:" + result);
//{"status":"on_queue","message":"","jobid":"******************************","data":{"action":"imagine","jobid":"******************************","progress":null,"prompt":"dog","discordimage":null,"cdnimage":null,"hookurl":null,"components":null,"seed":null}}
}
}
python请求示例
import requests
endpoint = "https://api.ttapi.io/midjourney/v1/imagine"
headers = {
"tt-api-key": your_key
}
data = {
"prompt": "a cute cat",
"mode": "fast",
"hookurl": "",
"timeout": 300
}
response = requests.post(endpoint, headers=headers, json=data)
print(response.status_code)
print(response.json())
tt-api-key 获取方式
- 首先:注册ttapi平台, 注册地址,注册后跳转至激活页面
- 点击 send activate email,发送激活邮件至您的邮箱
- 在您的邮箱中点击verify email(如果找不到激活邮件,有可能被邮件服务商勿拦,请翻阅垃圾邮件)
- 点击后即激活成功,自动跳转至平台主页,同时您的邮件中将会收到成功邮件,首页点击转个人中心即可查看tt-api-key以及其他相关信息
- 注册激活成功系统赠送100quota,可以免费请求33次imagine接口。具体相关支持请查看文档地址
功能特点
- 包含midjourney目前所有功能 imagine u v zoom pan vary blend describe seed 等等
- 支持midjourney所有命令 –v --cref --ar 等等
- 支持webhook回调任务状态交互以及主动查询任务结果
参数详解
- header中的tt-api-key为全局必传参数,所有请求都需要使用,个人中心中即可获得
- mode是我们要使用的模式支持
fast,relax,turbo
对应的也就是midjourney的模式,不设置默认为fast - timeout参数为超时时间设置,fast模式一般300秒以内即可,relax模式一般600秒以内即可,不设置默认为300
- hookurl为任务成功失败回调的地址,注意该地址请保证一定可以接收请求,任务状态数据会以json格式返回到该地址,以http状态码为准200即认为通知成功,http状态码非200最多会进行通知三遍
发表评论