Sample code for 30+ languages & platforms
Java

Twilio: Send SMS using Basic Authentication

See more REST Examples

Demonstrates how to use Twilio to send an SMS message using Basic authentication.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Rest;

public class ChilkatExample {

    public static void main(String[] args) {
        // Demonstrates how to use Basic Authentication in a REST API call for Twilio.
        // Sends an SMS text message..

        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Rest rest = new Rest();

        // Use Basic Authentication.
        // Your Twilio Account SID is the username.
        // Your Twilio Auth Token is the password.
        rest.setAuthBasic("TWILIO_ACCOUNT_SID", "TWILIO_AUTH_TOKEN");

        // Make the initial connection (without sending a request yet) to Twilio.
        boolean bTls = true;
        int port = 443;
        boolean bAutoReconnect = true;
        String responseJson;
        try {
            rest.connect("api.twilio.com", port, bTls, bAutoReconnect);

            // Provide the information for the SMS text message:
            rest.addQueryParam("To", "+16518675309");
            rest.addQueryParam("From", "+15005550006");
            rest.addQueryParam("Body", "Hey Jenny! Good luck on the bar exam!");
            rest.addQueryParam("MediaUrl", "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg");

            // Send the SMS text message.
            // Your Twilio Account SID is part of the URI path:
            responseJson = rest.fullRequestFormUrlEncoded("POST", "/2010-04-01/Accounts/TWILIO_ACCOUNT_SID/Messages.json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // When successful, the response status code will equal 201.
        if (rest.getResponseStatusCode() != 201) {
            // Examine the request/response to see what happened.
            System.out.println("response status code = " + rest.getResponseStatusCode());
            System.out.println("response status text = " + rest.getResponseStatusText());
            System.out.println("response header: " + rest.getResponseHeader());
            System.out.println("response body (if any): " + responseJson);
            System.out.println("---");
            System.out.println("LastRequestStartLine: " + rest.getLastRequestStartLine());
            System.out.println("LastRequestHeader: " + rest.getLastRequestHeader());
            return;
        }

        // The response is JSON.  We'll show how to get a few bits of information from it.
        // A full sample JSON response is shown below..

        JsonObject json = new JsonObject();
        json.setEmitCompact(false);
        json.load(responseJson);

        // First show the entire JSON.
        System.out.println(json.emit());

        // Now get some individual pieces of information:
        System.out.println("sid: " + json.stringOf("sid"));
        System.out.println("body: " + json.stringOf("body"));
        System.out.println("media: " + json.stringOf("subresource_uris.media"));

        System.out.println("Success.");

        // Sample JSON response:

        // {
        //   "sid": "MM97ecfd43e9f24e99b0c2c6ee016949e3",
        //   "date_created": null,
        //   "date_updated": null,
        //   "date_sent": null,
        //   "account_sid": "112e1111e0151133d11112101111d1111",
        //   "to": "+16518675309",
        //   "from": "+15005550006",
        //   "messaging_service_sid": null,
        //   "body": "Sent from your Twilio trial account - Hey Jenny! Good luck on the bar exam!",
        //   "status": "queued",
        //   "num_segments": "1",
        //   "num_media": "0",
        //   "direction": "outbound-api",
        //   "api_version": "2010-04-01",
        //   "price": null,
        //   "price_unit": "USD",
        //   "error_code": null,
        //   "error_message": null,
        //   "uri": "/2010-04-01/Accounts/AC2e9b6bc0f51133df24926f07341d3824/Messages/MM97ecfd43e9f24e99b0c2c6ee016949e3.json",
        //   "subresource_uris": {
        //     "media": "/2010-04-01/Accounts/AC2e9b6bc0f51133df24926f07341d3824/Messages/MM97ecfd43e9f24e99b0c2c6ee016949e3/Media.json"
        //   }
        // }
    }
}