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

List Twilio message history

See more Twilio Examples

List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.

Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.

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_Resp
oleobject loo_Reply
long li_Total
long li_More
long li_Parsed
long li_Count
long i

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
//  Load the owning Account SID and an API key from application configuration.
ls_AccountSid = "YOUR_ACCOUNT_SID"
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"
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")
loo_Url.Append("?PageSize=50")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

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

li_Total = 0
li_More = 1
do while (li_More = 1)
    li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
    if loo_Resp.StatusCode = 0 then
        Write-Debug loo_Http.LastErrorText
        destroy loo_Http
        destroy loo_Url
        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 <> 200) 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_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_Resp
        destroy loo_Reply
        return
    end if

    if loo_Reply.JsonTypeOf("messages") <> 4 then
        Write-Debug "Expected a messages array."
        destroy loo_Http
        destroy loo_Url
        destroy loo_Resp
        destroy loo_Reply
        return
    end if

    li_Count = loo_Reply.SizeOfArray("messages")
    i = 0
    do while (i < li_Count)
        loo_Reply.I = i
        Write-Debug loo_Reply.StringOf("messages[i].sid") + "  " + loo_Reply.StringOf("messages[i].direction") + "  " + loo_Reply.StringOf("messages[i].status")
        i = i + 1
    loop
    li_Total = li_Total + li_Count
    //  Twilio supplies a relative URI with the cursor and returns null on the last page.
    if loo_Reply.IsNullOf("next_page_uri") = 1 then
        li_More = 0
    else
        if loo_Reply.JsonTypeOf("next_page_uri") <> 1 then
            Write-Debug "Expected a next-page URI or null."
            destroy loo_Http
            destroy loo_Url
            destroy loo_Resp
            destroy loo_Reply
            return
        end if

        loo_Url.SetString("https://api.twilio.com")
        loo_Url.Append(loo_Reply.StringOf("next_page_uri"))
    end if

loop
Write-Debug "Messages found: " + string(li_Total)


destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Reply