Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now


Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?


Today we'll use C++, some standard libraries, libcurl, and some POSIX APIs to send SMS and MMS messages using the Twilio REST API. You'll need a (mostly) POSIX-compliant build environment as well as libcurl to follow along with this guide.


Sign Up for (or Sign In to) a Twilio Account

You'll need to login to your existing Twilio account, or create a new account.

Don't have an account yet? Sign up for a free Twilio trial, post-haste.


You'll need a SMS (or optionally, MMS) capable number to complete this demo. Either use a phone number that you already have, or purchase a new one with SMSes enabled.



If you need to purchase a number, after navigating to the 'Buy a Number' link click the SMS checkbox (and the MMS checkbox, if you wish to send media messages).


Prerequisites for This C++ MMS and SMS Guide

We're targeting POSIX Compliant environments with this guide, and tested our code with clang-800.0.42.1 on Mac OSX 10.11, gcc version 4.9.2 on Raspbian 8.0 (kernel 4.4.38-v7+) and gcc 5.4.0 in Cygwin 2.877 on Windows 7 64-Bit.

To build, you will need to ensure you have:

libcurl was already on the Mac. On the Raspberry Pi, you can install a suitable version with:

sudo apt-get install libcurl4-openssl-dev

Windows 7とCygwinでは、Cygwinセットアップでlibcurlをインストールできます。

Installing libcurl-devel and curl in Cygwin

Building cpp_demo

All three environments worked with this example Makefile. You may need to make minor edits - usually to the library search paths - to build in your environment.

On almost all *NIX-type systems (including Windows/Cygwin), building should be very similar to the following lines (you might even be able to copy, paste and execute):

git clone https://github.com/TwilioDevEd/twilio_cpp_demo.git
cd twilio_cpp_demo

Send an SMS or MMS Message with C++

We've just built cpp_demo, which attempts to send an SMS or MMS based on your inputs from the command line. src/twilio.cc and include/twilio.hh demonstrate a class, Twilio, which should be easy to integrate into your own codebase. (Note that before putting it into production, you need to add input validation and safety. The first order of concern: the code in its current form can be passed additional HTTP parameters through the command line inputs.)

C++開発者には、CによるSMSおよびMMSガイドでは下記のコードについてのいくつかの重要な洞察が得られるでしょう - しかし舞台裏で何が起こっているのか詳しく見てみることにしましょう。

  • First, we instantiate a Twilio object with our account credentials (find them on the Twilio Console).
  • Second, we call the method send_message with the to/from numbers, the message, and optionally a URL to a picture and a boolean of whether we want verbose output.
        Instantiating a Twilio class and sending a SMS or MMS message with C++ and libcurl.

        C++からTwilioのREST APIを使用する

        Instantiating a Twilio class and sending a SMS or MMS message with C++ and libcurl.
        • Third, once inside the code we first do some exception checking (making sure the message is the proper size), followed by setting up libcurl with our eventual HTTP POST
        • Fourth, curl makes the HTTP POST and we clean up with curl_easy_cleanup.
        • 最後に、成功か失敗かを得るためにレスポンスを確認し、呼び出し側の関数に結果を返します。
              The send_message method from the Twilio class from the CPP Messaging demo in C++.

              C++ Twilioクラス

              The send_message method from the Twilio class from the CPP Messaging demo in C++.

              Running cpp_demo

              Running cpp_demo from the command line is easy once you have it built. Merely set your account credentials and call the code as follows:

              bin/cpp_demo -a $account_sid -s $auth_token -t "+18005551212" -f "+18005551213" -m "Hello, World!"
              # Optionally, use '-p http://some/path/to/img.jpg' to send an MMS.


              After this demo, you now have the means to add communications to your new or existing C++ applications. Add monitoring or help to your C++ application, or bring your own use case.

              Whether you're a master of the language or closer to a practitioner of "C with classes" please do let us know on Twitter when you've got something built!

              Paul Kamp Kat King Maylon Pedroso
              Rate this page:


              誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。