wx-server-sdk
Call the Hunyuan image generation model from a WeChat mini program cloud function using the built-in wx-server-sdk. No additional installation is required — wx-server-sdk is pre-installed in the WeChat cloud function environment.
Image generation is only supported in cloud functions. It cannot be called directly from mini program client code.
Prerequisites
- A WeChat CloudBase environment set up
- wx-server-sdk 3.0.5-beta.1 or higher (required for
cloud.ai()) - Image generation credits from the Mini Program Growth Initiative
The cloud.ai() API requires wx-server-sdk 3.0.5-beta.1 or higher. Specify the version in your cloud function's package.json:
{
"dependencies": {
"wx-server-sdk": "3.0.5-beta.1"
}
}
Creating an Image Generation Cloud Function
Create a new cloud function and add the following code to index.js:
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
timeout: 150000, // HTTP request timeout 150s, overrides the ~15s default
})
const ALLOWED_SIZES = ['1024x1024', '1280x720', '720x1280', '1280x1280']
exports.main = async (event, context) => {
const prompt = (event.prompt || '').trim()
if (!prompt) {
return { error: 'Please enter a prompt' }
}
if (prompt.length > 500) {
return { error: 'Prompt must be 500 characters or less' }
}
const size = ALLOWED_SIZES.includes(event.size) ? event.size : '1024x1024'
const imageModel = cloud.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, // Optimized 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 Mini Program
After deploying the cloud function, call it from your mini program:
const res = await wx.cloud.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. To keep images permanently, download and upload them to cloud storage within the cloud function:
const https = require('https')
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
timeout: 150000,
})
exports.main = async (event, context) => {
const prompt = (event.prompt || '').trim()
const imageModel = cloud.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 cloud.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)
})
})
}