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 either:
const EncodeHelper = {
keySet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
toBase64: function(input) {
// Simplified encoding logic
let output = '';
for (let i = 0; i < input.length; i += 3) {
// Encoding logic here
}
return output;
}
};
const credentials = 'MY_USERNAME:MY_API_KEY';
const encodedCreds = EncodeHelper.toBase64(credentials);
const response = await fetch('https://api.example.com', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + encodedCreds
}
});
I keep getting 401 UNAUTHORIZED errors. I even tried encoding the string on my computer and pasting it in, 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 had success using the Buffer class in Airtable’s scripting environment for base64 encoding. It’s a reliable method that doesn’t require custom implementations. Here’s a concise example:
const encodedCreds = Buffer.from('USERNAME:API_KEY').toString('base64');
This generates the base64 string you need for the Authorization header. If you’re still getting 401 errors after this, the issue might be with your API credentials or the API endpoint itself. Double-check those details, and ensure you’re including the 'Basic ’ prefix in your Authorization header. Also, some APIs require additional headers or specific request formats, so review the API documentation thoroughly.
I’ve encountered this issue before, and I found a reliable solution that works in Airtable’s JavaScript environment. Instead of trying to implement base64 encoding yourself, you can use the built-in Buffer
class, which is available in Airtable’s scripting context.
Here’s how you can modify your code to make it work:
const credentials = 'MY_USERNAME:MY_API_KEY';
const encodedCreds = Buffer.from(credentials).toString('base64');
const response = await fetch('https://api.example.com', {
method: 'GET',
headers: {
'Authorization': 'Basic ' + encodedCreds
}
});
This approach is simpler and more robust than trying to implement your own base64 encoding function. It should resolve the 401 UNAUTHORIZED errors you’re experiencing.
If you’re still having issues after trying this method, double-check that your API credentials are correct and that you’re using the right endpoint for the API. Sometimes, the problem lies in the API configuration rather than the encoding process.
yo, i’ve seen this before. the buffer trick really works. just do: const encodedCreds = Buffer.from(‘USER:PASS’).toString(‘base64’); then use it in your header. if 401 persists, check your creds and endpoint. good luck!