Send 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 11 or later and a familiarity with how to create a new Java application.
- Gradle and a familiarity with how use the Gradle build tools.
Learn how to quickly send SMS messages in a Java application with the Sinch API.
Install the Sinch Java library
- If you don't have a java project already, create one:
gradle init - Select application, and then accept all default values.
- Paste the following code into your
build.gradlefile:dependencies { implementation 'com.sinch:sdk-sms:1.0.5' implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.31' } //Sinch SDK is hosted by maven so make sure you have MavenCentral in your repositories repositories { mavenCentral() }
Send your first SMS message
OpenApp.java in your favorite editor and copy/paste in this sample. Remember to keep your own package com.Fill in your parameters
- Assign the following parameters in
App.javawith your values:Parameter Your value SERVICE_PLAN_IDThe service plan ID found on your Sinch Customer Dashboard. TOKENThe API token found on your Sinch Customer Dashboard. Click Show to reveal your API token. SENDERAny number you've assigned to your Sinch account. Find the number on your Sinch Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page. RECIPIENTSThe number to which you want to send the SMS message. Double check that the region is correct on your base URL. Learn more about regional options here.
- Save the file.
Send your first SMS message
Now that your Java project is ready and the Sinch SDK is installed, send a text message to your mobile phone.
To send the message, run the following code:
gradle runRECIPIENTS parameter.Next steps
The code used in your Java application sends a POST request to the Sinch API/batches endpoint to send the SMS message. Click here to read more about the batches endpoint.Additional resources
- Explore the API specification to test more endpoints.