Spark SDK for Node.js: Getting Started

In this example, we'll use Node.js to create a simple project that will post a message to a room.

Dependencies

First, make sure your system has NodeJS and npm. We use nvm to install them, but the official downloads from nodejs.org will also work.

Note: npm is typically bundled with node, but some Linux package managers may require you to install npm explicitly.

Once you’ve got the dependencies in place, create a new node project.

Create the Project Directory

cd /my/project/directory
npm init

You’ll be prompted to answer a few simple questions, but typically the defaults are just fine.

Install the JavaScript SDK

Now, install ciscospark.

npm install --save ciscospark

Basic usage

So, you want to send a message. First, you need an access token. For demonstration purposes, we’ll use environment variables to authorize the sdk. You can get an access token from the Cisco Spark Developer Portal.

Create a new file in your project directory named index.js and add the following to it:

var spark = require('ciscospark');
spark.rooms.create({
  title: 'My First Room!'
})
  // Make sure to log errors in case something goes wrong.
  .catch(function(reason) {
    console.error(reason);
    process.exit(1);
  });

Now, open the Cisco Spark Web Client so you see your code in action. Then, back in your terminal, run the following command.

CISCOSPARK_ACCESS_TOKEN= node index.js

Check out the web client. You should see your new room. Now, let’s send a message to it.

Open up index.js again and replace its contents with the following code:

var spark = require('ciscospark');
spark.rooms.list({
  max: 10
})
  .then(function(rooms) {
  var room = rooms.items.filter(function (room) {
    return room.title === 'My First Room!';
  })[0];

  return spark.messages.create({
    text: 'Hello World!',
    roomId: room.id
  });
})
// Make sure to log errors in case something goes wrong.
.catch(function(reason) {
  console.error(reason);
  process.exit(1);
});

Run it again with the following:

CISCOSPARK_ACCESS_TOKEN=$YOUR_TOKEN_FROM_THE_PORTAL node index.js

And checkout the web client. Congrats! You’ve sent your first message!