GithubHelp home page GithubHelp logo

viktormorales / messagebird-nodejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from messagebird/messagebird-nodejs

0.0 1.0 0.0 353 KB

The open source Node.js client for MessageBird's REST API

Home Page: https://www.messagebird.com/developers

JavaScript 94.05% TypeScript 5.95%

messagebird-nodejs's Introduction

MessageBird REST API for Node.js

Build Status

This repository contains the open source Node.js client for MessageBird's REST API. Documentation can be found at: https://developers.messagebird.com

Requirements

  • Sign up for a free MessageBird account
  • Create a new access_key in the developers section
  • MessageBird REST API for Node.js requires Node.js >= 0.10 or io.js

Installation

npm install messagebird

Usage

We have put some self-explanatory examples in the examples directory, but here is a quick breakdown on how it works. Let's go ahead and initialize the library first. Don't forget to replace <YOUR_ACCESS_KEY> with your actual access key.

CommonJS require syntax:

var messagebird = require('messagebird')('<YOUR_ACCESS_KEY>');

Typescript with ES6 import (or .mjs with Node >= v13):

import initMB from 'messagebird';
const messagebird = initMB('<YOUR_ACCESS_KEY>');

Tip: Don't forget to enable the esModuleInterop in tsconfig.json.

Nice! Now we can send API requests through node. Let's use getting your balance overview as an example:

// Get your balance
messagebird.balance.read(function (err, data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});

// Result object:
{
  payment: 'prepaid',
  type: 'credits',
  amount: 42.5
}

Or in case of an error:

{ [Error: api error]
  errors: [
    {
      code: 2,
      description: 'Request not allowed (incorrect access_key)',
      parameter: 'access_key'
    }
  ]
}

Notes

Messaging and Voice API use different pagination semantics:

Messaging API uses limit and offset params for list methods (where applicable)

// list conversations
//In this case 20 is limit and 0 is offset
messagebird.conversations.list(20, 0, function (err, response) {
  if (err) {
    return console.log(err);
  }
  console.log(response);
});

Voice API uses page and perPage params for list methods (where applicable)

// list Call Flows
// In this case 1 is page, 2 is items per page
messagebird.callflows.list(1, 2, function (err, response) {
  if (err) {
    return console.log(err);
  }
  console.log(response);
});

Verifying Signatures

We sign our HTTP requests to allow you to verify that they actually came from us (authentication) and that they haven't been altered along the way (integrity). For each HTTP request that MessageBird sends, a MessageBird-Signature and MessageBird-Request-Timestamp header is added. Signature middleware calculates a signature using the timestamp, query parameters and body then compares the calculated signature to MessageBird-Signature header. If they are not same or request expired, middleware throws an error. This way, you will know if the request is valid or not. If you want to verify request manually, you can check here. Let's use Signature middleware to verify webhooks.

var Signature = require('messagebird/lib/signature');

// Replace <YOUR_SIGNING_KEY> with your actual signing key.
var verifySignature = new Signature('<YOUR_SIGNING_KEY>');

// Retrieve the raw body as a buffer.
app.use(require('body-parser').raw({ type: '*/*' }));

// Verified webhook.
app.get('/webhook', verifySignature, function(req, res) {
    res.send("Verified");
});

Conversations Whatsapp Sandbox

To use the whatsapp sandbox you need to add "ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX" to the list of features you want enabled. Don't forget to replace <YOUR_ACCESS_KEY> with your actual access key.

var messagebird = require('messagebird')("<YOUR_ACCESS_KEY>", null, ["ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX"]);

Documentation

Complete documentation, instructions, and examples are available at: https://developers.messagebird.com

License

The MessageBird REST API for Node.js is licensed under The BSD 2-Clause License. Copyright (c) 2014, MessageBird

messagebird-nodejs's People

Contributors

fvdm avatar aodinok avatar niekert avatar epels avatar canuzunoglu avatar elmarburke avatar kcroxford-mb avatar samwierema avatar trololov avatar mariuspot avatar joeynoh avatar dependabot[bot] avatar sbogx avatar dstotijn avatar guilhermehubner avatar kevinverelst avatar marcelcorso avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.