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, I can’t use btoa() in Airtable. I found a workaround online, but it’s not working. Here’s what I tried:

const EncodeHelper = {
  keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  encode: function(input) {
    // Encoding logic here (shortened for brevity)
  }
};

const authString = 'MY_USERNAME:MY_API_KEY';
const encodedAuth = EncodeHelper.encode(authString);

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

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

Does anyone know how to properly encode strings to base64 in Airtable’s environment? I’m out of ideas and could really use some help. Thanks!

I encountered a similar issue when working with Airtable’s script editor. The solution that worked for me was using the built-in ‘base64’ input/output encoding option in Airtable’s scripting environment. Here’s how you can modify your code:

const authString = 'MY_USERNAME:MY_API_KEY';
const encodedAuth = output.encode(authString, 'base64');

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

This approach leverages Airtable’s native encoding functionality, which should resolve the 401 UNAUTHORIZED errors you’re experiencing. Make sure to replace ‘MY_USERNAME:MY_API_KEY’ with your actual credentials. If you’re still facing issues, double-check your API endpoint and ensure your credentials are correct.

hey mikezhang, have u tried using the Buffer class? it’s built into node.js which airtable uses. try this:

const authString = ‘MY_USERNAME:MY_API_KEY’;
const encodedAuth = Buffer.from(authString).toString(‘base64’);

should work for ur API call. lmk if u need more help!

I’ve had success using a custom Base64 encoding function in Airtable’s script editor. Here’s a reliable approach that worked for me:

function base64Encode(str) {
    const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    let encoded = '';
    for (let i = 0; i < str.length; i += 3) {
        const c1 = str.charCodeAt(i), c2 = str.charCodeAt(i+1), c3 = str.charCodeAt(i+2);
        encoded += chars.charAt(c1 >> 2);
        encoded += chars.charAt(((c1 & 3) << 4) | ((c2 || 0) >> 4));
        encoded += chars.charAt(c2 ? (((c2 & 15) << 2) | ((c3 || 0) >> 6)) : 64);
        encoded += chars.charAt(c3 ? (c3 & 63) : 64);
    }
    return encoded;
}

const authString = 'MY_USERNAME:MY_API_KEY';
const encodedAuth = base64Encode(authString);

// Use encodedAuth in your API call

This function handles the encoding manually, avoiding compatibility issues with Airtable’s environment. It’s been reliable for me across various projects. Just make sure your API credentials are correct, and you should be good to go.