Node.js SDK
anchorFeatures
anchor- Manage Webex Teams user accounts
- Send messages and file attachments
- Create spaces and manage space memberships
anchorGetting Started Guide
anchorIn this example, we'll use Node.js to create a simple project that will post a message to a room.
If you're interested in integrating Webex Teams functionality in your front-end web applications, check out the Browser SDK.
Overview
- Create a Webex Teams Room
- Post a Message
Requirements
Step 1: 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.
Step 2: Install the JavaScript SDK
Now, install ciscospark
:
npm install --save ciscospark
Step 3: Start Using the SDK
So, you want to send a message. For demonstration purposes, we’ll use environment variables to authorize the SDK with your personal access token. If you're logged into this site, you can find your token in Getting Started. This token provides access to your account for testing purposes, and shouldn't be used for anything other than testing. If you don't already have a Webex Teams account, click Sign Up at the top of this page to create a new account.
Create a new file in your project directory named index.js
and add the
following to it:
var spark = require('ciscospark/env');
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 Webex Teams Web Client to you see your code in action. Then, back in your terminal, run the following command:
CISCOSPARK_ACCESS_TOKEN=$YOUR_TOKEN_FROM_THE_PORTAL 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/env');
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
Now check out the web client. Congrats! You’ve sent your first message!
anchorTroubleshooting the Node.js SDK
anchorIf you're having trouble with the Node.js SDK, here's some more information to help troubleshoot the issue.
SDK Requirements
Review the following SDK requirements to make sure you're using the correct minimum versions of Node.js, npm, etc.:
- Node.js LTS 6.x and later
- npm 3.x and later
- Optionally: nvm to manage Node.js versions
- A Webex Teams account and an integration with the necessary scopes
Additional Logging
You can add additional logging to your application to help narrow down any issues with the SDK.
Change the Log Level via an Environment Variable
You can change the log level via the CISCOSPARK_LOG_LEVEL
environment
variable. For example, if you start your application server via npm
serve
, you'd set the variable before running your application to see
increased logging:
CISCOSPARK_LOG_LEVEL=info npm serve
While developing your application, you can set the
CISCOSPARK_LOG_LEVEL
environment variable to debug
see even more
logging. For example, if you start your application server via npm
serve-dev
, you'd set the variable before running your application to
see increased logging:
CISCOSPARK_LOG_LEVEL=debug npm serve-dev
Firewall Ports
The Node.js SDK makes use of several network protocols and ports. If you're encountering connection or connectivity issues, make sure there aren't any firewalls blocking or preventing communication with any Webex Teams endpoints. For more information about the network and web security requirements for Webex Teams services, please see this Webex Teams help article.
Getting Support
If you're stumped, contact the Webex Developer Support team for more help with the SDK.