anycoder-873d13ec / pages /api /generate-image.js
shimanta420's picture
Upload pages/api/generate-image.js with huggingface_hub
d63450c verified
raw
history blame contribute delete
682 Bytes
import { Configuration, OpenAIApi } from 'openai'
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
})
const openai = new OpenAIApi(configuration)
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ message: 'Method not allowed' })
}
try {
const { prompt } = req.body
const response = await openai.createImage({
prompt: prompt,
n: 1,
size: "512x512",
})
res.status(200).json({ url: response.data.data[0].url })
} catch (error) {
console.error('OpenAI API error:', error)
res.status(500).json({ error: 'Failed to generate image' })
}
}