Search code examples
herokuparse-platformtwilioparse-cloud-codeparse-server

How to configure Twilio on parse server?


I am attempting to incorporate Twilio into my application that is running on my parse server deployed with Heroku and MongoLab. I am trying to configure by using this code in my cloud/main.js file

var twilio = require("twilio");
twilio.initialize("87se46bovanw4v5aiwy4o57","ia8o57awyov57yn875vyboe");


Parse.Cloud.define("inviteWithTwilio", function(request, response) {
// Use the Twilio Cloud Module to send an SMS
twilio.sendSMS({
    From: "6543211234",
    To: 8065456703,
    Body: "Start using Parse and Twilio!"
  }, {
success: function(httpResponse) { response.success("SMS sent!"); },
error: function(httpResponse) { response.error("Uh oh, something went wrong"); }
  });
});

however, I get this response

    UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} [NSDebugDescription: JSON text did not start with array or object and option to allow fragments not set.]

I have determined that the problem lies in the first two lines when trying to initialize Twilio. I figure this is because the Twilio Cloud Module isn't integrated into my parse server like it was with Parse Hosted cloud code, but I'm not sure. How can I fix this problem? Thanks for your time.


Solution

  • Twilio developer evangelist here.

    It sounds like, from our conversation in the comments, like you have not installed the Twilio npm module just yet. On Parse, you didn't have to install the module as it was included by default. To use the Twilio module using Parse server you need it installed.

    To install the module, open up your application in the terminal and type:

    $ npm install twilio --save
    

    The --save flag is important as it saves the dependency to your package.json file. Check in the updated package.json and deploy your code again. Now, when deploying to Heroku the npm modules, including the Twilio module, will be installed.

    Marin, who already answered as well, had a good point. I also recommend using twilio.sendMessage. It uses the newer and better featured Messages resource (rather than the deprecated SMS resource).

    Let me know if this helps at all.