Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Send an SMS with Twilio
See more Twilio Examples
Submit an SMS with the sender, recipient, and text as form fields. This example starts with Twilio test credentials and its successful test sender. Twilio simulates the submission without sending or billing for a message. Test credentials and test numbers.
For live SMS, use the live Account SID in the URL, authenticate with an API key SID and secret, and use an SMS-capable Twilio sender and a recipient in E.164 format. HTTP 201 creates the message. Save its SID to check delivery status later.
Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkStringBuilderW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkHttpW http;
http.put_RequireSslCertVerify(true);
http.put_BasicAuth(true);
// Use the test Account SID and test Auth Token from the Twilio Console.
const wchar_t *accountSid = L"YOUR_TEST_ACCOUNT_SID";
http.put_Login(accountSid);
http.put_Password(L"YOUR_TEST_AUTH_TOKEN");
CkStringBuilderW url;
url.Append(L"https://api.twilio.com/2010-04-01/Accounts/");
url.Append(accountSid);
url.Append(L"/Messages.json");
// Add raw field values. Chilkat form-encodes them, including the + in a phone number.
CkHttpRequestW req;
req.put_HttpVerb(L"POST");
req.put_ContentType(L"application/x-www-form-urlencoded");
req.AddParam(L"From",L"+15005550006");
req.AddParam(L"To",L"+12025550123");
req.AddParam(L"Body",L"Hello from Twilio!");
CkHttpResponseW resp;
CkJsonObjectW reply;
success = http.HttpReq(url.getAsString(),req,resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
bool parsed = reply.Load(resp.bodyStr());
if ((success != true) || (resp.get_StatusCode() != 201)) {
wprintf(L"HTTP %d\n",resp.get_StatusCode());
if ((parsed == true) && (reply.HasMember(L"code") == true)) {
wprintf(L"Twilio error %d: %s\n",reply.IntOf(L"code"),reply.stringOf(L"message"));
}
return;
}
if (parsed != true) {
wprintf(L"Expected a JSON response.\n");
return;
}
if ((reply.JsonTypeOf(L"sid") != 1) || (reply.JsonTypeOf(L"status") != 1)) {
wprintf(L"The create response is missing the message SID or status.\n");
return;
}
wprintf(L"Message SID: %s\n",reply.stringOf(L"sid"));
wprintf(L"Initial status: %s\n",reply.stringOf(L"status"));
}