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 21 or later and a familiarity with how to create a new Java application. (The SDK itself only requires JDK 8 or later, but this quickstart guide uses JDK 21 as it is the latest version with long term support.)
- Apache Maven and a familiarity with how to use the Maven CLI.
- ngrok. You'll use ngrok to open a tunnel to your local server.
In this tutorial, you will learn how the Java SDK can be used to handle inbound SMS messages. You will create an application that receives an inbound SMS and replies with an SMS acknowledging the content of your inbound message.
Set up your Java application
To quickly get started setting up a simple client application using the Java SDK:
- Clone the sinch-sdk-java-quickstart repository. (If you have already cloned the quickstart in another guide, you do not need to do this step.)
- Navigate to the
getting-started/sms/respond-to-incoming-message/server/src/main/resources
folder. - Your Sinch credentials can be found on the Access Keys page of the Customer Dashboard. You can also create new access key IDs and Secrets, if required.
To add them, open the
application.yaml
file and enter them under thecredentials:
tag:project-id:
- project IDkey-id:
- API key IDkey-secret:
- API key secret
You also need to set the SMS region under theNote:
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.sms:
tag:region
- SMS region (US/EU)
Start your web server and set up a tunnel
When you send an SMS to your Sinch number, the Sinch server sends a request to a callback event URL you need to configure on your SMS service.
- Navigate to
getting-started/sms/respond-to-incoming-message/server
and start the server by executing the following command:mvn clean spring-boot:run
- Open a tunnel to the server you just set up. We are using ngrok for this. If you don't have ngrok installed already, install it with the following command:
npm install ngrok -g
- Open a terminal or command prompt and enter:
ngrok http 8090
You will see a screen like the following.
- On the highlighed "Forwarding" line, copy the address ending in
.ngrok.io
and add/SmsEvent
to the end of it.
Configure your Callback URL
- To configure a callback URL for your Sinch account, login to your dashboard.
- Click on the service plan ID link and edit the Callback URL field with the ngrok.io domain URL from the previous section.
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
- Explore the API specification to test more endpoints.