Twilio documentation is well regarded for being user friendly and helping developers learn how to integrate Twilio’s APIs into their applications. Sample code is shown across various tabs within the documentation supporting Node.js, Python, Java, C#, Ruby, PHP, and other languages. One tab that does not exist (and no one would ever expect it) is a tab for the Bourne-again shell.
This method comes in handy when purchasing a new Twilio number that is SMS ready. It takes about ten seconds to purchase a new number and it should take even less time to validate SMS works. Rather than configuring Postman or modifying a previous application it would be ideal to run a simple script in macOS Terminal.

curl -X POST \
    "https://api.twilio.com/2010-04-01/Accounts/ABCD1234/Messages" \
    -d "From=+14244881616" \
    -d "To=+18079072252" \
    -d "Body=Price is what you pay. Value is what you get." \
    -u "ABCD1234:EFGH5678"

Below is an explanation of the cURL flag options.

  1. -X tells curl to put the word POST in the HTTP Request. The default is GET.
  2. -d is the user data. From, To, and Body are required by Twilio.
  3. -u represents authentication credentials in the form of user:password. In this case it is accountSID:authTOKEN

Saving the script and reusing it later requires manually modifying in VIM each time a new Twilio number will be tested. The goal is have a better, faster, more efficient way of testing SMS with any Twilio number belonging to the account SID. Using variables in place of static -d and -u entries is more efficient and secure.

curl -X POST \
    "https://api.twilio.com/2010-04-01/Accounts/${account_sid}/Messages" \
    -d "From=${twilio_number}" \
    -d "To=${mobile_number}" \
    -d "Body=${sms_message}" \
    -u "${account_sid}:${auth_token}"

The script will now reference the value of the stored variables rather than manual entries. Variables can be temporarily or permanently assigned.

Open Terminal and add the variables to .bash_profile. The .bash_profile resides in the user home directory. If .bash_profile does not exist it is safe to create it.

$ ls -la .bash_p*
-rw-r--r-- 1 mh .bash_profile

$ vim ~/.bash_profile
export twilio_number="+14244881616"
export mobile_number="+18079072252"
export account_sid="ABCD1234"
export auth_token="EFGH5678"
export sms_message="Price is what you pay. Value is what you get."

After saving and exiting VIM enter source .bash_profile so the variables are loaded. Use the echo command for validation. To have .bash_profile load by default when opening Terminal add it to .bashrc. If .bashrc does not exist it is safe to create it.

$ source .bash_profile
$ echo $twilio_number

+14244881616

$ echo $sms_message

Price is what you pay. Value is what you get.

$ echo ". ~/.bash_profile" >> ~/.bashrc
$ more ~/.bashrc

. ~/.bash_profile

$ more ~/.bash_profile

export twilio_number="+14244881616"
export mobile_number="+18079072252"
export account_sid="ABCD1234"
export auth_token="EFGH5678"
export sms_message="Price is what you pay. Value is what you get."

At this point each time the cURL script executes it will use the variables loaded from .bash_profile. If there is a need to change the Twilio number simply enter the variable name followed by the new value then press enter.

$ twilio_number="+12244355103"
$ echo twilio_number

+12244355103

After executing the cURL script in Terminal a response from Twilio will display similar to the output below.

<TwilioResponse>
    <Message>
    <Sid>ABCD1234</Sid>
    <DateCreated>Sat, 24 Nov 2018 07:44:25 +0000</DateCreated>
    <DateUpdated>Sat, 24 Nov 2018 07:44:25 +0000</DateUpdated>
    <DateSent/>
    <AccountSid>ABCD1234</AccountSid>
    <To>+18079072252</To>
    <From>+14244881616</From>
    <MessagingServiceSid/>
    <Body>Price is what you pay. Value is what you get.</Body>
    <Status>queued</Status>
    <NumSegments>1</NumSegments>
    <NumMedia>0</NumMedia>
    <Direction>outbound-api</Direction>
    <ApiVersion>2010-04-01</ApiVersion>
    <Price/><PriceUnit>USD</PriceUnit>
    <ErrorCode/><ErrorMessage/>
    </Message>
</TwilioResponse>

When Terminal receives the confirmation from Twilio after the message is sent the formatting is not presented in a readable manner. Using Postman to query the account SID returns properly formatted JSON history.

Use the following URL and replace AccountSid with the proper SID.

https://api.twilio.com/2010-04-01/Accounts/{AccountSid}/Messages.json

Click Auth and select Basic Auth as the type. Enter the SID as the Username and the Auth Token as the Password.

After pressing Send in Postman the appropriate JSON will be returned by Twilio. Notice the Status header shows Delivered confirming Twilio delivered the SMS to the provider (in this case AT&T). Pressing Save will save the contents within the Postman environment folder. Pressing Download will prompt to save the JSON file somewhere on the computer.

 

Checking Apple Messages confirms the text was successfully delivered.

 

There are currently no comments.