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.
This tutorial requires the following tools and understanding:
- A stable internet connection for downloading Java, Maven, and IntelliJ IDEA.
- Basic familiarity with using the terminal or command line.
- A Twilio account which has a free tier available.
- Twilio Account SID and Auth Token from the Twilio Console.
- A Twilio phone number. This comes as part of your Twilio account.
- Familiarity with the Twilio virtual phone for testing if you don't want to use your personal phone number.
- Java SDK
- Maven
- IntelliJ IDEA
These are the steps to create a new Java project with IntelliJ IDEA:
- Open IntelliJ IDEA.
- Click New Project or File > New > Project with IntelliJ already open.
- 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.
- Select Create from archetype.
- Check Create from archetype.
- Select maven-archetype-quickstart from the list.
- Click Next to move to New Project screen with fields for GroupId, ArtifactId, and Version.
- Click Advanced Settings arrow to expand additional options.
- Add your project information:
- Type
com.exampleinto the GroupId box. - Type
twilio-java-exampleinto the ArtifactId box. - Don't change the value in the Version box.
- Type
- IntelliJ populates the Project name field on ArtifactId value.
- Choose where to save your project and set it in the Project location box.
- Click Next.
- Click Finish.
IntelliJ creates the correct directory structure and open the project. Your application's source code goes in the src/main/java directory.
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.
To interact with Twilio services, add the Twilio Java helper library to your project through adding a dependency to your build configuration.
-
In IntelliJ, open the
pom.xmlfile in your project root. -
Find the
<dependencies>section. -
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> -
After adding the dependency, IntelliJ displays notification about Maven changes.
-
Click Import Changes or Load Maven Changes when prompted.
These are the steps to create your first Twilio Java application using Java:
-
Create a Java file called
TwilioExample.javain yoursrc/main/javadirectory:1import com.twilio.Twilio;2import com.twilio.rest.api.v2010.account.Call;3import com.twilio.type.PhoneNumber;4import java.net.URI;56public class TwilioExample {7// Replace with your actual credentials (consider using environment variables)8public static final String ACCOUNT_SID = "your_account_sid_here";9public static final String AUTH_TOKEN = "your_auth_token_here";1011public static void main(String[] args) {12// Initialize Twilio13Twilio.init(ACCOUNT_SID, AUTH_TOKEN);1415Call call = Call.creator(new com.twilio.type.PhoneNumber("+18005550100"),16new com.twilio.type.PhoneNumber("+18005550199"),17URI.create("http://demo.twilio.com/docs/voice.xml"))18.create();19}20} -
Replace
your_account_sid_hereandyour_auth_token_herewith your actual Twilio Account SID and Auth Token from the Twilio Console. For production applications, use environment variables 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)2public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");3public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");45// Initialize Twilio with environment variables6Twilio.init(ACCOUNT_SID, AUTH_TOKEN);To set environment variables:
On Windows (Command Prompt):
1set TWILIO_ACCOUNT_SID=your_account_sid_here2set TWILIO_AUTH_TOKEN=your_auth_token_hereOn Windows (PowerShell):
1$env:TWILIO_ACCOUNT_SID="your_account_sid_here"2$env:TWILIO_AUTH_TOKEN="your_auth_token_here"On macOS/Linux:
1export TWILIO_ACCOUNT_SID=your_account_sid_here2export TWILIO_AUTH_TOKEN=your_auth_token_here -
To set the receiving, or To, phone number, replace the first
phoneNumberin thecallmethod. -
To set the sending, or From, phone number, replace the second
phoneNumberin thecallmethod. 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:
- Right-click on
TwilioExample.javain IntelliJ. - Select Run 'TwilioExample.main()'.
- Check the console output for the call SID confirmation.
- Verify that the receiving phone number got the call.
You're ready to build out your Java application. Learn more with the following resources: