Node SDK
Call the Hunyuan image generation model from a cloud function or cloud-hosted service using @cloudbase/node-sdk. Supports custom image sizes, prompt rewriting, and thinking mode.
Image generation is only supported in server-side environments (cloud functions / cloud hosting). It cannot be called directly from mini program or web page client code.
Prerequisites
- A CloudBase environment set up
- Image generation credits from the Mini Program Growth Initiative
Installation
npm install @cloudbase/node-sdk
Creating an Image Generation Cloud Function
After creating a new cloud function and installing the dependency, add the following code:
const cloudbase = require('@cloudbase/node-sdk')
const app = cloudbase.init({ env: process.env.ENV_ID })
const ai = app.ai()
exports.main = async (event) => {
const { prompt, size = '1024x1024' } = event
const imageModel = ai.createImageModel('hunyuan-image')
const res = await imageModel.generateImage({
model: 'hunyuan-image-v3.0-v1.0.4',
prompt,
size,
revise: { value: true },
enable_thinking: { value: false },
})
return {
url: res.data[0].url, // Image URL, valid for 24 hours
revisedPrompt: res.data[0].revised_prompt,
}
}
Image generation can take a long time (enabling revise adds approximately 10 seconds; enabling thinking mode adds up to 60 seconds). It is recommended to set the cloud function timeout to 900 seconds.
Calling from a Web Page
Call the cloud function from a web page using @cloudbase/js-sdk:
npm install @cloudbase/js-sdk
import cloudbase from '@cloudbase/js-sdk'
const app = cloudbase.init({ env: 'your-env-id' })
await app.auth().anonymousAuthProvider().signIn()
const res = await app.callFunction({
name: 'generateImage',
data: {
prompt: 'A chubby orange cat napping on a windowsill, watercolor style, warm tones',
size: '1024x1024',
},
})
console.log(res.result.url) // Image URL
console.log(res.result.revisedPrompt) // Rewritten prompt
Saving Images to Cloud Storage
Generated image URLs expire after 24 hours. It is recommended to upload the image to cloud storage directly within the cloud function:
const https = require('https')
const cloudbase = require('@cloudbase/node-sdk')
const app = cloudbase.init({ env: process.env.ENV_ID })
const ai = app.ai()
exports.main = async (event) => {
const { prompt } = event
// Generate the image
const imageModel = ai.createImageModel('hunyuan-image')
const res = await imageModel.generateImage({
model: 'hunyuan-image-v3.0-v1.0.4',
prompt,
revise: { value: true },
enable_thinking: { value: false },
})
// Download and upload to cloud storage
const imageBuffer = await downloadImage(res.data[0].url)
const uploadRes = await app.uploadFile({
cloudPath: `ai-images/${Date.now()}.jpg`,
fileContent: imageBuffer,
})
return {
fileID: uploadRes.fileID,
revisedPrompt: res.data[0].revised_prompt,
}
}
function downloadImage(url) {
return new Promise((resolve, reject) => {
https.get(url, (response) => {
const chunks = []
response.on('data', (chunk) => chunks.push(chunk))
response.on('end', () => resolve(Buffer.concat(chunks)))
response.on('error', reject)
})
})
}