New C++ Twilio API Wrapper by Laurent Luce

One of the great things about REST is that all you need to know to use it is how to speak HTTP. It is the ultimate cross-platform technology as near every language or framework can make web requests these days. As an example of this, Laurent Luce has written a Twilio API wrapper in C++. Laurent is a software engineer with expertise in networking and web services using technologies such as Python, Django, Javascript, C++, MySQL and AWS.

The C++ Twilio REST SDK simplifies the process of making requests to the Twilio REST API, generating TwiML and validating HTTP request signatures using C++. The project is hosted on GitHub.

The library consists of the following files:

  • Rest.cpp: Twilio REST helper
  • TwiML.cpp: TwiML generation helper
  • Utils.cpp: Utils to validate a HTTP request signature: X-Twilio-Signature
  • Examples.cpp: Examples of usage
  • UnitTests.h: Unit tests
  • Makefile: makefile to build the library (requires libcurl and openssl), examples code and unit tests suite.

To build the library just run ‘make’. Run ‘make examples’ to build the examples and run ‘make unittests’ to build the unit tests suite and run it. When using the library, don’t forget to include Utils.h, Rest.h and TwiML.h at the top of your source code and to use the twilio namespace: “using namespace twilio;”.

Laurent put together the following code examples based on a restaurant dish recommendation service using SMS for communication. For all of the examples, we are going to use the C++ std namespace: “using namespace std;” and define the following constants:

Receiving and replying to a SMS asking for a restaurant dish recommendation.

When someone asks for a dish recommendation, he sends a SMS with a text such as “Burger in San Francisco”. Twilio issues a HTTP POST request to your server with the text in the POST parameter “Body”. The URL called is SMS_URL.

First, we need to validate the HTTP POST request to make sure it is coming from a Twilio server. We use the Utils class to help us with that.

Then we need to reply with our dish recommendation. We use the classes TwiMLResponse and Sms to help us generate the XML.

response.toXML() returns the following:

Sending a daily recommendation to a list of subscribers using SMS.

We need to issue POST requests using the following URL: /2010-04-01/Accounts/{AccountSid}/SMS/Messages. In the following code, “subscribers” is a vector containing the phone numbers that we need to send to. We use the class Rest to help us send the SMS messages.

Retrieving the list of SMS messages we sent and their status.

We need to issue a GET request using the following URL: /2010-04-01/Accounts/{AccountSid}/SMS/Messages. We use the class Rest to help us with the GET request.

“res” should contain the list of SMS sent:

Note that pagination might need to be handled if the number of SMS messages is too large for one HTTP response. See the paging documentation for more details.

There is a lot more you can do using the Twilio C++ library so go ahead and have fun with it. Laurent would love to get your feedback and ideas over on the GitHub project page as well.

Do you have a Twilio API library or other code the community may find useful? We want to hear from you! Let us know in the comments or email so we can write about it.

  • addisonhuddy

    Really great. Using it for a fun project. Thanks!

    • Twilio

      Thanks for the feedback Addison. Please let us know what type of posts you’d like to see in the future!