Converting text to base64 in Airtable's JavaScript environment?

I’m 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 CustomBase64 = {
  encode: function(input) {
    // Simplified encoding logic
    let output = '';
    for (let i = 0; i < input.length; i++) {
      output += input.charCodeAt(i).toString(36);
    }
    return output;
  }
};

const authString = 'USER:PASS';
const encodedAuth = CustomBase64.encode(authString);

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

I keep getting 401 UNAUTHORIZED errors. I even tried encoding the string on my computer and pasting the result, but no luck.

Any ideas on how to properly encode to base64 in Airtable’s environment? I’m stuck and could really use some help. Thanks!

I’ve dealt with this issue before in Airtable’s scripting environment. Here’s a reliable method I’ve used for base64 encoding without external libraries:

function base64Encode(str) {
    const b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    let o1, o2, o3, h1, h2, h3, h4, bits, i = 0, enc = '';
    
    do {
        o1 = str.charCodeAt(i++);
        o2 = str.charCodeAt(i++);
        o3 = str.charCodeAt(i++);
        
        bits = o1 << 16 | o2 << 8 | o3;
        
        h1 = bits >> 18 & 0x3f;
        h2 = bits >> 12 & 0x3f;
        h3 = bits >> 6 & 0x3f;
        h4 = bits & 0x3f;
        
        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < str.length);
    
    const r = str.length % 3;
    return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
}

const authString = 'api:USERNAME:YOUR_API_KEY';
const encodedAuth = base64Encode(authString);

// Use encodedAuth in your API call headers

This should resolve your 401 errors. Remember to replace the placeholder credentials with your actual API details.

I’ve encountered a similar issue in Airtable’s script environment. The problem with your current approach is that it’s not true base64 encoding, which is why you’re getting unauthorized errors.

Here’s a solution that worked for me:

function base64Encode(str) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    let output = '';
    str = String(str);
    for (let block = 0, charCode, i = 0, map = chars;
         str.charAt(i | 0) || (map = '=', i % 1);
         output += map.charAt(63 & block >> 8 - i % 1 * 8)) {
        charCode = str.charCodeAt(i += 3/4);
        block = block << 8 | charCode;
    }
    return output;
}

const authString = 'api:USERNAME:YOUR_API_KEY';
const encodedAuth = base64Encode(authString);

// Use encodedAuth in your API call

This function implements base64 encoding without relying on btoa(). It should work in Airtable’s environment and produce a valid base64 string for your Authorization header. Make sure to replace ‘USERNAME’ and ‘YOUR_API_KEY’ with your actual credentials.

hey mate, i had similar trouble. try this lib i found called ‘js-base64’. u can import it like this:

import { Base64 } from 'js-base64';

then just use Base64.encode() for ur auth string. worked like a charm for me in airtable scripts!