Sending SMS Messages

Learn how to quickly send SMS messages in a Java application with the Sinch API.

In this guide you will learn how to:

Learn how to:
  1. Sign up for your free Sinch account
  2. Buy a number
  3. Install the Sinch Java library
  4. Send your first SMS message

What you need to know before you start

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

Sign up for your free account

Already have an account?

If you already have a free account, skip to the next section

You can sign up for a free Sinch account here.

  • When you sign up, you'll have to verify your email address and phone number. This lets Sinch verify your identity and you'll also be able to use your phone to send and receive test messages.
  • After you've verified your number, you'll be asked some questions in order to get you started in a way that's relevent to you.
  • After you finish setting up your account, you'll have access to your dashboard where you can view all of your projects and applications. You can also access your account ID, set up API tokens, get phone numbers, and more.

Buy a number

Already have an number?

If you already have sinch number, skip to the next section

You can get numbers that are set up for SMS, Voice, or both. You can buy a number from any country where Sinch has numbers in inventory. Countries with numbers available for purchase are marked with Direct Access. All other countries will require you to submit a request to Sinch for manual handling.

You can search for and purchase virtual numbers by logging in to your dashboard.

  1. Go to Numbers | Get virtual numbers, fill in the required information, and click SEARCH. If you don't want to specify anything about the number, choose your country and click SEARCH. Available numbers to buy are displayed in the list.

  2. When you see a number you want to buy, click GET next to the number to buy it.

  3. You can configure your number with an SMS, Voice or Campaign ID (only for 10DLC numbers) when you activate the number.

    When you get a virtual number, if supporting documentation is needed, you will see a message in an orange box at the bottom of your form. You won't be able to get a virtual number until supporting documentation is submitted. Click here to learn more about supporting documentation.

Install the Sinch SDK for Java

If you don't have a java project already, create one:

gradle init

Select your application, and then accept all default values.

Paste the following code into your build.gradle file:

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 SMS message

Now that your Java project is ready and the Sinch SDK is installed you can send a text message to your mobile phone.

Open App.java in your favorite editor and copy/paste in this sample. Remember to keep your own package com.

Send an SMS message


// Find your serviceplan_id and Token at https://dashboard.sinch.com/sms/api
// Install the Java helper library at https://developers.sinch.com/docs/sms/sdks/java/
// Find your sinch number at // https://dashboard.sinch.com/numbers/your-numbers/numbers
import com.sinch.xms.*;
import com.sinch.xms.api.*;

public class App {
	private static final String SERVICE_PLAN_ID = "";
	private static final String TOKEN = "";
	private static ApiConnection conn;

	public static void main(String[] args) {
		String SENDER = ""; //Your sinch number
		String[] RECIPIENTS = { "" }; //your mobile phone number

		ApiConnection conn = ApiConnection
								.builder()
								.servicePlanId(SERVICE_PLAN_ID)
								.token(TOKEN)
								.start();
		MtBatchTextSmsCreate message = SinchSMSApi
										.batchTextSms()
										.sender(SENDER)
										.addRecipient(RECIPIENTS)
										.body("Test message from Sinch.")
										.build();
		try {
			// if there is something wrong with the batch
			// it will be exposed in APIError
			MtBatchTextSmsResult batch = conn.createBatch(message);
			System.out.println(batch.id());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("you sent:" + message.body());
	}
}

Assign the following parameters in App.java with your values:

ParameterYour value
SERVICE_PLAN_IDThe service plan ID found on your Sinch dashboard.
TOKENThe API token found on your Sinch dashboard. Click Show to reveal your API token.
SENDERAny 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.
RECIPIENTSThe number to which you want to send the SMS message.

To send the message, run the following code:

gradle run

You should receive an SMS message to the number you assigned to your RECIPIENTS parameter.

Next steps

The code you 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.

Send an SMS message


// Find your serviceplan_id and Token at https://dashboard.sinch.com/sms/api
// Install the Java helper library at https://developers.sinch.com/docs/sms/sdks/java/
// Find your sinch number at // https://dashboard.sinch.com/numbers/your-numbers/numbers
import com.sinch.xms.*;
import com.sinch.xms.api.*;

public class App {
	private static final String SERVICE_PLAN_ID = "";
	private static final String TOKEN = "";
	private static ApiConnection conn;

	public static void main(String[] args) {
		String SENDER = ""; //Your sinch number
		String[] RECIPIENTS = { "" }; //your mobile phone number

		ApiConnection conn = ApiConnection
								.builder()
								.servicePlanId(SERVICE_PLAN_ID)
								.token(TOKEN)
								.start();
		MtBatchTextSmsCreate message = SinchSMSApi
										.batchTextSms()
										.sender(SENDER)
										.addRecipient(RECIPIENTS)
										.body("Test message from Sinch.")
										.build();
		try {
			// if there is something wrong with the batch
			// it will be exposed in APIError
			MtBatchTextSmsResult batch = conn.createBatch(message);
			System.out.println(batch.id());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println("you sent:" + message.body());
	}
}