Next.js Discord

Discord Forum

Fetch from API (/app/api) in production ERROR. Next.js 13+

Unanswered
Thrianta posted this in #help-forum
Open in Discord
ThriantaOP
How do I fetch from next.js api (src/app/api folder) in production? In development I used localhost:3000/api/....

I tried environment variable passing API_URL, but on every single redeploy, vercel tries to host on new random generated domain and it fails. I cannot predict vercels randomized domain to set it in environment variables. I tried to set domain to a custom one in vercel settings, but I couldn't because it says Your most recent Production Deployment has failed.

5 Replies

Giant panda
From client side code you just fetch /api/foo without a base URL
From server side code you don't fetch at all and call the underlying logic directly
@Giant panda From client side code you just fetch `/api/foo` without a base URL
King Rail
Do you have any idea why i get the error: "Error: Failed to parse URL from /api/getAllProducts", when i try and fetch like that?
const res = await fetch(
    `/api/getAllProducts`,
    {
      method: "GET",
      headers: {
        "content-type": "application/json",
      },
      next: { revalidate: 86400 },
    }
  )

Am i doing something wrong?
Giant panda
Sounds like you are fetching your own API from the server-side? If so, don't.
King Rail
Oh you are right. I think i tested on the only ssr rendered page on my site... Thank you!