Hi there. I successfully did the chat completions api but when i change the endpoint and body for image generations i get a âFailed to fetchâ error.
try {
const openaiResponse = await fetch(âhttps://api.openai.com/v1/chat/completionsâ, {
method: âPOSTâ,
headers: {
âAuthorizationâ: Bearer ${API_KEY}
,
âContent-Typeâ: âapplication/jsonâ,
},
body: JSON.stringify({
model: âgpt-4oâ,
messages: [
{ role: âsystemâ, content: âYou are a helpful game assistant.â },
{ role: âuserâ, content: prompt }
],
temperature: 0.7,
}),
})
if (!openaiResponse.ok) {
const jsonResp = await openaiResponse.json()
return {
statusCode: openaiResponse.status,
body: JSON.stringify({
success: false,
error: jsonResp,
}),
}
} else {
const jsonResp = await openaiResponse.json()
return {
statusCode: openaiResponse.status,
body: JSON.stringify({
success: true,
imageInfo: jsonResp,
}),
}
}
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify({ error: 'OpenAI API call failed' }),
};
}
and i check the response in game.js like this
game(â/startGameâ, {
method: âPOSTâ,
})
.then((body) => {
if(!body.data.success){
console.log(âchecking body successâ)
AIWord = JSON.stringify(body.data.error, null, 2)
} else {
AIWord = JSON.stringify(body.data.imageInfo, null, 2)
}
})
.catch((err) => {
console.error(err)
})