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 = {
keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
encode: function(input) {
// ... (encoding logic here)
}
};
const credentials = 'MY_USERNAME:MY_API_KEY';
const encodedCreds = EncodeHelper.encode(credentials);
const response = await fetch('https://example-api.com', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + encodedCreds
}
});
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 out of ideas and could really use some help. Thanks!
I’ve encountered a similar issue when working with Airtable’s script editor. The built-in Buffer class actually provides a reliable way to encode strings to base64. Here’s how I solved it:
const credentials = 'api:USERNAME:YOUR_API_KEY';
const encodedCreds = Buffer.from(credentials).toString('base64');
const response = await fetch('https://example-api.com', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + encodedCreds
}
});
This approach has worked consistently for me across different Airtable bases and API integrations. Make sure you’re using the correct API endpoint and that your credentials are valid. If you’re still getting 401 errors, double-check your API key and ensure it has the necessary permissions for the operation you’re trying to perform. Hope this helps!
hey there, i’ve run into this before. try using the built-in ‘atob()’ function instead of ‘btoa()’. it’s weird but it works for base64 encoding in airtable. just do:
const encodedCreds = atob(‘api:USERNAME:YOUR_API_KEY’);
make sure ur api key is right tho. good luck!
I’ve had success using the Buffer class for base64 encoding in Airtable’s script editor, but there’s another approach worth considering. You can use the TextEncoder and btoa functions together to achieve the same result:
const credentials = 'api:USERNAME:YOUR_API_KEY';
const encoder = new TextEncoder();
const encodedCreds = btoa(String.fromCharCode(...encoder.encode(credentials)));
const response = await fetch('https://example-api.com', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + encodedCreds
}
});
This method has worked reliably for me across various Airtable projects. If you’re still encountering issues, double-check your API endpoint and ensure your credentials are correct. Also, verify that your API key has the necessary permissions for the operation you’re attempting.