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

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cAccountSid
LOCAL oUrl
LOCAL oResp
LOCAL oReply
LOCAL nTotal
LOCAL nMore
LOCAL nParsed
LOCAL nCount
LOCAL i

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
//  Load the owning Account SID and an API key from application configuration.
cAccountSid := "YOUR_ACCOUNT_SID"
oHttp:Login := "YOUR_API_KEY_SID"
oHttp:Password := "YOUR_API_KEY_SECRET"
oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://api.twilio.com/2010-04-01/Accounts/")
oUrl:Append(cAccountSid)
oUrl:Append("/Messages.json")
oUrl:Append("?PageSize=50")
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")
nTotal := 0
nMore := 1
DO WHILE (nMore == 1)
    nSuccess := oHttp:HttpNoBody("GET", oUrl:GetAsString(), oResp)
    IF (oResp:StatusCode == 0)
        ? oHttp:LastErrorText
        oHttp:destroy()
        oUrl:destroy()
        oResp:destroy()
        oReply:destroy()
        RETURN
    ENDIF

    nParsed := oReply:Load(oResp:BodyStr)
    IF ((nSuccess != 1) .OR. (oResp:StatusCode != 200))
        ? "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()
        oResp:destroy()
        oReply:destroy()
        RETURN
    ENDIF

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

    IF (oReply:JsonTypeOf("messages") != 4)
        ? "Expected a messages array."
        oHttp:destroy()
        oUrl:destroy()
        oResp:destroy()
        oReply:destroy()
        RETURN
    ENDIF

    nCount := oReply:SizeOfArray("messages")
    i := 0
    DO WHILE (i < nCount)
        oReply:I := i
        ? oReply:StringOf("messages[i].sid") + "  " + oReply:StringOf("messages[i].direction") + "  " + oReply:StringOf("messages[i].status")
        i := i + 1
    ENDDO
    nTotal := nTotal + nCount
    //  Twilio supplies a relative URI with the cursor and returns null on the last page.
    IF (oReply:IsNullOf("next_page_uri") == 1)
        nMore := 0
    ELSE
        IF (oReply:JsonTypeOf("next_page_uri") != 1)
            ? "Expected a next-page URI or null."
            oHttp:destroy()
            oUrl:destroy()
            oResp:destroy()
            oReply:destroy()
            RETURN
        ENDIF

        oUrl:SetString("https://api.twilio.com")
        oUrl:Append(oReply:StringOf("next_page_uri"))
    ENDIF

ENDDO
? "Messages found: " + Str(nTotal)

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