Receive an SMS Message with Java

Note:

Before you can get started, you need the following already set up:

  • Set all SMS API configuration settings.
  • JDK 8 or later and a familiarity with how to create a new Java application.
  • Gradle and a familiarity with how use the Gradle build tools.
  • ngrok. You'll use ngrok to open a tunnel to your local server.

Handle and reply to incoming SMS Messages to your Sinch number.

When someone sends an SMS message to your Sinch number, we will send an http request to your server. This is known as a callback or webhook. You can configure your webhooks in your SMS API settings.

Learn how to quickly set up a Java server to receive SMS messages with the Sinch API using the Spark web application framework.

Steps:
  1. Set up your Java application
  2. Configure your callback URL
  3. Send your SMS message

Set up your Java application

  1. Create a new folder where you want to store your application. Open a command prompt or terminal to that location and execute the following command:
    Copy
    Copied
    gradle init

    This command starts up the process to create a new blank Java application. You can select the default options for most of the prompts, but ensure you select that you want to create an application.

    Note:

    This guide assumes you're using Gradle but if you want to use another Java build tool, we've provided installation instructions for the most popular ones below.

  2. To listen to incoming HTTP requests, we will use Spark. In your build.gradle file add the following dependencies:
    Copy
    Copied
    dependencies {
        implementation: 'com.sparkjava:spark-core:2.9.4'
        implementation: 'org.slf4j:slf4j-simple:2.0.9'
    }

Now that you've set up your application file, you must install the SDK itself and populate the code sample with your corresponding information.

The easiest way to install the SDK is using the Maven central repository.

Installing the SDK

You can create your Java project using your favorite method. Once your project is created, you need to add the SDK as a dependency. Depending on if you are using Gradle or Maven, you can add the necessary dependencies in the following ways:

Gradle

Add or ensure the following is in the application's build.gradle file:
Gradle with GroovyGradle with Kotlin
Copy
Copied
...
repositories {
    ...
    mavenCentral()
    ...
}
...

dependencies {
    ...
    implementation 'com.sinch.sdk:sinch-sdk-java:+'
    ...
}
...
Copy
Copied
...
repositories {
    ...
    mavenLocal()
    ...
}
...

dependencies {
    ...
    implementation("com.sinch.sdk:sinch-sdk-java:+")
    ...
}
...

Maven

Add the following to the pom.xml file:
Copy
Copied
<dependency>
    <groupId>com.sinch.sdk</groupId>
    <artifactId>sinch-sdk-java</artifactId>
    <version>LATEST</version>
</dependency>
Open App.java in your favorite editor and copy/paste in the App.java sample found on this page. Remember to keep your own package com.

Modify your application

The code provided in App.java includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.

Initialize the client

Before initializing a client using this SDK, you'll need three pieces of information:

  • Your Project ID
  • An access key ID
  • An access key Secret
These values can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.

To start using the SDK, you need to initialize the main client class and create a configuration object to connect to your Sinch account. You can find all of the credentials you need on your Sinch dashboard.

US RegionEuropean Region
Copy
Copied
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;

public class App {
    
    public static void main(String[] args) {
        SinchClient client = new SinchClient(Configuration.builder()
                                    .setKeyId("YOUR_access_key")
                                    .setKeySecret("YOUR_access_secret")
                                    .setProjectId("YOUR_project_id")
                                    .setSmsRegion(SMSRegion.US)
                                    .build());
    }
}
Copy
Copied
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.models.Configuration;
import com.sinch.sdk.models.SMSRegion;

public class App {
    
    public static void main(String[] args) {
        SinchClient client = new SinchClient(Configuration.builder()
                                    .setKeyId("YOUR_access_key")
                                    .setKeySecret("YOUR_access_secret")
                                    .setProjectId("YOUR_project_id")
                                    .setSmsRegion(SMSRegion.EU)
                                    .build());
    }
}
Note:

For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials.

Set up an ngrok tunnel and run your application

  1. In the terminal or command prompt window, start running your application with:
    Copy
    Copied
    gradle run
  2. Before you can handle incoming traffic to your local server, open up a tunnel to your local server. For that, you can use an ngrok tunnel. If you haven't already, install ngrok, and then open a terminal or command prompt. Run the following command:
    Copy
    Copied
    ngrok http 4567
    Take note of the URL that ends in .ngrok.io.

Configure your Callback URL

You need to configure a Callback URL for your Sinch account. Login to your Sinch Customer Dashboard. Click on the service plan ID link and edit the Callback URL field with the ngrok.io domain URL from the previous step.

Send your SMS message

Now send an SMS message to your Sinch number from your mobile phone and you will get an automatic reply.

Next Steps

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.