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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setBasicAuth(true);
        // Load the owning Account SID and an API key from application configuration.
        String accountSid = "YOUR_ACCOUNT_SID";
        http.setLogin("YOUR_API_KEY_SID");
        http.setPassword("YOUR_API_KEY_SECRET");
        StringBuilder url = new StringBuilder();
        url.append("https://api.twilio.com/2010-04-01/Accounts/");
        url.append(accountSid);
        url.append("/Messages.json");
        url.append("?PageSize=50");
        HttpResponse resp = new HttpResponse();
        JsonObject reply = new JsonObject();
        int total = 0;
        boolean more = true;
        while (more) {
            try {
                http.httpNoBody("GET", url.getAsString(), resp);
                success = true;
            } catch (ChilkatException e) {
                success = false;
            }
            if (resp.getStatusCode() == 0) {
                System.out.println(http.getLastErrorText());
                return;
            }

            boolean parsed;
            try {
                reply.load(resp.getBodyStr());
                parsed = true;
            } catch (ChilkatException e) {
                parsed = false;
            }
            if ((!success) || (resp.getStatusCode() != 200)) {
                System.out.println("HTTP " + resp.getStatusCode());
                if ((parsed) && (reply.hasMember("code"))) {
                    System.out.println("Twilio error " + reply.intOf("code") + ": " + reply.stringOf("message"));
                }

                return;
            }

            if (!parsed) {
                System.out.println("Expected a JSON response.");
                return;
            }

            if (reply.jsonTypeOf("messages") != 4) {
                System.out.println("Expected a messages array.");
                return;
            }

            int count = reply.sizeOfArray("messages");
            int i = 0;
            while (i < count) {
                reply.setI(i);
                System.out.println(reply.stringOf("messages[i].sid") + "  " + reply.stringOf("messages[i].direction") + "  " + reply.stringOf("messages[i].status"));
                i = i + 1;
            }

            total = total + count;
            // Twilio supplies a relative URI with the cursor and returns null on the last page.
            if (reply.isNullOf("next_page_uri")) {
                more = false;
            } else {
                if (reply.jsonTypeOf("next_page_uri") != 1) {
                    System.out.println("Expected a next-page URI or null.");
                    return;
                }

                url.setString("https://api.twilio.com");
                url.append(reply.stringOf("next_page_uri"));
            }
        }

        System.out.println("Messages found: " + total);
    }
}