import OpenAI from ‘openai’;
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
const generateImage = async (req, res) => {
try {
const response = await openai.createImage({
model: “dall-e-3”,
prompt: “a white siamese cat”,
n: 1,
size: “1024x1024”,
response_format: “url”,
});
image_url = response.data.data[0].url;
res.status(200).json({
success: true,
data: imageUrl, // Corrected variable name
});
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
res.status(400).json({
success: false,
error: ‘The image could not be generated’,
});
}
};
export { generateImage };