Twilio API

From DreamFactory
Jump to: navigation, search

This tutorial shows how to configure a remote web service to connect to the Twilio API. It shows how to pass your authorization header to the remote service. This example is specific to the Twilio API, but many APIs have similar concepts.

Create the Remote Web Service

  • Sign up for a free Twilio account at https://www.twilio.com.
  • In the DreamFactory admin console, go to the Services tab and click Create.
  • Set the Service Type to 'Remote Service' > 'HTTP Service'
  • Set the service name to 'twilio'.
  • Set the service label to 'Twilio API'. Check the Active checkbox.
  • 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: https://{AccountSid}:{AuthToken}@api.twilio.com/2010-04-01/Accounts. This 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 (see screenshot below). For more information on Twilio basic auth, see https://www.twilio.com/docs/api/rest/request

Twilio-http-service.png

  • Make sure to select all five HTTP verbs so that the header is sent to the Twilio API for all requests.
  • Optionally, go to the Service Definition tab and copy in a Swagger 2.0 JSON definition for many of the important Twilio API calls. The Swagger 2.0 JSON file for the Twilio API is located here.

Twilio-http-swagger.png

  • Click Create Service to save your new service.

Call the Remote Web Service

You can now use any REST client to access your remote web service using the DreamFactory REST API. See other tutorials for authentication options.

For example, with cURL you can first log in as a DreamFactory admin and get a session token:

curl -i -k -3 -X POST "http://localhost:8080/api/v2/system/admin/session" -d '{ "email" : "[email protected]", "password" : "your_password" }'

Then make a cURL call to the Twilio API, like this:

curl -i -k -3 -X GET "http://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>"

DreamFactory combines the request URL from the client with the configured base URL for the service to generate the actual URL for the request.

If you're using the Swagger 2.0 JSON or YAML definition for the Twilio API, you can easily add role-based access control to the Twilio API in the 'Roles' tab of the DreamFactory admin console.

Remember that you can also create server-side scripts to manipulate the API request or response.