Search code examples
iosswiftsmstwilioalamofire

Twilio POST request using Alamofire on iOS


I have this IBAction that is supposed to simply test sending SMS:

    let todosEndpoint: String = "https://api.twilio.com/2010-04-01/Accounts/\(ACCOUNT_SID)/Messages.json"
    let message = ["To": "+1555...5555", "From": "+555...5555", "Body": "Hello!"]
    for item in message {
        print(item)
    }

    Alamofire.request(todosEndpoint, method: .post, parameters: message, encoding: JSONEncoding.default)
        .authenticate(user: ACCOUNT_SID, password: ACCESS_TOKEN)
        .responseJSON { response in
            debugPrint(response)

    }

However, I'm getting this as a response:

[Result]: SUCCESS: {
    code = 21603;
    message = "A 'From' phone number is required.";
    "more_info" = "https://www.twilio.com/docs/errors/21603";
    status = 400;
}

I am sure I am providing the right phone number and authentication and I've used python to test sending messages using my API key and phone number. Is the structure of my dictionary wrong or something? Thank you for any help!


Solution

  • Twilio developer evangelist here.

    We do not recommend that you make API calls directly to Twilio from your application. You would need to include your account credentials in the application somehow, so a malicious attacker could decompile the app, extract your details and abuse your Twilio account.

    Instead, we recommend you set up a server of your own that can keep the credentials safe and then make requests to that yourself. Check out this blog post on sending SMS messages on iOS with Twilio, Swift and Alamofire.