1
votes

I'm trying to send an SMS using twilio, without specifying a specific From number, instead letting Copilot chose the best number for me (as documented in https://www.twilio.com/docs/api/rest/sending-messages#post-parameters-conditional).

However, when sending a request, the Twilio REST API complains that I did not specify a From number. Specifying a From number, as expected, works. But I'd like to avoid this to be more flexible in case I need more numbers down the road. Am I misunderstanding the documentation ?

I used HTTPie to test the API. The full request/response :

$ http --print=HBhb --form POST 'https://$ACCOUNT_SID:[email protected]/2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json' MessagingServiceSid=$SERVICE_SID To=$PHONE Body=Hello

POST /2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Basic REDACTED
Connection: keep-alive
Content-Length: 83
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: api.twilio.com
User-Agent: HTTPie/0.9.2

MessagingServiceSid=$SERVICE_SID&To=$PHONE&Body=Hello

HTTP/1.1 400 BAD REQUEST
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since
Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: ETag
Connection: keep-alive
Content-Length: 136
Content-Type: application/json
Date: Thu, 28 Jul 2016 14:54:38 GMT
Twilio-Request-Duration: 0.056
Twilio-Request-Id: RQ5b9acf9d222947668fa9ada38d5beaba
X-Powered-By: AT-5000
X-Shenanigans: none

{
    "code": 21603, 
    "message": "A 'From' phone number is required.", 
    "more_info": "https://www.twilio.com/docs/errors/21603", 
    "status": 400
}
1
Do you have a phone number set up with your Messaging Service?philnash

1 Answers

2
votes

I can see that you have a number in the MessageService, however it looks like you are using the old deprecated endpoint that does not support sending with the MessageService.

You have: /2010-04-01/Accounts/$ACCOUNT_SID/SMS/Messages.json

You should be using: /2010-04-01/Accounts/$ACCOUNT_SID/Messages.json

Give that a try and let me know how it goes.