Contact Us

Send a text message with javascript in under 2 minutes

Prerequisites

Step 1: Set up a project

Open a terminal or command prompt and start a new node.js project with

mkdir sender
cd sender
npm init

 

Give your project a name like “sender” and enter in any optional project details as desired:

Step 2: Install the SDK

Adding the dependency to the project is as simple as entering the command:

npm install messagemedia-messages-sdk

Step 3: Add the code

Open the folder in your text editor and add a file called index.js to the root of the package. Paste the following code into index.js and add in your API Key and Secret, and your phone number:

const lib = require('messagemedia-messages-sdk');

/* Basic Auth */
lib.Configuration.basicAuthUserName = "YOUR_BASIC_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_BASIC_SECRET_KEY";

/* HMAC
lib.Configuration.hmacAuthUserName = "YOUR_HMAC_API_KEY";
lib.Configuration.hmacAuthPassword = "YOUR_HMAC_SECRET_KEY";
*/

var controller = lib.MessagesController;

let body = new lib.SendMessagesRequest();

body.messages = [];

body.messages[0] = new lib.Message();

body.messages[0].content = 'Hello world!';
body.messages[0].destinationNumber = '+614';

controller.sendMessages(body, function(error, response, context) {
    if (error) {
        console.log(error);
    } else {
        console.log(response);
    }
});

Step 4: Send

Save your code and type the following in the console:

node index.js

 

If it all works, “success” will print in the console and you will receive the message. Edit the console message to include details of the response, if you need. You’ve now sent a simple message with Java. Check out the API documentation and the Github repository to see what else you can do.