Enviroment variables

Hello!

I have an environment variable that has a # in it, for example:

TOKEN='Bearer Bi5yCZ1UawinafopianpieaigenEU^XWkE\$Jhcumfw0i#!7oH@X9zag'

In my local environment, it works fine when I wrap it in quotes, but it seems like that doesn’t work in Vercel. Is there a fix for this? I already tried escaping it with \.

Thanks!

1 Like

@babylon1999
Try using double quotes, too, just to see if it works ("VALUE" instead of 'VALUE'). If that does not work, you could encode it as a Base64, which will eliminate the # character, and decode the Base64 to a normal string in your code.


Base64 Encoding Example:

.env or Vercel environment variables:

TOKEN="QmVhcmVyIEJpNXlDWjFVYXdpbmFmb3BpYW5waWVhaWdlbkVVXlhXa0VcJEpoY3VtZncwaSMhN29IQFg5emFn"

Somewhere in your code:

const encodedToken = process.env.TOKEN;
const decodedToken = Buffer.from(encodedToken, "base64").toString("utf-8");

// Do something with the decodedToken!

If that does not work, you could encode it as a URI, which should also eliminate the # character, and decode the URI to a normal string in your code.

URI Encoding Example (does not work for this specific case)

.env or Vercel environment variables:

TOKEN="Bearer%20Bi5yCZ1UawinafopianpieaigenEU%5EXWkE%24Jhcumfw0i%23!7oH%40X9zag"

Somewhere in your code:

const encodedToken = process.env.TOKEN;
const decodedToken = decodeURIComponent(encodedToken);

// Do something with the decodedToken! 

Base64 encoding did the trick, thanks!

It seems weird that not a lot of people are opening issues about this, tokens usually have all sorts of weird characters, there should be some sort of manual escaping in the Vercel dashboard.

1 Like

@babylon1999 Awesome!
Also, did URI encoding not work? Perhaps I should update my answer with Base64…

No, it didn’t for some reason.

1 Like

@babylon1999 Thanks for letting me know, I updated the answer. :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.