What's the best way to convert text to base64 in Airtable's JavaScript editor?

Hey everyone! I’m stuck trying to make an API call from Airtable’s script editor. The GET request needs an Authorization header with a base64 encoded string of api: USERNAME:YOUR_API_KEY.

The problem is, btoa() isn’t available in Airtable. I found a workaround online, but it’s not working:

const EncodeHelper = {
  _chars: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  toBase64: function(input) {
    let output = '';
    let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    let i = 0;

    while (i < input.length) {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
        enc4 = 64;
      }

      output = output +
        this._chars.charAt(enc1) + this._chars.charAt(enc2) +
        this._chars.charAt(enc3) + this._chars.charAt(enc4);
    }
    return output;
  }
};

const authString = 'USER_NAME:API_KEY';
const encodedAuth = EncodeHelper.toBase64(authString);

const response = await fetch('https://api.example.com', {
  method: 'GET',
  headers: {
    'Authorization': 'Basic ' + encodedAuth
  }
});

I’m still getting 401 UNAUTHORIZED errors. I even tried encoding the string on my computer with btoa() and pasting the result, but no luck.

Any ideas on how to properly encode to base64 in Airtable’s editor? I’m out of ideas and really need to get this API connection working. Thanks for any help!

hey there! i’ve run into this before. try using the Buffer class instead:

const encodedAuth = Buffer.from('USER_NAME:API_KEY').toString('base64');

this should work in airtable’s script editor. make sure your api key is correct too. lemme know if u need more help!

I’ve faced this issue before in Airtable’s script editor. While the Buffer solution is popular, I found a more reliable method using the built-in ‘base64’ module. Here’s what worked for me:

const base64 = require('base64');
const authString = 'USER_NAME:API_KEY';
const encodedAuth = base64.encode(authString);

This approach is straightforward and doesn’t require external libraries. Make sure you’re using the correct API endpoint and that your API key has the necessary permissions. If you’re still getting 401 errors, it might be worth checking with the API provider to ensure there aren’t any additional authentication steps required. Also, double-check that you’re formatting the Authorization header correctly in your fetch request.

I’ve encountered similar issues with base64 encoding in Airtable’s script editor. One reliable method I’ve found is using the ‘crypto’ module, which is available in Airtable’s environment. Here’s a snippet that should work for you:

const crypto = require('crypto');
const authString = 'USER_NAME:API_KEY';
const encodedAuth = crypto.createHash('sha256').update(authString).digest('base64');

This approach uses SHA-256 hashing, which is more secure than standard base64 encoding. It’s worth noting that some APIs might expect a specific encoding method, so double-check their documentation. Also, ensure your API key is up-to-date and has the necessary permissions. If you’re still getting 401 errors after this, the issue might be on the API provider’s end or with how the Authorization header is being constructed in your request.