What is the name of the domain?
What is the error number?
What is the error message?
Internal Server Error
What is the issue you’re encountering
I am having problems with deploying my NextJS app, which is using an API endpoint for chat completions from OpenRouter AI. The API endpoint below worked well on my local, but when I wanted to deploy my app on Cloudflare, the Internal Server Error 500
keeps showing up. // app/api/ai/route.ts import { NextResponse } from "next/server"; // Specify Edge runtime export const runtime = "edge"; export async function POST(req: Request) { try { const { message } = await req.json(); const response = await fetch("https://openrouter.ai/api/v1/chat/completions", { method: "POST", headers: { "Authorization": `Bearer ${process.env.NEXT_PUBLIC_OPENROUTER_API_KEY}`, "Content-Type": "application/json", }, body: JSON.stringify({ model: "meta-llama/llama-3.3-70b-instruct:free", messages: [ { role: "user", content: systemPrompt, // I have a custom prompt here }, ], }), }); if (!response.ok) { throw new Error(`OpenRouter API responded with status: ${response.status}`); } const result = await response.json(); return NextResponse.json({ content: result.choices[0].message.content }); } catch (error: any) { console.error("AI API error:", error.message); return NextResponse.json({ error: error.message }, { status: 500 }); } }
The client calling the API is just basic: try { const res = await fetch("/api/chat", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ messages: transformedData }), }); toast.dismiss(); if (!res.ok) { const errorText = await res.text(); toast.error(`AI failed to think..., ${errorText}`); throw new Error(errorText); } const responseText = await res.json(); const clean = DOMPurify.sanitize(responseText.content, { USE_PROFILES: { html: true }, }); setResponse(clean); toast.success("AI has responded!"); } finally { setProcessing(false); }
I am suspecting some problems with the Edge
runtime compatibility between Cloudflare and NextJS. But I still struggle a lot and can’t still figure it out. I don’t know what I am missing here. Please help if you can. Thanks a lot in advance, guys!
What steps have you taken to resolve the issue?
I have tried to deploy just a bare simple app which only has the UI and the API on Vercel, but the error Internal Server Error 500
is still there.
I also tried to make the API call’s base URL through AI gateway of Cloudflare (OpenRouter · Cloudflare AI Gateway docs), but it is not helping.
In addition, I tried to use the OpenAI SDK to make the chat completions, but it still worked well on local, but can’t on Cloudflare.
Was the site working with SSL prior to adding it to Cloudflare?
Yes
What is the current SSL/TLS setting?
Full
What are the steps to reproduce the issue?
Try to make an API call (NextJs app) for AI chat completion (Base URL from OpenRouter AI) from Cloudflare website → Worked on Local. But failed on Cloudflare production with the code 500 Internal Server Error.