Skip to content
Last updated

Send a Conversation Message with the Sinch Java SDK

Note:

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

  • Set all Conversation 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 Conversation messages in a Java application with the Sinch API.

Steps:
  1. Set up your Java application.

  2. Send your first Conversation message.

Set up your Java application

To quickly get started setting up a simple client application using the Java SDK:

  1. If you haven't already, clone the sinch-sdk-java-quickstart repository.

  2. Navigate to the getting-started/conversation/send-text-message/client/src/main/resources folder.

  3. Open the config.properties file. Using the access key credentials from your Sinch Build Dashboard, populate the following fields with your values:

    FieldDescription
    SINCH_PROJECT_IDThe unique ID of your Project.
    SINCH_KEY_IDThe unique ID of your access key.
    SINCH_KEY_SECRETThe secret that goes with your access key.

    Note: For security reasons, this secret is only visible right after access key creation.
    CONVERSATION_REGIONThis optional setting can be uncommented/included if you'd like to target a Conversation API server in a region other than the US. Note that you'll only be able to use Conversation API apps configured for same region as the one associated with the server you target. See the list of supported values for more information.
  4. Save the file.

Modify your application

  1. Navigate to the /getting-started/conversation/send-text-message/client/src/main/java/conversation/ folder and open the Snippet.java file.

    Snippet.java
    package conversation;
    
    import com.sinch.sdk.domains.conversation.api.v1.ConversationService;
    import com.sinch.sdk.domains.conversation.api.v1.MessagesService;
    import com.sinch.sdk.domains.conversation.models.v1.*;
    import com.sinch.sdk.domains.conversation.models.v1.messages.*;
    import com.sinch.sdk.domains.conversation.models.v1.messages.request.*;
    import com.sinch.sdk.domains.conversation.models.v1.messages.response.SendMessageResponse;
    import com.sinch.sdk.domains.conversation.models.v1.messages.types.text.*;
    import java.util.*;
    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(ConversationService conversationService) {
    
        MessagesService messagesService = conversationService.messages();
    
        String appId = "YOUR_app_id";
        String from = "YOUR_sms_sender";
        String to = "RECIPIENT_number";
    
        String body = "This is a test Conversation message using the Sinch Java SDK.";
        String smsSender = "SMS_SENDER";
    
        ChannelRecipientIdentities recipients =
            ChannelRecipientIdentities.of(
                ChannelRecipientIdentity.builder()
                    .setChannel(ConversationChannel.SMS)
                    .setIdentity(to)
                    .build());
    
        AppMessage<TextMessage> message =
            AppMessage.<TextMessage>builder()
                .setBody(TextMessage.builder().setText(body).build())
                .build();
    
        SendMessageRequest<TextMessage> request =
            SendMessageRequest.<TextMessage>builder()
                .setAppId(appId)
                .setRecipient(recipients)
                .setMessage(message)
                .setChannelProperties(Collections.singletonMap(smsSender, from))
                .build();
    
        LOGGER.info("Sending SMS Text using Conversation API");
    
        SendMessageResponse value = messagesService.sendMessage(request);
    
        LOGGER.info("Response: " + value);
      }
    }
  2. The code provided in Snippet.java includes placeholder parameters. Replace the following placeholder values for these parameters with your values:

    Placeholder valueYour value
    YOUR_app_idFind your app ID on your Sinch [dashboard](https://dashboard.sinch.com/convapi/apps).
    RECIPIENT_numberThe channel identity of the recipient to which you want to send the message. When using the SMS channel, this will be a phone number.
    YOUR_sms_senderYour Sinch virtual phone number, available on the [Sinch Build Dashboard](https://dashboard.sinch.com/numbers/your-numbers). This is only required if you are using the SMS channel.
  3. Save your file.

Package the application

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/getting-started/conversation/send-text-message/client folder and run the following command:

mvn package

This command creates the target folder and application.

Send your first Conversation message

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.

Next steps

The code included in the Snippet.java file sends a POST request to the Sinch API /messages:send endpoint to send the text message.

Additional resources