Btoa method is not defined

Hello community.

I am trying to encode the API Key in B64 to been able to have a secured communication. Baed on what I have read at different forums, including : How do you encode api key to base64 for freshdesk?
It suggests to use the method: btoa, but it always sayis that that bethod is not defined:

I have a serverless app, so I retrieve the iparams:

    //console.log(args);
    var subdomain = args['iparams']['fd_domain'];
    var subdomain = args['iparams']['api_key'];
    var EncodedAuthentication = btoa(apiKey.value);
    console.log(EncodedAuthentication); 

And that is the ouput:

Can somebody assists me?

Regards

1 Like

Hey @Chavas

Can you try this out - Node.js throws "btoa is not defined" error - Stack Overflow. Basically, btoa is not supported in node > v4. They recommend using this

Buffer.from(apiKey.value).toString('base64')

You can also use this :link: base-64 - npm

2 Likes

Hello @shravan.balasubraman thanks for your reply.

Sadly I already tried it, and still happening the same>
image

Hi,

Have you tried the base-64 module.

const base64 = require(‘base-64’);
and then I can use it easily like below. works fine.
headers: {

  Authorization: `Basic ${base64.encode(args.iparams.freshdeskapikey + ':X')}`

}
1 Like

Hi thanks all.
Finally I have solved it.

Step 1. (Only if required)
If you do not have the btoa on your local machine, download it. To do so, you simply need to open your console and set the next command:

npm install btoa

Step 2.
At the manifest, include the dependecy of the btoa:

{
  "platform-version": "2.0",
  "product": {
    "freshdesk": {}
  },
  "whitelisted-domains": [
    "https://DOMAIN.freshdesk.com"
],
  "dependencies": {
    "btoa" : "1.2.1"
  } 
}

*It might be needed another version. Check the version that you are using from Step 1

Step 3.
At the beginig of your server.js you include:

var btoa = require('btoa');

That has made that the line:

var EncodedAuthentication = btoa(apiKey+":X");

Works without any problem.

Hope it helps.

Regards

2 Likes

If you are using any script file and getting "Uncaught ReferenceError: x is not defined " which means ‘x’ is either a variable or a method which you are trying to use before declaring it using var keyword. This means that there is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope otherwise , it will endup throwing this ‘x’ is not defined error . This usually indicates that your library is not loaded and JavaScript does not recognize the ‘x’.

To solve this error: Load your library at the beginning of all your scripts.

There can be multiple other reasons for this issue:

  • Conflict with Other Libraries
  • Path to your library included is not correct
  • Llibrary file is corrupted
  • Working offline (when you use CDN)