We’re giving away 1,500 DJI Tello drones. Enter to win ›
By Cagatay Tanyildiz | Published January 2, 2018 - Updated January 2, 2018
When we’ve completed our application in Node-RED, we can then publish it and use API Connect to provide us with higher level REST API functionality. API Connect provides the following functionality:
Below is a creenshot of a API Connect UI:
This tutorial will demonstrate how to:
In order to use this how-to, the reader will need the following prerequisites:
The easiest way to deploy Node-RED flows is to import a JSON file that contains flows to your Node-RED palette. But first we need to install a few packages. After creating a Node-RED instance we first try to install a node into our palette. You can install nodes with the “Manage Palette” button.
When clicking the “Manage Palette” button, we first go to the “Install” section and search for “watson”. Then install both the node-red-contrib-ibm-watson-iot and node-red-contrib-ibm-wiotp-devices-ops packages.
We can now import flows into our editor. Dowload the allFlows.json file and copy the content of the file to clipboard then open your Node-RED palette. You will find the import section under the hamburger menu which is placed in the upper right corner. Click the clipboard button to import all the flows from the JSON file.
Before proceeding we will delete Flow 1, double click on the Flow 1 tab and use the “Delete” button to remove it.
Before continuing we need to configure the IoT nodes according to your service credentials. If you’re not sure how to create an API key and token for the IoT service, follow this How-to to find out how.
After creating your service credentials you need to open one of the IoT nodes and configure it with these values. See the screenshow below as an example.
Be sure to change the authentication method to API Key if it is not already set. Then click to pencil icon next to API Key section and enter your API key and token. After entering these credentials all of your IoT nodes will be properly configured.
Now let’s dig into the details for each flow, there are four different flows in the editor:
In this flow we are using the API key authentication method for communicating with the IoT Platform. With this method you can connect to any IoT Platform, it doesn’t need to be in your IBM Cloud space.
In this flow we are subscribing to data from Watson IoT Platform. Just like in the previous step, we can use an API key.
The only difference between this flow and the previous is the database portion. Here we can use an existing Cloudant Database, which was created when creating the Node-RED instance, or we can connect to an external database. For the purposes of this how-to we will use the existing Cloudant database.
In this flow we see the HTTP input button which creates an HTTP endpoint for creating a web service and HTTP output which creates a response for your web service. When getting a request for the /sensor-data API we use the database button to get all sensor information from the database and then serve the data over the HTTP response button.
After finishing the configuration we now need to test everything out. Click the red “Deploy” button which is placed at the upper right corner. You shouldn’t see any errors, but if there is an unexpected error you can see from your debug page which is placed at right part of the editor, next to “Info” section. If there are no errors click the little square next to interval button, this will launch the debug page. See the screenshot below as an example, the values in your debugger will be different.
At this point, we can send messages to the IoT Platform and read it from Node-RED successfully. With this simple click you just tested three flow which are “Mock Data to Watson IoT”, “Data From Watson IoT” and “Watson IoT to Database”. Now we need to test our REST API flow. Copy your Node-RED instance url into a browser and add /sensor-data to at the end of it. The URL should look like this: cagataytanyildiz.mybluemix.net/sensor-data. We are expecting to see a JSON blob from the database. If everything is correct we can go through the API Connect part below.
API Connect has a hierarchy for collecting, creating and serving APIs publicly. See the figure below to understand how API Connect is structured. For this how-to, we will be focusing on the “Catalog” to serve our APIs.
Let’s launch the API Connect instance from the IBM Cloud dashboard.
Once the API Connect UI is launched use the >> button, which placed next to the Dashboard button, then go to Drafts and select the API tab.
Click the ADD button, but before creating an API expand the Additional Properties and select Identify Using with None under the Security section.
Once completed, go to the Paths section and add a new /sensor-data path for your web service.
Now we need to bind our web service to an API. For this, go to the Assemble
section and click on the invoke diagram. When you click the invoke diagram,
you need to enter your web service url (for example,
https://cagataytanyildiz-node-red.mybluemix.net/sensor-data) and you select
request type, which is GET, for this scenario. Lastly set Cache Type
to No Cache.
After adding the service to your API we need to “Save” and Validate the API design. Click the Save button on the right side of the page. After getting a success message, click on the Validate button which is placed next to the Save button. We do not expect any error message in this section. After preparing the API we need to prepare the Product as well, let’s go back to the Drafts page, but this time we will add a Product.
Give the Product a name and and title. Once complete, click the Save button located on theright side of the page. Then go to Design section and click the Save button first the Validate button. We do not expect any error messages in this section.
Now we need to add our API to our Product. Go to the catalog and click onto the newly created product. Navigate to the API section and bind your product with your API, to do so, click on the Add API button which is placed right upper corner of the API section and select the saved API.
After that, click the Save button and then Stage button. Staging will take a few minutes. Once completed, we can publish it by going to the Dashboard. To do so, click on the >> button and select Dashboard, then the Sandbox catalog. Now select the staged product and publish it using the three dots menu item.
We can now test our API. Go back to your API Page through the Drafts section and open the Assemble section. Click the play button which placed next to search bar.
You should be able to see your path under the Operation dropdown. Select the GET operation. The returned status code should be a 200 OK, this indicates everything was completed successfully. Congratulations!
Using API Connect and Node-RED we were able to easily publish an application that used the Watson IoT Platform with little to no coding. Furthermore, it’s possible to use API Connect to connect different services together, not just Node-RED or the Watson IoT Platform. We encourage the reader to continue to read more about the API Connect and Node-Red services.
In order to learn more about Node-RED and API Connect, check out the following resources.
September 26, 2018
Back to top