Sinch Developer Documentation

Building blocks to enable rich customer communication

Getting started - Java

Learn how to quickly send SMS messages with the Sinch API

In this guide, we show you how to:

  1. Create an account and get your free test number (US only).
  2. Send your first SMS.

Sign up for a Sinch account

Before you can send your first SMS, you need a Sinch
account
. If you are in the United States, you also need a free test phone number.

Image of configure numberImage of configure number
Click activate.

Image of configure numberImage of configure number
To use the number with the rest API select REST and click GET FREE TEST NUMBER.

Installing Java helper library

To use java, install our Java library

Send SMS

 private static final String[] RECIPIENTS = {"1232323131", "3213123"};
 try (ApiConnection conn =
        ApiConnection.builder().servicePlanId(SERVICE_PLAN_ID).token(TOKEN).start()) {
      // Sending a simple Text Message
      MtBatchTextSmsResult batch =
          conn.createBatch(
              SinchSMSApi.batchTextSms()
                  .sender("{your free test number}")
                  .addRecipient(RECIPIENTS)
                  .body("This is a test message!")
                  .build());

      System.out.println("Successfully sent batch " + batch.id());
    } catch (Exception e) {
      System.out.println("Batch send failed: " + e.getMessage());
    }

Before you can execute the code that sends an SMS message, you need to modify it in a few places.

Replace the token values

Before you can execute the code that sends an SMS message, you need to replace the following values with your values:

{service_plan_id}
{your token}
{your free test number}
{To number}

To find the service plan and token, go to https://dashboard.sinch.com/sms/api/rest, log in and click “Show” to reveal your API token.

Screen shot of dashboardScreen shot of dashboard

To find the From-number, click the service plan id link and scroll to the bottom of the page and then change the {To number} to your phone number.

Click here to read more about the batches endpoint.

Receive SMS via web-hook

The next step shows how to handle the sending of an SMS to your Sinch number.

Configure the Callback URL for your SMS service

Before you implement the server to handle incoming SMS, you need to configure your SMS service to handle callbacks. Head over to https://dashboard.sinch.com/sms/api/rest, click your service and you will see a section like the below.

Screen shot of dashboardScreen shot of dashboard

If you just want to look at what we post you can use http://requestbin.net/

Click create and you will see:

Screen shot of request binScreen shot of request bin

Copy the bin URL to the callback URL info and click Save.

Screen shot of callback configuredScreen shot of callback configured

That's it! You're now ready to send an SMS to your Sinch number

To see the data we send on incoming SMS, refresh your request bin page.

requestbin requestrequestbin request

Handle incoming SMS with Java Spring boot

Below is a spring controller.

@RestController
public class InboundController {
    @PostMapping("/sms/incoming")
    public ResponseEntity<Object> receiveInbound(@RequestBody Object body) {
        System.out.println(body);
        return new ResponseEntity<>("Accepted", HttpStatus.OK);
    }
}

Before you can handle incoming traffic to your local server, you need to open up a tunnel to your local server. For that, you can use ngrok tunnel. Open a terminal/command prompt and type: ngrok http 3000

requestbin requestrequestbin request

Go back to your dashboard and change the callback URL for your SMS service.

  1. In the terminal windows, start running the SpringBoot Application with ./gradlew bootRun if gradle or ./mvnw spring-boot:run if Maven
  2. Send an SMS to your Sinch Number.
  3. You will now see the request come in.

requestbin requestrequestbin request

You can read more about all the different endpoints in the API reference guide

Edit on GitHub

Updated 3 days ago


Getting started - Java


Learn how to quickly send SMS messages with the Sinch API

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.