Next.js Discord

Discord Forum

Fetch api endpoint from api endpoint

Answered
Podenco Canario posted this in #help-forum
Open in Discord
Podenco CanarioOP
Hi 👋

Sorry if it's kind of a dumb question, context is NextJS App Router :
I have a file src/app/api/myfirstendpoint/route.js where I want to
fetch('api/mysecondendpoint/', {
  method: 'POST',
  body: JSON.stringify({ superData: myData })
})


But I get Failed to parse URL from /api/mysecondendpoint
Is it weird to do it like this ? How can I manage to have a relative path for my endpoint from an actual endpoint ?
For more context I receive on myfirstendpoint a Webhook with a lot of possible data types, and I want to parse the POST request sent from Webhook to other endpoints like mysecondendpoint.

I might be mislead on how an API should actually work, I could do my database insert/delete directly, but I wondered if doing endpoint to endpoint communication a thing ?
Answered by B33fb0n3
instead of calling your own server from your server, only execute the same code on your endpoint. Like that you have less network requests. If you still want to fetch your own endpoint, make the path absolute like https://example.de/api/your/route/
View full answer

3 Replies

Answer
Podenco CanarioOP
Ok I have three different servers (local, preprod and prod) so I wanted to do a proper relative path.
But I guess this is just not something I should do. Like you said, it'll just add to the network requests for no real interest.
I'll just directly insert things in my DB instead.
Thanks 👍
happy to help