Skip to content

A Java library for communicating with the Twilio REST API and generating TwiML.

License

Notifications You must be signed in to change notification settings

duttonw/twilio-java

 
 

Repository files navigation

twilio-java

Build Status Maven Central Learn with TwilioQuest

Documentation

The documentation for the Twilio API can be found here.

The Java library documentation can be found here.

Recent Update

As of release 7.14.0, Beta and Developer Preview products are now exposed via the main twilio-java artifact. Releases of the alpha branch have been discontinued.

If you were using the alpha release line, you should be able to switch back to the normal release line without issue.

If you were using the normal release line, you should now see several new product lines that were historically hidden from you due to their Beta or Developer Preview status. Such products are explicitly documented as Beta/Developer Preview both in the Twilio docs and console, as well as through in-line code documentation here in the library.

TLS 1.2 Requirements

New accounts and subaccounts are now required to use TLS 1.2 when accessing the REST API. "Upgrade Required" errors indicate that TLS 1.0/1.1 is being used.

Installing

twilio-java uses Maven. At present the jars are available from a public maven repository.

Use the following dependency in your project to grab via Maven:

   <dependency>
      <groupId>com.twilio.sdk</groupId>
      <artifactId>twilio</artifactId>
      <version>7.X.X</version>
      <scope>compile</scope>
   </dependency>

or Gradle:

implementation "com.twilio.sdk:twilio:7.X.X"

If you want to compile it yourself, here's how:

$ git clone [email protected]:twilio/twilio-java
$ cd twilio-java
$ mvn install       # Requires maven, download from https://maven.apache.org/download.html

Versions

twilio-java uses a modified version of Semantic Versioning for all changes. See this document for details.

Quickstart

Initialize the Client

// Find your Account SID and Auth Token at twilio.com/console
// DANGER! This is insecure. See https://twil.io/secure
String accountSid = "ACXXXXXX";
String authToken = "XXXXXXXX";

Twilio.init(accountSid, authToken);

Send an SMS

Message message = Message.creator(
    new PhoneNumber("+15558881234"),  // To number
    new PhoneNumber("+15559994321"),  // From number
    "Hello world!"                    // SMS body
).create();

System.out.println(message.getSid());

Make a Call

Call call = Call.creator(
    new PhoneNumber("+15558881234"),  // To number
    new PhoneNumber("+15559994321"),  // From number

    // Read TwiML at this URL when a call connects (hold music)
    new URI("https://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")
).create();

System.out.println(call.getSid());

Handling Exceptions

import com.twilio.exception.ApiException;

try {
    Message message = Message.creator(
        new PhoneNumber("+15558881234"),  // To number
        new PhoneNumber("+15559994321"),  // From number
        "Hello world!"                    // SMS body
    ).create();
    
    System.out.println(message.getSid());
} catch (final ApiException e) {
    System.err.println(e);
}

Using a Different Client

TwilioRestClient client = new TwilioRestClient.Builder(accountSid, authToken).build();

Message message = Message.creator(
    new PhoneNumber("+15558881234"),  // To number
    new PhoneNumber("+15559994321"),  // From number
    "Hello world!"                    // SMS body
).create(client);  // Pass the client here

System.out.println(message.getSid());

Generating TwiML

To control phone calls, your application needs to output TwiML.

TwiML in twilio-java now use the builder pattern!

TwiML twiml = new VoiceResponse.Builder()
    .say(new Say.Builder("Hello World!").build())
    .play(new Play.Builder("https://api.twilio.com/cowbell.mp3").loop(5).build())
    .build();

That will output XML that looks like this:

<Response>
    <Say>Hello World!</Say>
    <Play loop="5">https://api.twilio.com/cowbell.mp3</Play>
</Response>

Docker Image

The Dockerfile present in this repository and its respective twilio/twilio-java Docker image are currently used by Twilio for testing purposes only.

Getting Help

If you need help installing or using the library, please check the Twilio Support Help Center first, and file a support ticket if you don't find an answer to your question.

If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!

About

A Java library for communicating with the Twilio REST API and generating TwiML.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%