API

如何调用后台API进行开发,即将上线,敬请期待.

API的应用场景

  • 二开形成新的应用、小程序或者APP,提供接口
  • 接入硬件产品如AI相机、AI试装
  • 接入企业内部其他生产系统

工作流调用

获取API key

  1. 登录EasyAI平台,进入个人空间,打开API KEY窗口

同步调用工作流

  1. 使用OpenAI格式接口调用工作流,示例代码:

const API_KEY = 'your api key';  // 替换成你的API KEY
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "Bearer ${API_KEY}");

const raw = JSON.stringify({
    "prompt": "a cat",
    "size": "1024x1024",
    "model": "text2img",  //model 对应EasyAI平台的工作流名称
    "n": 2
});

// size,n,prompt会自动转换为EasyAI平台的width and height,batchSize,positive,其他参数保持不变,支持EasyAI平台上的所有参数

var requestOptions = {
    method: 'POST',
    headers: myHeaders,
    body: raw,
};

// 将http://127.0.0.1:3001换成你的EasyAI后端服务器地址
fetch("http://127.0.0.1:3001/v1/images/generations", requestOptions)
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log('error', error));

响应格式:

{
    "status": "success",
    "data": [
      {
        "url": "https://oss.gptpro.ink/docImg/202505191424072.png-doc.jpg",
        "type": "image"
      }
    ]
}

查看请求参数

工作流设计页面可以查询到请求的参数

也可以通过浏览器F12拦截请求查看参数:F12打开浏览器的控制台,切换到网络选项卡,点击发送请求,查看请求参数

异步调用工作流

提交任务接口:POST /v1/ai/generations

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer {your api key}");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
   "params": {
      "positive": "a dog"   //绘图参数,查看方法同上
   },
   "options": {
      "workflow_name": "flux"  //工作流名称
   }
});

var requestOptions = {
   method: 'POST',
   headers: myHeaders,
   body: raw,
   redirect: 'follow'
};

fetch("http://127.0.0.1:3001/v1/ai/generations", requestOptions)
   .then(response => response.text())
   .then(result => console.log(result))
   .catch(error => console.log('error', error));

响应格式:

{
  "task_id": "6864dfeafd238d554dc4300e",
  "status": "submitted",
  "message": "成功加入任务清单,排队等待任务执行,任务执行完成后根据output中的taskid取回绘图结果",
  "data": {
    "queue": 1,   //当前任务排队数
    "taskId": "6864dfeafd238d554dc4300e"
  }
}

任务结果查询

获取任务结果接口:GET /v1/ai/result/:taskId

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer {your api key}");

var requestOptions = {
   method: 'GET',
   headers: myHeaders,
};

fetch("http://127.0.0.1:3001/v1/ai/result/6864dfeafd238d554dc4300e", requestOptions)
   .then(response => response.text())
   .then(result => console.log(result))
   .catch(error => console.log('error', error));

响应格式:

{
    "task_id": "6864dfeafd238d554dc4300e",
    "status": "success",
    "output": [
        "https://easyai-1253343986.cos.ap-shanghai.myqcloud.com/image/temps/663e19cd4fa9d8078385c7c9/ComfyUI_01796_-Xl-dij.png"
    ],
    "type": "image"  //绘图结果类型,可选image,text,video,audio,3d
}

取消执行中的任务

取消任务接口:Get /v1/ai/cancel/:taskId

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer {your api key}");
var requestOptions = {
   method: 'GET',
   headers: myHeaders,
}
fetch("http://127.0.0.1:3001/v1/ai/cancel/6864dfeafd238d554dc4300e", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

相应格式:

 {
  "status": "success",
  "message": "任务取消成功"
}

工作流上传

建议使用ComfyUI-EasyAI插件进行工作流上传