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

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @sTmp2 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    --  Load the owning Account SID and an API key from application configuration.
    DECLARE @accountSid nvarchar(4000)
    SELECT @accountSid = 'YOUR_ACCOUNT_SID'
    EXEC sp_OASetProperty @http, 'Login', 'YOUR_API_KEY_SID'
    EXEC sp_OASetProperty @http, 'Password', 'YOUR_API_KEY_SECRET'
    DECLARE @url int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT

    EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://api.twilio.com/2010-04-01/Accounts/'
    EXEC sp_OAMethod @url, 'Append', @success OUT, @accountSid
    EXEC sp_OAMethod @url, 'Append', @success OUT, '/Messages.json'
    EXEC sp_OAMethod @url, 'Append', @success OUT, '?PageSize=50'
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @reply int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT

    DECLARE @total int
    SELECT @total = 0
    DECLARE @more int
    SELECT @more = 1
    WHILE (@more = 1)
      BEGIN
        EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            RETURN
          END
        DECLARE @parsed int
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @reply, 'Load', @parsed OUT, @sTmp0
        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        IF (@success <> 1) or (@iTmp0 <> 200)
          BEGIN

            EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
            PRINT 'HTTP ' + @iTmp0
            EXEC sp_OAMethod @reply, 'HasMember', @iTmp0 OUT, 'code'
            IF (@parsed = 1) and (@iTmp0 = 1)
              BEGIN

                EXEC sp_OAMethod @reply, 'IntOf', @iTmp0 OUT, 'code'

                EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'message'
                PRINT 'Twilio error ' + @iTmp0 + ': ' + @sTmp0
              END
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            RETURN
          END
        IF @parsed <> 1
          BEGIN

            PRINT 'Expected a JSON response.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            RETURN
          END
        EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'messages'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected a messages array.'
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            RETURN
          END
        DECLARE @count int
        EXEC sp_OAMethod @reply, 'SizeOfArray', @count OUT, 'messages'
        DECLARE @i int
        SELECT @i = 0
        WHILE (@i < @count)
          BEGIN
            EXEC sp_OASetProperty @reply, 'I', @i
            EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'messages[i].sid'

            EXEC sp_OAMethod @reply, 'StringOf', @sTmp1 OUT, 'messages[i].direction'

            EXEC sp_OAMethod @reply, 'StringOf', @sTmp2 OUT, 'messages[i].status'
            PRINT @sTmp0 + '  ' + @sTmp1 + '  ' + @sTmp2
            SELECT @i = @i + 1
          END
        SELECT @total = @total + @count
        --  Twilio supplies a relative URI with the cursor and returns null on the last page.
        EXEC sp_OAMethod @reply, 'IsNullOf', @iTmp0 OUT, 'next_page_uri'
        IF @iTmp0 = 1
          BEGIN
            SELECT @more = 0
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'next_page_uri'
            IF @iTmp0 <> 1
              BEGIN

                PRINT 'Expected a next-page URI or null.'
                EXEC @hr = sp_OADestroy @http
                EXEC @hr = sp_OADestroy @url
                EXEC @hr = sp_OADestroy @resp
                EXEC @hr = sp_OADestroy @reply
                RETURN
              END
            EXEC sp_OAMethod @url, 'SetString', @success OUT, 'https://api.twilio.com'
            EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'next_page_uri'
            EXEC sp_OAMethod @url, 'Append', @success OUT, @sTmp0
          END
      END

    PRINT 'Messages found: ' + @total

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @url
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @reply


END
GO