Help with Twilio REST API POST Function

Hello,

I have difficulty in creating the blocks for a Twilio POST Function as suggested from their Documentation listed below:

curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json
–data-urlencode “Body=Hi there”
–data-urlencode “From=+15017122661”
–data-urlencode “To=+15558675310”
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

Should I set the webAPI Body as a string joining the Body, From and To?
Body=Hi there
From=+15017122661
To=+15558675310

Should I have Headers like :
Content-type: application/x-www-form-urlencoded
Authorization : Bearer $TWILIO_AUTH_TOKEN

Where to place the Username and Password ?
$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

These are the Blocks I used
image

image

image

I have “Network Request Failed Error” on my android device and “undefined” in my iPhone device

Same error if I used the following:

I appreciate much your help on this. Thank you in advance.

1 Like

What is in your Headers block?

Can you show the complete blocks of the body?

In your Post block you are only displaying error block but not status or response. What do they contain?

Hi Muneer,

Here are my answers to your questions:

What is in your Headers block?

  • I asked about the Headers in my post above although I tried the following below :
    Content-type: application/x-www-form-urlencoded
    Authorization : Bearer $TWILIO_AUTH_TOKEN

Can you show the complete blocks of the body?

  • The Body block is also posted above. Please make suggestions if I am wrong . I reposted here below:
    image

In your Post block you are only displaying error block but not status or response. What do they contain?

  • I have 400 in android and 401 in Apple device for their respective statuses.

Again, I need help because I don’t know what Headers, Query Parameters (if there are) and Objects should I initialize and put into my Blocks. I also need help with the -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN too. Where to initialize them.

Thanks.

1 Like

Thank you for the reply.

I only asked the questions after I went through your post and rewriting the same info will not help at all.

I need to know is this part of the Web API component setup or in a Headers code block. If it is part of the code, then show the image. You can of course redact the sensitive information. Is it an object or a text?

I asked for the complete and you provided exactly what was there. If it was sufficient I wouldn’t have asked for a better image.

What is available after the From=+44393…? Is there any other thing after the complete number? I’m asking about special characters used in formatting.

From the status of 400 the most possible cause is the format of your data. One common format used in API calls is to mimic the URL parameter format which means separating parameters by & sign. Try it and see if it solves the issue.

  • 400 means the format of the information is wrong. This is connected with the information you supplied in the body section. Unfortunately, you decided not to show the complete block to see if you are following a specific format.

  • 401 is authorization problem.

Hi muneer,

Thank you for your response.

Right from the start, I admitted that I need help in putting the POST for this Twilio curl command. I don’t know how to put them into Thunkable Blocks, I don’t know what should I put as Headers or Query Parameters, or even the Body Parameters. The Blocks that I used are just my tries, and of course, because they are wrong, they are giving me errors, 400 and 401 statuses.

I’m sure, you are a Thunkable Expert, so, could you please show me your Thunkable Blocks for the following :
curl -X POST https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json
–data-urlencode “Body=Hi there”
–data-urlencode “From=+15017122661”
–data-urlencode “To=+15558675310”
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

I got this from this link : Make an HTTP Request to Twilio - Twilio

Thank you again.

1 Like

Usually this goes into the body section but with the following format.
“Body=Hi%20there&From=+15017122661&To=+15558675310”
Use the same header that you used already.

For the first test, use all hard-coded in the body section. Once you see it working then you can use the variables to fill-in user data.

Hi muneer,

I followed your suggestions:

Headers
Content-type: application/x-www-form-urlencoded
Authorization : Bearer $TWILIO_AUTH_TOKEN

URL
https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json

Body
Body=Hi%20there&From=+15017122661&To=+15558675310

Status
401

Error
Undefined

If you could tell me how to transform this → -u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN into the webAPI, I think this will solved the 401 status.

Thanks.

1 Like

This Authorization : Bearer $TWILIO_AUTH_TOKEN is supposed to be the encrypted version of this $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN.

Another way is to code it this way:
https://$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN@api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json

1 Like

Hi muneer,

I have tried both of your suggestions for $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

This : https://$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN@api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json

and this : Authorization : Bearer $TWILIO_AUTH_TOKEN

and the Body is : Body
Body=Hi%20there&From=+15017122661&To=+15558675310

Any comment on my Content-type: application/x-www-form-urlencoded This is also on my Headers. Do you think this has an effect on the result?

Yes, I admit, I forgot to mention. This was initialized in the Simple Settings of webAPI Object. I didn’t use Blocks for the Headers.

1 Like

If you get 401 then the $TWILIO_AUTH_TOKEN is not correct. You need to check with your account details in Twilio and get the correct code.

Content-Type is correct as far as the examples in the documentation provides.

See this from the documentation of Twilio
image

This is the full example URL

Another suggestion in the documentation to use Basic instead of Bearer
Authorization: Basic

1 Like

I will try the Authorization: Basic

Thanks.

1 Like

Hi @muneer,

This is now my working Blocks.
image

I read an article about postman.com, I used the curl statement Twilio provided, put them each in their corresponding places. The most important thing is in the Authorization Tab, I selected Basic Auth and typed my $TWILIO_ACCOUNT_SID in the Username Entry and $TWILIO_AUTH_TOKEN in the Password Entry. After that, I opened the Headers Tab including the Hidden Auto-generated Headers, I saw the base64 encoded Authorization. This is in the form of Basic set-of-random-characters different from the $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN. This is what you are referring about encrypted $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN. I copied that and placed it in my Headers in Thunkable along side with my current Content-Type:application/x-www-form-urlencoded

There is no need to use %2B or %20 for the Body, From and To in the Body Block. It has a 201 status and the SMS is sent to the Phone Number stored in my variable. Phone number formats should follow the format below:
image

Thank you.

1 Like

So I concluded from above that you were getting 401 because the set-of-random-characters were not the correct one. You had them generated by Postman and used it in your app and it worked.

Well done.

Basically, you stated the same name, so I assumed they are the same, and the encryption site I found first was not the right one.

Anyways, I hope this will help others.

1 Like

OK, sorry for that, it must by a typo mistake (more correctly, a copy/paste mistake). Should you have pointed it out at that time it would have been corrected on the spot.

Now, that the issue is solved, enjoy the app running as expected.