In this chapter, let's call a web API from Node-RED. Basically, in Node-RED, processing is performed as per the created flow, but it is JSON data that connects processing. In that sense, it is very compatible with web APIs.
Let's get started with the following four topics:
By the end of this chapter, you will have mastered how to call any type of web API from Node-RED.
To progress through this chapter, you will need the following:
The code used in this chapter can be found in Chapter07 folder at https://github.com/PacktPublishing/-Practical-Node-RED-Programming.
Many of you reading this book may already be familiar with web APIs. However, let's review the RESTful API in order to call a web API with Node-RED.
REST stands for Representational State Transfer. RESTful API basically refers to the invocation interface in HTTP of a web system that is implemented according to "REST principles." So, in a broad sense, it's safe to say that the REST API and RESTful API are the same things. So, what exactly is the RESTful API? We will learn the outline and principles of the RESTful API, and the advantages and disadvantages of using the RESTful API, in this section.
REST was proposed by Roy Fielding, one of the HTTP protocol creators, around the year 2000, and is a set (or way of thinking) of design principles suitable for linking multiple software when building a distributed application. In addition, the RESTful API is an API designed according to the following four REST principles:
These are the four principles. As you can see from these four principles, a major feature of REST is that it makes more effective use of HTTP technology and has a high affinity with web technology. Therefore, it is currently used for developing various web services and web applications.
With the recent widespread use of smartphones, it is becoming more obvious that business systems can be used not only on PCs but also on mobiles. In addition, not just one system but a system that can be linked with multiple systems and various web services will not be selected by users. RESTful APIs are receiving a great deal of attention as an indispensable tool for solving these problems.
As the following figure shows, a web API can be called from anywhere via the internet:
Figure 7.1 – RESTful API diagram
Now, let's recall what Node-RED is. Its workflow tool-like style is like a standalone tool, but Node-RED is certainly one of web applications too. In other words, it's an application that works very well with the RESTful API described here.
Next, let's cover again what kinds of parameters Node-RED nodes have.
Of the many nodes that Node-RED has, not many are suitable for calling web APIs (REST APIs). A typical node used when calling the web API is the http request node.
To call an external API on Node-RED, simply set the endpoint URL of the API to the URL property of the http request node.
For example, when it is necessary to set a parameter in the endpoint URL when calling an API, it is possible to set the output value of the previous node connected. The method is very easy. Instead of a literal string, you can just set the {{payload}} variable in the value setting part of the parameter.
In {{payload}}, the character string inherited from the previous processing node is entered.
Take the following example (note that this URL does not exist): http://api-test.packt.com/foo?username={{payload}}&format=json:
Figure 7.2 – Setting the API endpoint URL with {{payload}} as a parameter
The process of the http request node cannot be executed by the http request node alone. Before the http request node, it is necessary to connect the trigger process, such as the inject node. At that time, if there is a parameter you want to pass to the API call, that is, the http request node, please set it in msg.payload.
If the API you want to call in the http request node is POST, the JSON data to be included in the request will be satisfied as a request parameter by creating it in the preprocessing node, storing it in msg.payload as it is, and connecting it to the http request node.
By using the http request node like this, API cooperation can be easily realized. API calls are important for linking multiple services on Node-RED. For example, the function node of Node-RED is basically processed by JavaScript, but by making a program developed in other development languages, such as Java, into an API, it can be used by calling from Node-RED.
So far, we've learned what a RESTful API is and which node is appropriate for an API call.
In this part, let's create a flow that actually calls the API from Node-RED and learn how to call the API and how to handle the result value from the API.
There are a few things to think about first, such as which API to call. Fortunately, various APIs are published on the internet.
This time, I would like to use the OpenWeatherMap API. In OpenWeatherMap, for example, the following APIs for data acquisition are prepared:
For more information, please see the official website of OpenWeatherMap: https://openweathermap.org/.
Okay, let's prepare to use the OpenWeatherMap API.
To use the OpenWeatherMap API, we need to create an account. Please access the following URL: https://openweathermap.org/.
If you already have an account, please log in without taking the following steps.
For those who are using it for the first time, please click the Sign In button, and then click the Create an Account link. It is easy to register. Just follow the guidance and confirm the email sent to you by OpenWeatherMap after registration. This is what the creating an account page looks like:
Figure 7.3 – Creating an OpenWeatherMap account
Next, let's create an API key.
When you log in to OpenWeatherMap, you can see the API keys tab, so please click it. You already have a default API key but please create a specific API key for this tutorial. Enter any key string and click the Generate button.
Please note that the API keys shown in this book are created by me as a sample and cannot be used. Be sure to create a new API key in your account:
Figure 7.4 – Generating API key
Important note
After creating the API key, the key will not be activated for 10 minutes to a couple of hours. If a web response error such as 401 is returned even when you access the API endpoint URL described in the next section, the specified API key may not have been activated, so please wait and try again.
To check your API endpoint URL, follow these steps:
Figure 7.5 – Opening API doc of Current Weather Data
API doc, city name, state code, and country code are from ISO 3166. The URLs under the API call area are endpoint URLs for using this API. Please copy this URL to the clipboard:
Figure 7.6 – API endpoint URL with parameters
Next, let's see whether we can run this API or not.
Let's try to use this API. You just have to open your browser, paste the URL, and replace the city name and API key with yours. You can choose any city name, but the API key is the specific one you created in the previous section:
Figure 7.7 – Calling the API and getting the result
I have now confirmed that this API works correctly. Now let's call this API from Node-RED and use it.
Now let's create a flow that calls the OpenWeatherMap API on Node-RED. Start Node-RED in your environment. You can use either standalone Node-RED or Node-RED on IBM Cloud:
Figure 7.8 – The flow to use the API
For this, the flow is very simple and easy to make. Please follow these steps to make the flow:
Figure 7.9 – Setting the API endpoint URL with your parameters
Figure 7.10 – Checking the Action property
Figure 7.11 – Wiring all nodes
Please wire the timestamp node and the http request node. The http request node output is wired to the json node and the debug node. Lastly, please wire the json node output to another debug node.
Figure 7.12 – Result data (JSON) on the debug window
You can also see the result data as a JSON object on the same debug window as in the following screenshot:
Figure 7.13 – Result data (object) on the debug window
Congratulations! You have succeeded in making a sample flow by calling the OpenWeatherMap API. If you didn't succeed in making this flow completely, you can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter07/open-weather-flows.json.
In the next section, we will learn about the convenience of using the IBM Watson API with Node-RED on IBM Cloud.
In the previous section, you learned how to call the API and handle the resulting values from the API.
In this section too, we will create a flow that actually calls the API from Node-RED, but we will learn how to call the Watson API provided by IBM. We will also create a flow that actually calls the API from Node-RED, but we will learn how to call the Watson API provided by IBM.
Why Watson? Watson is a brand of artificial intelligence services and APIs provided by IBM.
All Watson APIs can be used from IBM Cloud. So, by running Node-RED on IBM Cloud, you can effectively use Watson's services. This has advantages such as when calling the Watson API from Node-RED, implementation of authentication can be omitted.
Watson can be called from environments other than IBM Cloud, so it can be called directly from a Raspberry Pi or can be used from either cloud platforms such as AWS and Azure or on-premises environments. See the following figure, showing what a Watson API looks like:
Figure 7.14 – Watson API diagram
For more information, see the IBM Watson official website: https://www.ibm.com/watson.
Okay, let's see how easy it is to use the Watson API on Node-RED on IBM Cloud.
If you've followed the steps from the first chapter, you should already have an IBM Cloud account. Just log in to IBM Cloud (https://cloud.ibm.com).
If you do not have an IBM Cloud account, create one from the following URL and log in to IBM Cloud. See Chapter 6, Implementing Node-RED in the Cloud, for detailed instructions: http://ibm.biz/packt-nodered.
In the previous section, we created a sample flow using standalone Node-RED or Node-RED on IBM Cloud. Of course, you can use the standalone version of Node-RED to call the Watson API, but some benefits will be lost. So, we will work with Node-RED on IBM Cloud in this part.
As in the previous step, if you have not used Node-RED on IBM Cloud yet, please return to Chapter 6, Implementing Node-RED in the Cloud, and run through it to activate Node-RED on IBM Cloud before moving on to the next step.
Next, create Watson's service on IBM Cloud. Strictly speaking, this means creating an instance as a service so that you can call the Watson API service provided on IBM Cloud as your own API.
Watson has several APIs, such as voice recognition, image recognition, natural language analysis, sentiment analysis, and so on. This time, I would like to use the sentiment analysis API.
Follow these steps to create a Watson Tone Analyzer API service:
Figure 7.15 – Searching Watson services
a. Region: Dallas (you can select any region, but Dallas is recommended)
b. Pricing plan: Lite (free pricing)
c. Service name: Default (you can modify this to any name you want to use)
d. Resource group: Default (you can't select anything else for a Lite account)
e. Tags: N/A
Figure 7.16 – Creating a Tone Analyzer service
You can check the API key and URL from the Manage menu on this screen:
Figure 7.17 – Checking your credentials
In the next section, we will connect Node-RED and the Tone Analyzer service.
As you already know, Node-RED can call the Watson API without coding for authentication. We need to connect Node-RED and the Watson API instance before using Node-RED with the Watson API. In the last step, we created the Tone Analyzer API instance, so let's connect these two instances as follows:
Figure 7.18 – Selecting the Node-RED service you created
Figure 7.19 – Creating a connection for Node-RED and Watson
Figure 7.20 – Clicking the Next button to select the connecting service
Figure 7.21 – Clicking the Connect button to complete the connection
Figure 7.22 – Clicking the Restage button to start restaging the Node-RED service
Figure 7.23 – Checking the status of the Node.js runtime for the Node-RED service
You have succeeded in preparing the Node-RED and Watson API flow. Next, let's create the flow by calling the Tone Analyzer API.
Now, let's create a flow that calls the Watson Tone Analyzer API on Node-RED. You already started Node-RED on IBM Cloud. Either standalone Node-RED or Node-RED on IBM Cloud can be used.
To proceed with this tutorial, you need to install the following two nodes:
Figure 7.24 – Installing node-red-node-twitter
Figure 7.25 – Installing node-red-node-sentiment
Search for these nodes in the palette and install them to your Node-RED flow editor. After that, make a flow as shown in the following figure:
Figure 7.26 – The flow to use the Tone Analyzer API
In this flow, the function node processes the text, tone, and sentiment included in the result value obtained from Twitter so that they are output as separate debugs. This is to make the results easier to see.
This flow is a little more complicated than the flow you created in the previous step. Please follow these steps to make the flow:
Figure 7.27 – Creating an app on Twitter Developer
Figure 7.28 – Setting a name of your app
You will see the tokens. Please note and save your access token and access token secret. These will be used for the setting of the twitter in node too:
Figure 7.29 – Note your token and token secret
Figure 7.30 – Placing the twitter in node
Figure 7.31 – Editing the Twitter properties
The values for API key, API key secret, Access token, and Access token secret should be taken from your text editor from step 8.
Figure 7.32 – Configuring your Twitter information
Figure 7.33 – Finalizing the settings of the twitter in node
For this node, no properties are needed to be set or changed:
Figure 7.34 – Placing the sentiment node
Figure 7.35 – Placing the tone analyzer v3 node
a. Name: Any string you want to name
b. Method: General Tone
c. version_date: Multiple Tones
d. Tones: All
e. Sentences: True
f. Content type: Text:
Figure 7.36 – Configuring the tone analyzer v3 node properties
Figure 7.37 – Placing the function node
msg.payload = {
"text" : msg.payload,
"tone" : msg.response,
"sentiment" : msg.sentiment
};
return msg;
Please refer to the following screenshot for the coding for the function node:
Figure 7.38 – JavaScript source code for the function node
You can get the code here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter07/format-payload.js.
The flow is now complete. When you click the deploy button and the twitter in node connects to Twitter using your account, it will automatically retrieve the tweets that meet your criteria and process the subsequent flow.
This is done automatically, so you don't have to take any special action.
Here, it is set to get all tweets that have #nodered as a hashtag. If you don't get many tweets, it means that a tweet that contains the specified hashtag has not been created, so please change the hashtag set in the twitter in node and try again.
All the processing results of this flow will be displayed in the debug tab.
It is msg.payload.text that extracts the tweet body from the acquired tweets and displays it:
Figure 7.39 – Result of getting the tweet body
It is msg.payload.tone that extracts and displays emotions detected in the acquired tweets:
Figure 7.40 – Result of tone analysis from the tweet
It is msg.payload.sentiment that judges whether the sentiment is positive or negative in the acquired tweets:
Figure 7.41 – Result of the sentiment of a tweet
Congratulations! You have succeeded in making a sample flow by calling the Watson API. If you didn't succeed in making this flow completely, you can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter07/get-sentiment-twitter-flows.json.
In this chapter, we learned how to create a sample flow (application) that calls two types of web APIs. We are gradually getting used to creating complicated flows. Use cases for calling web APIs are frequently found in Node-RED. The flow creation methods we learned about here will help us to create more complex flows in the future.
In the next chapter, let's learn about a project feature that can be integrated with repositories such as GitHub, which is a function added from Node-RED version 1.0.