Next.js Discord

Discord Forum

Am I supposed to use VERCEL_URL as an environment variable to request APIs after deployment?

Unanswered
Japanese pilchard posted this in #help-forum
Open in Discord
Japanese pilchardOP
Hey guys, I deployed my first production-scale project on Vercel today and I am facing issues sending requests to APIs. I was aware that I cannot hard code my URI to localhost:3000 when sending API requests so I added a dedicated environment variable and set it to localhost:3000, I would later change this to whatever domain was assigned to me by Vercel. After deploying the app, I am unable to send API requests from the live website (it results in 500 Internal Error), however when I send requests from ThunderClient (which is a Postman like software to send API requests) to the live website, with the correct Authorization token in the header, I get the JSON values I am looking for.

How am I supposed to send API requests to my routes, is it using the VERCEL_URL or is there any other way?

My current domain name is "https://lifestyleplusconstruction.vercel.app", therefore making a request to https://lifestyleplusconstruction.vercel.app/api/xyz with the correct request token returns the correct JSON, just not from the website.

This is my first post here so I apologize for the length and the quality of the question, please let me know if I need to add any other details, any help is greatly appreciated. Thank you for your time!

3 Replies

Don't call route api ie: route.ts from your server component. Read Data fetching patterns in the documentation
Japanese pilchardOP
This project was created using the pages router so I have api folders, however, am I not supposed to call the apis from getServerSideProps?
Himalayan
Not using fetch you should call the logic that gets the data for the api completely going around the api itself