Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NextJS Build Url Path with ENV variables

My project uses NextJS 13.1.6

I’m having trouble building a url through string concatenation using environment variables.

env.local:
 WEBSITE_URL: 'https://somewebsite.com/api/input';
 SECRET_KEY: '1234abcd';

src/pages/api/api-requests.js
  const response = await fetch(`${process.env.WEBSITE_URL}?secret=${process.env.SECRET_KEY}`)

This url produces this output:
 "'https://www.somewebsite.com/api/input';?secret='1234abcd';'"

As you can see there are odd characters in that url string – semi-colons ; and quotes ”

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

No idea what I’m doing wrong here…

Any help appreciated!

>Solution :

If you .env file look like this

WEBSITE_URL = 'https://www.somewebsite.com/api/input';

remove ; and '' as Scottish Smile commented. Because in .envfile all variables are read as strings, so you don’t need to specify

WEBSITE_URL = https://www.somewebsite.com/api/input
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading