JS SDK
Call the Hunyuan image generation model directly via @cloudbase/js-sdk, without going through a cloud function as a proxy.
caution
Image generation requires @cloudbase/js-sdk >= 3.6.0 and is only supported in server-side (Node.js) environments. Browser-side environments do not support direct image generation calls. To generate images from a web page, use the Node SDK to create a cloud function as a server-side endpoint, then call it from the web page.
Prerequisites
- A CloudBase environment set up
@cloudbase/js-sdk3.6.0 or higher- Image generation resources granted by the Mini Program Growth Plan
Installation
npm install @cloudbase/js-sdk
For tree-shaking imports, register the AI module separately:
import cloudbase from "@cloudbase/js-sdk/app";
import { registerAi } from "@cloudbase/js-sdk/ai";
registerAi(cloudbase);
Usage Example
Basic Image Generation
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id",
});
const ai = app.ai();
const imageModel = ai.createImageModel("hunyuan-image");
imageModel.generateImageSubUrlConfig["hunyuan-image"]["HY-Image-3.0-Plus-4090-Tob-v1.0"] =
"images/ar/generations";
const res = await imageModel.generateImage({
model: "HY-Image-3.0-Plus-4090-Tob-v1.0",
prompt: "A chubby orange cat napping on a windowsill, watercolor style, warm tones",
size: "1024x1024",
revise: { value: true },
});
console.log(res.data[0].url); // Image URL, valid for 24 hours
console.log(res.data[0].revised_prompt); // Revised prompt