Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
string ls_AccountSid
oleobject loo_Url
oleobject loo_Req
oleobject loo_Resp
oleobject loo_Reply
long li_Parsed

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
//  Use the test Account SID and test Auth Token from the Twilio Console.
ls_AccountSid = "YOUR_TEST_ACCOUNT_SID"
loo_Http.Login = ls_AccountSid
loo_Http.Password = "YOUR_TEST_AUTH_TOKEN"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Url.Append("https://api.twilio.com/2010-04-01/Accounts/")
loo_Url.Append(ls_AccountSid)
loo_Url.Append("/Messages.json")

//  Add raw field values. Chilkat form-encodes them, including the + in a phone number.
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
loo_Req.AddParam("From","+15005550006")
loo_Req.AddParam("To","+12025550123")
loo_Req.AddParam("Body","Hello from Twilio!")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Req,loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> 201) then
    Write-Debug "HTTP " + string(loo_Resp.StatusCode)
    if (li_Parsed = 1) AND (loo_Reply.HasMember("code") = 1) then
        Write-Debug "Twilio error " + string(loo_Reply.IntOf("code")) + ": " + loo_Reply.StringOf("message")
    end if

    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if li_Parsed <> 1 then
    Write-Debug "Expected a JSON response."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if (loo_Reply.JsonTypeOf("sid") <> 1) OR (loo_Reply.JsonTypeOf("status") <> 1) then
    Write-Debug "The create response is missing the message SID or status."
    destroy loo_Http
    destroy loo_Url
    destroy loo_Req
    destroy loo_Resp
    destroy loo_Reply
    return
end if

Write-Debug "Message SID: " + loo_Reply.StringOf("sid")
Write-Debug "Initial status: " + loo_Reply.StringOf("status")


destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_Resp
destroy loo_Reply