Help

ERROR: btoa is not defined

Topic Labels: Automations
Solved
Jump to Solution
617 2
cancel
Showing results for 
Search instead for 
Did you mean: 

Hi everyone,

I am trying to automate an SMS using a basic Twilio script that I found here:
Twilio Script

This script works when I run it within the script app, even with my changes made to disable to input text. When I run the same code in the automation run script action I get the error code ‘ReferenceError: btoa is not defined’.

My code is below:

* Twilio send SMS script
 *
 * Send a single SMS to a provided telephone number using the Twilio service
 *
 * The Airtable "Send SMS" Block (available to customers with a "Pro" account)
 * is capable of sending many messages in batches based on the contents of the
 * Airtable Base in which it is installed.
 *
 * **Notes on adapting this script.**
 *
 * The script prompts for input every time it is run. For some users, one or
 * more of these values may be the same with every execution. To streamline
 * their workflow, these users may modify this script by defining the constant
 * values in the first few lines. The values should be expressed as JavaScript
 * strings in the object named `hardCoded`.
 */
'use strict';

/**
 * Users may provide values for any of the properties in the following object
 * to streamline the script's startup.
 */
const hardCoded = {
    twilioAccountSid: 'XXX',
    twilioSendingNumber: 'XXX',
    // Note: the code in Airtable scripts is visible to all users of the
    // Airtable base. By entering the Twilio Auth Token here, all users will
    // have access to that sensitive information.
    receivingNumber: 'XXX',
    messageBody: 'XXX',
    twilioAuthToken: 'XXX'
};

const twilioAccountSid =
    hardCoded.twilioAccountSid || (await input.textAsync('Twilio Account SID'));
const receivingNumber = hardCoded.receivingNumber;
const messageBody = hardCoded.messageBody;
const twilioSendingNumber =
    hardCoded.twilioSendingNumber || (await input.textAsync('Twilio sending telephone number'));
const twilioAuthToken = hardCoded.twilioAuthToken || (await input.textAsync('Twilio Auth Token'));

const url = `https://api.twilio.com/2010-04-01/Accounts/${twilioAccountSid}/Messages.json`;
const headers = {
    // Format the "Authorization" header according to "HTTP Basic Auth"
    Authorization: `Basic ${  btoa(`${twilioAccountSid}:${twilioAuthToken}`)}`,
    // Twilio expects request data to be formatted as though it were submitted
    // via an HTML form
    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
};
const body = new URLSearchParams({
    From: twilioSendingNumber,
    To: receivingNumber,
    Body: messageBody
});

let result;

try {
    let response = await fetch(url, {
        method: 'POST',
        headers,
        body
    });

    if (!response.ok) {
        result = `Error sending SMS: "${await response.text()}"`;
    } else {
        result = 'SMS sent successfully.';
    }
} catch (error) {
    result = `Error sending SMS: "${error}"`;
}

output.text(result);

Can anyone provide me with some help here?

Thank you

1 Solution

Accepted Solutions

Welcome to the Airtable community.

You have run into one of the differences between scripting app and automation action scripts. You can’t use btoa in an action script. No idea why.

You can run the btoa conversion in scripting app, output the resulting string, and substitute that in place of the btoa in this automation script.

See Solution in Thread

2 Replies 2

Welcome to the Airtable community.

You have run into one of the differences between scripting app and automation action scripts. You can’t use btoa in an action script. No idea why.

You can run the btoa conversion in scripting app, output the resulting string, and substitute that in place of the btoa in this automation script.

Welcome to the community, @Nicholas_Payne! :grinning_face_with_big_eyes: If @kuovonne’s answer solved your problem, please mark her comment as the solution to your question. This helps others who may be searching with similar questions. Thanks!