Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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.

Twilio Messages API reference

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cAccountSid
LOCAL oUrl
LOCAL oReq
LOCAL oResp
LOCAL oReply
LOCAL nParsed

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
//  Use the test Account SID and test Auth Token from the Twilio Console.
cAccountSid := "YOUR_TEST_ACCOUNT_SID"
oHttp:Login := cAccountSid
oHttp:Password := "YOUR_TEST_AUTH_TOKEN"
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://api.twilio.com/2010-04-01/Accounts/")
oUrl:Append(cAccountSid)
oUrl:Append("/Messages.json")

//  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"
oReq:AddParam("From", "+15005550006")
oReq:AddParam("To", "+12025550123")
oReq:AddParam("Body", "Hello from Twilio!")

oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")
nSuccess := oHttp:HttpReq(oUrl:GetAsString(), oReq, oResp)
IF (oResp:StatusCode == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

nParsed := oReply:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != 201))
    ? "HTTP " + Str(oResp:StatusCode)
    IF ((nParsed == 1) .AND. (oReply:HasMember("code") == 1))
        ? "Twilio error " + Str(oReply:IntOf("code")) + ": " + oReply:StringOf("message")
    ENDIF

    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nParsed != 1)
    ? "Expected a JSON response."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF ((oReply:JsonTypeOf("sid") != 1) .OR. (oReply:JsonTypeOf("status") != 1))
    ? "The create response is missing the message SID or status."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "Message SID: " + oReply:StringOf("sid")
? "Initial status: " + oReply:StringOf("status")

oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oResp:destroy()
oReply:destroy()