npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

messagebird

v4.0.1

Published

A node.js wrapper for the MessageBird REST API

Downloads

119,481

Readme

MessageBird REST API for Node.js

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:

const messagebird = require('messagebird').initClient('<YOUR_ACCESS_KEY>');

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

import { initClient } from 'messagebird';
const messagebird = initClient('<YOUR_ACCESS_KEY>');

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

For each HTTP request that MessageBird sends, a MessageBird-Signature-JWT header is added.

The MessageBird-Signature-JWT header is a signature that consists of all the information that is required to verify the integrity of the request. The signature is generated from the request URL and request body and is signed with the HMAC-SHA256 algorithm using your your signing key. You can validate this signature using our SDKsto ensure that the request is valid and unaltered. The token also includes timestamp claims that allow you to prove the time of the request, protecting from replay attacks and the like. For more details consult the documentation.

Examples:

Let's use Express Signature middleware to verify webhooks.

// This example show how to verify the authenticity of a MessageBird webhook.
const mbWebhookSignatureJwt = require('messagebird/lib/webhook-signature-jwt');
const express = require('express');

const secret = '<YOUR SIGNING KEY>';

const app = express();

// If the node server is behind a proxy, you must trust the proxy to infer the correct protocol and hostname.

app.set('trust proxy', () => true);

// Replace <YOUR_SIGNING_KEY> with your actual signing key.
const verifySignature = new mbWebhookSignatureJwt.ExpressMiddlewareVerify(secret);

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

// Verified webhook.
app.get('/webhook', verifySignature, (req, res) => {
  res.send('verified');
});
app.post('/webhook', verifySignature, (req, res) => {
  res.send('verified');
});

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) 2022, MessageBird