/messages-nodejs-sdk

MessageMedia NodeJS SDK for sending and receiving messages

Primary LanguageJavaScriptApache License 2.0Apache-2.0

MessageMedia Messages NodeJS SDK

Pull Requests Welcome HitCount npm

The MessageMedia Messages API provides a number of endpoints for building powerful two-way messaging applications.

Isometric

Table of Contents

🔐 Authentication

Authentication is done via API keys. Sign up at https://developers.messagemedia.com/register/ to get your API keys.

Requests are authenticated using HTTP Basic Auth or HMAC. For Basic Auth, your API key will be basicAuthUserName and API secret will be basicAuthPassword. For HMAC, your API key will be hmacAuthUserName and API secret will be hmacAuthPassword. This is demonstrated in the Send an SMS example below.

⁉️ Errors

Our API returns standard HTTP success or error status codes. For errors, we will also include extra information about what went wrong encoded in the response as JSON. The most common status codes are listed below.

HTTP Status Codes

Code Title Description
400 Invalid Request The request was invalid
401 Unauthorized Your API credentials are invalid
403 Disabled feature Feature not enabled
404 Not Found The resource does not exist
50X Internal Server Error An error occurred with our API

📰 Information

Slack and Mailing List

If you have any questions, comments, or concerns, please join our Slack channel: https://developers.messagemedia.com/collaborate/slack/

Alternatively you can email us at: developers@messagemedia.com

Bug reports

If you discover a problem with the SDK, we would like to know about it. You can raise an issue or send an email to: developers@messagemedia.com

Contributing

We welcome your thoughts on how we could best provide you with SDKs that would simplify how you consume our services in your application. You can fork and create pull requests for any features you would like to see or raise an issue. Please be aware that a large share of the files are auto-generated by our backend tool.

⭐ Installation

Now install messagemedia-messages-sdk via npm by using:

npm install messagemedia-messages-sdk

Alternatively, add the following to the dependencies section of your package.json:

"messagemedia-messages-sdk": "^2.0.1"

🎬 Get Started

It's easy to get started. Simply enter the API Key and secret you obtained from the MessageMedia Developers Portal into the code snippet below and a mobile number you wish to send to.

Send an SMS

Destination (destinationNumber) and source number (sourceNumber) should be in the E.164 format. For example, +61491570156 NOT 0491570156. The code snippet below comprises of only the bare minimum parameters required to send a message. You can view the full list of parameters over here. Alternatively, you can refer this code snippet with all the parameters in use.

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 = '+61491570156';

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

Send an MMS

Destination (destinationNumber) and source number (sourceNumber) should be in the E.164 format. For example, +61491570156 NOT 0491570156. The code snippet below comprises of only the bare minimum parameters required to send a message. You can view the full list of parameters over here. Alternatively, you can refer this code snippet with all the parameters in use.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_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 = '+61491570156';
body.messages[0].format = lib.FormatEnum.MMS;
body.messages[0].media = ['https://upload.wikimedia.org/wikipedia/commons/6/6a/L80385-flash-superhero-logo-1544.png'];
body.messages[0].subject = 'This is an MMS message';

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

Get Status of a Message

You can get a messsage ID from a sent message by looking at the message_id from the response of the above example.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_SECRET_KEY";

var controller = lib.MessagesController;

let messageId = '877c19ef-fa2e-4cec-827a-e1df9b5509f7';

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

Get replies to a message

You can check for replies that are sent to your messages.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_SECRET_KEY";

var controller = lib.RepliesController;

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

Check Delivery Reports

This endpoint allows you to check for delivery reports to inbound and outbound messages.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_SECRET_KEY";

var controller = lib.DeliveryReportsController;

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

Confirm Delivery Reports

This endpoint allows you to mark delivery reports as confirmed so they're no longer returned by the Check Delivery Reports function.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_SECRET_KEY";

var controller = lib.DeliveryReportsController;

let body = new lib.ConfirmDeliveryReportsAsReceivedRequest();

body.deliveryReportIds = ['011dcead-6988-4ad6-a1c7-6b6c68ea628d', '3487b3fa-6586-4979-a233-2d1b095c7718', 'ba28e94b-c83d-4759-98e7-ff9c7edb87a1'];

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

Check credits remaining (Prepaid accounts only)

This endpoint allows you to check for credits remaining on your prepaid account.

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

lib.Configuration.basicAuthUserName = "YOUR_API_KEY";
lib.Configuration.basicAuthPassword = "YOUR_SECRET_KEY";

var controller = lib.MessagesController;

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

📕 API Reference Documentation

Check out the full API documentation for more detailed information.

😕 Need help?

Please contact developer support at developers@messagemedia.com or check out the developer portal at developers.messagemedia.com

📃 License

Apache License. See the LICENSE file.