Quick Start
Outdated: this section documents The Things Network V2, which is no longer maintained. See the documentation for The Things Stack V3, or browse the V2 docs index.
In this Quick Start, you’ll learn how to initialize a new Node.js application, install the Node.js Application SDK and receive uplink messages from your application.
Setup #
Let’s install Node.js, create a Node.js project and install the Node.js Application SDK.
-
Download and install Node.js.
-
Create a new Node.js project:
cd $HOME mkdir app cd app npm initJust press return to accept the default answer to any question asked.
-
Install and save the Node.js Application SDK as dependency:
npm install --save ttn
Connect #
Next, we will write the script that requires the Node.js Application SDK and uses it to connect.
-
Create the main script and open it in your favorite editor:
touch index.js open index.js -
Import the Node.js Application SDK:
var ttn = require("ttn") -
In the Console, navigate to the application you’d like to connect to.
-
In
index.js, enter the App ID and App Access Key:var appID = "foo" var accessKey = "ttn-account-v2.eiPq8mEeYRL_PNBZsOpPy-O3ABJXYWulODmQGR5PZzg"Here’s where you can find the values in the console:
- For
appID, copy the value for Application ID in the Application Overview box. - For
accessKey, scroll down and then show and copy the value for default key in the Access Key box.
- For
-
Add a listener for the
connectanderrorevents to test the connection:ttn.data(appID, accessKey) .then(function (client) { client.on("uplink", function (devID, payload) { console.log("Received uplink from ", devID) console.log(payload) }) }) .catch(function (error) { console.error("Error", error) process.exit(1) }) -
Run the script to test the connection:
node .You should see something like:
Received uplink from office-hq { app_id: 'office-app', dev_id: 'office-hq', hardware_serial: '0004A30B001B61DE', port: 1, counter: 8717, payload_raw: <Buffer 01 e9 02 b1 01 69>, payload_fields: { celcius: 6.89, light: 489, sound: 361, state: { on: true } }, metadata: { time: '2017-08-31T12:38:35.784566782Z', frequency: 868.3, modulation: 'LORA', data_rate: 'SF7BW125', coding_rate: '4/5', gateways: [ [Object], [Object], [Object] ] } }Use
Ctrl+Cto terminate the script.If you get an error it should say what is wrong:
Error: Connection refused: Not authorized
🎉 Congratulations! Now you know how to receive messages in a Node.js script. Go build something! See API for all the functionality