Set up 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 an API key SID and secret, 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 opens 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.
To create your first Twilio Java application, follow these steps:
-
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 API_KEY = "your_api_key_here";10public static final String API_SECRET = "your_api_secret_here";1112public static void main(String[] args) {13// Initialize Twilio14Twilio.init(API_KEY, API_SECRET, ACCOUNT_SID);1516Call call = Call.creator(new com.twilio.type.PhoneNumber("+18005550100"),17new com.twilio.type.PhoneNumber("+18005550199"),18URI.create("http://demo.twilio.com/docs/voice.xml"))19.create();20}21} -
Replace
your_account_sid_herewith your Account SID, andyour_api_key_hereandyour_api_secret_herewith an API key SID and secret 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 API_KEY = System.getenv("TWILIO_API_KEY");4public static final String API_SECRET = System.getenv("TWILIO_API_SECRET");56// Initialize Twilio with an API key and secret7Twilio.init(API_KEY, API_SECRET, ACCOUNT_SID);To set environment variables:
On Windows (Command Prompt):
1set TWILIO_ACCOUNT_SID=your_account_sid_here2set TWILIO_API_KEY=your_api_key_here3set TWILIO_API_SECRET=your_api_secret_hereOn Windows (PowerShell):
1$env:TWILIO_ACCOUNT_SID="your_account_sid_here"2$env:TWILIO_API_KEY="your_api_key_here"3$env:TWILIO_API_SECRET="your_api_secret_here"On macOS/Linux:
1export TWILIO_ACCOUNT_SID=your_account_sid_here2export TWILIO_API_KEY=your_api_key_here3export TWILIO_API_SECRET=your_api_secret_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: