Terence Bennett - September 15, 2016

twilio.png

Twilio has a superb API for integrating SMS messages into your applications. It’s easy to add Twilio as a remote HTTP service to any application you’re building with DreamFactory. DreamFactory lets you securely store your Twilio authentication credentials, call the Twilio API directly from a DreamFactory session, and easily add role-based access control to any Twilio API endpoint. This brief tutorial shows you how to add Twilio to DreamFactory in five minutes.

Step 1: Get a free Twilio account 

If you don’t already have a Twilio account, sign for a free account here.

Step 2: Get a free DreamFactory account

If you don’t have a DreamFactory instance, sign up for a free developer account here or install DreamFactory from Bitnami or GitHub.

Read more about Integrating GitHub here.

Step 3: Add Twilio to DreamFactory as a Remote HTTP Service

Navigate to the ‘Services’ tab of the DreamFactory Admin Console and follow the steps below to set up a secure connection to Twilio.

  • Click Create and set the Service Type to ‘Remote Service’ > ‘HTTP Service’.
  • In the ‘Info’ tab, set the service name to ‘twilio’ and label to ‘Twilio’. Check the Active checkbox.

twilio-info.png

  • Go to the Config tab for the new service and set the Base URL to https://api.twilio.com/2010-04-01/.
  • Under Headers, click the ‘+’ button and enter ‘Authorization’ for Name and ‘Basic <basic auth header> for Value. To get the basic auth header (a long string of characters), enter this URL with your own Twilio Account Sid and Auth Token, which you can find in the Twilio’s Console Dashboard: https://{AccountSid}:{AuthToken}@api.twilio.com/2010-04-01/Accounts. This URL will generate a basic auth header string that you can copy and paste from a REST client like Postman or from your browser’s developer tools console. This is the long string you’ll need to enter into as the Value in the screenshot below. For more information on Twilio basic auth, see https://www.twilio.com/docs/api/rest/request.
  • Make sure to select all five HTTP verbs so that the header is sent to the Twilio API for all requests.

twilio-config.png

Step 4: Call the Twilio API from DreamFactory

Now that Twilio is configured in DreamFactory, you can use any REST client to access Twilio API endpoints using the DreamFactory REST API. For example, here’s how it works with cURL.

  • Log into DreamFactory as an Admin to get a DreamFactory session token.
curl -i -k -3 -X POST "https://localhost:8080/api/v2/system/admin/session" -d '{ "email" : "[email protected]", "password" : "your_password" }' -H "Content-Type: application/json"
  • Then make an API call to Twilio, like below with cURL. DreamFactory combines the request URL from the client with the configured base URL for the service to generate the actual URL for the request. You can now call any Twilio API endpoint with a DreamFactory session!
curl -i -k -3 -X GET "https://localhost:8080/api/v2/twilio/Accounts/<your twilio account sid>.json" -H "X-DreamFactory-Api-Key: <api key for the admin app in the apps tab>" -H "X-DreamFactory-Session-Token: <session token returned by the login api call above>"

Step 5 (Optional): Add Swagger 2.0 Definition for the Twilio API

It’s also easy to add a Swagger definition for Twilio. The Swagger definition documents the Twilio API and makes it easy to make live API calls right in the DreamFactory API docs. There’s a Swagger 2.0 JSON definition for Twilio on GitHub here. Just copy and paste the JSON into the ‘Service Definition’ tab.

twilio-swagger.png

twilio-swagger-endpoints.png

The other benefit of adding the Swagger definition is to add role-based access control to the Twilio API. In the ‘Roles’ tab of DreamFactory, simply grant or deny access to your Twilio API for one or more roles. You can even partition the Twilio API into multiple Swagger files with different endpoints and assign the Swagger endpoints to different roles, all under the same Twilio account. It’s all up to you!

twilio-role.png

You can also implement server-side event scripts on any Twilio endpoint defined in the Swagger definition. This is a powerful way to add logic to any Twilio API request or response.

This brief tutorial should help you get started building great apps with the simplicity and power of DreamFactory and Twilio. Let us know what you build!