Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Set up a your Java development environment


This tutorial covers creating your first Twilio voice application using Java. It starts with the set up of your Java development environment.

Time to complete: Approximately 30-60 minutes depending on your operating system, internet speed, and download times.


Prerequisites

prerequisites page anchor

This tutorial requires the following tools and understanding:


Create a Java project with IntelliJ IDEA

create-a-java-project-with-intellij-idea page anchor

These are the steps to create a new Java project with IntelliJ IDEA:

  1. Open IntelliJ IDEA.
  2. Click New Project or File > New > Project with IntelliJ already open.
  3. In the project creation wizard:
    • Select New Project from the left sidebar.
    • Choose your Project SDK: Select the Java version you installed. This will show Java 21.
    • Click Next.
  4. Select Create from archetype.
  5. Check Create from archetype.
  6. Select maven-archetype-quickstart from the list.
  7. Click Next to move to New Project screen with fields for GroupId, ArtifactId, and Version.
  8. Click Advanced Settings arrow to expand additional options.
  9. Add your project information:
    • Type com.example into the GroupId box.
    • Type twilio-java-example into the ArtifactId box.
    • Don't change the value in the Version box.
  10. IntelliJ populates the Project name field on ArtifactId value.
  11. Choose where to save your project and set it in the Project location box.
  12. Click Next.
  13. Click Finish.

IntelliJ creates the correct directory structure and open the project. Your application's source code goes in the src/main/java directory.


Add Twilio functionality to your Java app

add-twilio-functionality-to-your-java-app page anchor

Your applications interact with Twilio in two ways:

  • Call the Twilio API from your code—Send SMS messages, make phone calls, or use other Twilio services.
  • Have Twilio call your application using a webhook—Respond to incoming messages, calls, or other events.

Call the Twilio API from your code

call-the-twilio-api-from-your-code page anchor

To interact with Twilio services, add the Twilio Java helper library to your project through adding a dependency to your build configuration.

  1. In IntelliJ, open the pom.xml file in your project root.

  2. Find the <dependencies> section.

  3. Add this dependency inside the <dependencies> tags:

    1
    <dependency>
    2
    <groupId>com.twilio.sdk</groupId>
    3
    <artifactId>twilio</artifactId>
    4
    <version>10.5.1</version>
    5
    </dependency>
  4. After adding the dependency, IntelliJ displays notification about Maven changes.

  5. Click Import Changes or Load Maven Changes when prompted.

Create your Twilio Java app code

create-your-twilio-java-app-code page anchor

These are the steps to create your first Twilio Java application using Java:

  1. Create a Java file called TwilioExample.java in your src/main/java directory:

    1
    import com.twilio.Twilio;
    2
    import com.twilio.rest.api.v2010.account.Call;
    3
    import com.twilio.type.PhoneNumber;
    4
    import java.net.URI;
    5
    6
    public class TwilioExample {
    7
    // Replace with your actual credentials (consider using environment variables)
    8
    public static final String ACCOUNT_SID = "your_account_sid_here";
    9
    public static final String AUTH_TOKEN = "your_auth_token_here";
    10
    11
    public static void main(String[] args) {
    12
    // Initialize Twilio
    13
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
    14
    15
    Call call = Call.creator(new com.twilio.type.PhoneNumber("+18005550100"),
    16
    new com.twilio.type.PhoneNumber("+18005550199"),
    17
    URI.create("http://demo.twilio.com/docs/voice.xml"))
    18
    .create();
    19
    }
    20
    }
  2. Replace your_account_sid_here and your_auth_token_here with your actual Twilio Account SID and Auth Token from the Twilio Console. For production applications, use environment variables(link takes you to an external page) or configuration files to store credentials instead of hardcoding them.

    (error)

    Don't include credentials in production apps

    This quickstart hardcodes your credentials for faster setup. To keep credentials secret and control access when you deploy to production, use environment variables and API keys.

    1
    // Using environment variables (recommended for production)
    2
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    3
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");
    4
    5
    // Initialize Twilio with environment variables
    6
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    To set environment variables:

    On Windows (Command Prompt):

    1
    set TWILIO_ACCOUNT_SID=your_account_sid_here
    2
    set TWILIO_AUTH_TOKEN=your_auth_token_here

    On Windows (PowerShell):

    1
    $env:TWILIO_ACCOUNT_SID="your_account_sid_here"
    2
    $env:TWILIO_AUTH_TOKEN="your_auth_token_here"

    On macOS/Linux:

    1
    export TWILIO_ACCOUNT_SID=your_account_sid_here
    2
    export TWILIO_AUTH_TOKEN=your_auth_token_here
  3. To set the receiving, or To, phone number, replace the first phoneNumber in the call method.

  4. To set the sending, or From, phone number, replace the second phoneNumber in the call method. This should be your Twilio phone number that you got as a part of the prerequisites.

These steps are how to run your Java application in IntelliJ:

  1. Right-click on TwilioExample.java in IntelliJ.
  2. Select Run 'TwilioExample.main()'.
  3. Check the console output for the call SID confirmation.
  4. Verify that the receiving phone number got the call.

You're ready to build out your Java application. Learn more with the following resources: