Using CloudMine as an HTTP callback endpoint
Lots of people are using our server-side superpowers to enhance our APIs with their own custom business logic. We are now taking that flexibility to the next level by allowing you to use any code snippet as its own endpoint for an HTTP callback from 3rd party services.
Imagine you want to handle incoming SMS messages from the likes of Twilio, SMSified or Nexmo. Each of these services requires an HTTP callback to be specified so that your server can perform some action when an SMS comes in. Usually, these callbacks are called via POST with either XML or JSON payloads, or via GET with URL parameters.
CloudMine now provides an easy way for you to implement these callback endpoints. We’ll look at a quick example of how to use this new feature to write an SMS echo app hosted completely by CloudMine and powered by Nexmo.
Nexmo allows you to specify a callback URL that they either GET or POST to (either is fine with us), and all data is passed in URL parameters.
We are going to write a snippet to handle the callback, which we’ll call echo_demo. The full callback URL you supply to Nexmo is:
Let’s look at the code that implements echo_demo. It accepts parameters from Nexmo, prepends “you said: ” to the original text and sends it back to the sender. It also logs the incoming and outgoing SMS information back to your CloudMine app.
We start by initializing the CloudMine and Nexmo libraries with their respective credentials.
From the Nexmo documentation we see that the msisdn param will be the original sender phone number, and the to param is the original destination number (your Nexmo provisioned number). The code swaps the to and from numbers to send back the echo.
Next, the easynexmo.sendTextMessage function is invoked to send the reply. When that call completes, we log the incoming and outgoing SMS information in the CloudMine app (indexed by the timestamp). Finally, when that operation completes, we exit the script.
The object logged to CloudMine will look like:
In addition to processing URL parameters, our endpoint also automatically handles form posts, JSON payloads, and XML payloads. Check out the full documentation for more information: https://www.cloudmine.me/developer_zone#code/standalone
And that’s it! It’s that easy to use CloudMine as an endpoint for incoming events. We can’t wait to see what kind of cool and innovative apps you will build using this feature.