Sending SMS with Java using Sendchamp API

In this tutorial, you’ll learn how to send SMS messages with Java using sendchamp SMS API.

Aliyu Abubakar

Sep 5, 2021

In this Sendchamp Developer Tutorial you’ll learn how to send SMS using the Senchamp SMS API with Java.

In this guide we will walk you through:

  1. Signing up free Sendchamp account
  2. Create a Sender ID
  3. Setting up Java Application
  4. Sending your first message

Prerequisite

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

  • Java and a familiarity with how to create a new app.
  • A sendchamp account

Signing up free sendchamp account

If you Already have an account? skip to the next section You can sign up for a free sendchamp account here.

image

When you sign up, you'll have to verify your email address. This allows sendchamp to verify your identity.

After you've verified your email address, you'll be asked a question in order to get you started in a way that's relevant to you.

After you finish setting up your account, you'll have access to your dashboard where you can access your API Key and perform dashboard communication functions.

Create a Sender ID

Once your account is created, you’ll need to add a new sender ID in order to send SMS Messages. Click channels on your dashboard under settings, click on SMS channel and add a sender ID. Take note of your ID, because you'll use it later.

image

Make sure you set up your development environment before continuing; you can read on how to set up your Java Environment.

You can now make your API request and send an SMS message with Sendchamp using Java.

Setting up Your Java Application

First, let's create a new file in the directory and call it sendSms.Java.

Replace the string PUBLIC-KEY in the following Java code and hardcode your API key.

Note: For production applications, we recommend storing your key in a configuration file or environment variable instead and passing this variable with the key to the require function.

Unirest.setTimeouts(0, 0);
HttpResponse<String> response = Unirest.post("https://sandbox-api.sendchamp.com/api/v1/sms/send")

The above base URL is for testing and messages are previewed using simulator. To be able to send live messages, you'll have to switch to live on your dashboard and replace your public key.

Live Base URL

response = Unirest.post("https://api.sendchamp.com/api/v1/sms/send")

Now, to send a message, we add our API Access key and pass a few required arguments:

.header("Accept", "application/json")
.header("Authorization", "Bearer PUBLIC_KEY")
.header("Content-Type", "application/json")
.body("{\n    \"to\": [\"2348055372961\"],\n    
\"message\": \"Hello from Olumide Latest\",\n  
\"route\": \"non_dnd\"\n}")  
\"sender_name\": \"Kuda\"\n}")
 .asString();

But wait, what do these attributes mean?

  • The originator is the sender of the message; This Sender ID must have been requested via the dashboard or use "Sendchamp" as default.

  • The recipients are the phone numbers that will receive the message. All numbers should be in the international format with country code. You must specify this attribute as an array even if you have just a single recipient. You can send a message to up to 50 numbers at a time.

  • The body is the content of the message. SMS messages are limited to 160 characters, and if you enter a longer value for the body it will be split into multiple parts which are each sent and billed separately.

  • Set route to dnd, non_dnd or international.

Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in the SMS API documentation.

If everything works fine, you should see the API response as output from the script. If you used a live API key and added funds to your wallet, the message will be delivered to the recipient.

Thanks for getting to the end of the tutorial. Be sure that if you have more questions, you can join our [Slack community] (https://bit.ly/community-tutorial-link) here and we will help resolve it.

© 2022 Sendchamp. All rights reserved.