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.
Learn how to quickly send SMS messages in a Java application with the Sinch API.
To quickly get started setting up a simple client application using the Java SDK:
If you haven't already, clone the sinch-sdk-java-quickstart repository.
Navigate to the
getting-started/sms/send-sms-message/client/src/main/resources
folder.Open the
config.properties
file. Using the access key credentials from your Sinch Build Dashboard, populate the following fields with your values:Field Description SINCH_PROJECT_ID The unique ID of your Project. SINCH_KEY_ID The unique ID of your access key. SINCH_KEY_SECRET The secret that goes with your access key.
Note: For security reasons, this secret is only visible right after access key creation.SMS_REGION This optional setting can be uncommented/included if you'd like to target a SMS API server in a region other than the US. Save the file.
Navigate to the
/getting-started/sms/send-sms-message/client/src/main/java/sms/
folder and open theSnippet.java
file.Snippet.javapackage sms; import com.sinch.sdk.domains.sms.api.v1.BatchesService; import com.sinch.sdk.domains.sms.api.v1.SMSService; import com.sinch.sdk.domains.sms.models.v1.batches.request.TextRequest; import com.sinch.sdk.domains.sms.models.v1.batches.response.BatchResponse; import java.util.Collections; import java.util.logging.Logger; public class Snippet { private static final Logger LOGGER = Logger.getLogger(Snippet.class.getName()); static void execute(SMSService smsService) { BatchesService batchesService = smsService.batches(); String from = "YOUR_sinch_phone_number"; String recipient = "YOUR_recipient_phone_number"; String body = "This is a test SMS message using the Sinch Java SDK."; LOGGER.info(String.format("Submitting batch to send SMS to '%s'", recipient)); BatchResponse value = batchesService.send( TextRequest.builder() .setTo(Collections.singletonList(recipient)) .setBody(body) .setFrom(from) .build()); LOGGER.info("Response: " + value); } }
The code provided in Snippet.java includes placeholder parameters. Replace the following placeholder values for these parameters with your values:
Parameter Your value YOUR_Sinch_phone_number
Any number you've assigned to your Sinch account. Find the number on your Sinch dashboard by clicking the service plan ID link and scrolling to the bottom of the page. YOUR_recipient_phone_number
The phone number to which you want to send the test SMS message. Save your file.
Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the sinch-sdk-java-quickstart/sms/send-sms-message/client
folder and run the following command:
mvn package
This command creates the target
folder and application.
Now that your package is ready, you can send a text message to your mobile phone. To send the message, run the following code:
java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar
The console will display the response details received from the Sinch servers and you should receive an SMS message to the number you specified.
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.
- Explore the API specification to test more endpoints.