Enhance your IoT Application with External APIs

Thushara Sampath
5 min readSep 19, 2021

IoT solutions need external data and services to enhance their functionalities. The best way to obtain pre-collected/processed data, external services is APIs. For sake of simplicity, this tutorial is based on NODE-RED so that we can focus on the main topic instead of setting up a complex IoT environment.

In this tutorial, we will discuss,

  1. Get API Keys from open API services
  2. Use Dedicated Nodes to retrieve data from APIs
  3. Use HTTP nodes to get data APIs

prerequisites 📃

  1. An email account
  2. Basic Knowledge of Node-Red
  3. Working Node-Red server

You can find step by step guide to setting up Node-Red with a simple IoT application here 👉 Build Prototype IoT System With Node-Red, MQTT, and NodeMCU.

1. Get API Keys from open API services 🔑

API: Application Programming Interface enable applications to exchange data and functionality easily and securely. Simply we can request data we need and API sends back requested data or we can send data to process and API sends back processed data. Most of the case we use RESTful APIs which employ HTTP in communication.

Most of the time we need to have an API key to use an API service. It is our identity and we need to send our key every time we request data from API.

We have a number of free (there are limitations) APIs and paid premium APIs. In this tutorial, we will use free APIs. you can visit API Hub — Free Public & Open Rest APIs | RapidAPI to find the API that fits your needs.

Let’s try very useful free API called OpenWeather Map which can be used to get whether data of a specific location. Follow the steps mentioned below to get an API Key.

  1. Sign Up to OpenWeatherMap API.
  2. Sign In to your new account.
  3. Go to My API Keys.
  4. On that screen, give the name you preferred and click Generate.
type a name and click generate

5. Now you have an API key. it appears as follows: 4f26235c6af62472b595254bee8e780a6

The process of getting an API key is almost the same as this in other API services as well.

2. Use Pre-Build to retrieve data from APIs

Now we are going to implement a method to get data from API to our IoT System. For this, we can use a platform like Node-Red, Things Board or any platform which is connected to your system. As I said before, we are going to use Node-Red.

For popular APIs like OpenweatherMap, there are pre-made Nodes. We can use them to connect with APIs easily.

pre-made nodes

So how to install Pre-Build API nodes ❓

For Node-Red in Node-Fred Service,

follow these steps

For Node-Red server in Local machine or virtual machine,

open palette manager
search and install OpenWeatherMap nodes
confirm installation

Now you can find new nodes at the bottom of the node tray😎.

Next step is simple.

create this flow using OpenWeatherMap, inject, and debug nodes

Then follow the steps in the image below.

Here we have few selectable options. we can get the current weather or 5-day forecast of a location. Also, To select the location we can type city and country or we can give coordinates of the location. Select appropriate settings to fit your application.

Now deploy the flow. Then Trigger the Inject node to start the flow. Go to debug tab. If everything went well you should be able to see API response. Here you can find weather information of selected location.

Now we know how to use pre-build nodes to call an API. We have lots of free APIs which do not have pre-build nodes 😥. In that case, what can we do ❓

3. Use HTTP node to get data from APIs

Let’s consider Exchangeratesapi.io. This API can be used to get up-to-date currency data. As we discussed before making an account and get an API key. Create following flow using the trigger, HTTP request, debug nodes.

HTTP flow

Then double click HTTP node to open settings.

Set Method to GET and URL to http://api.exchangeratesapi.io/v1/latest?access_key=YOUR_API_KEY. Replace “YOUR_API_KEY” with your API key😂 which you obtain from Here. Deploy the flow. Once triggered, you should be able to see API response in the debug window.

API response with currency data

Keep in mind that almost every API has Documentation where they describe how to use their API. In the documentation, you can find URLs, HTTP configurations, etc. (Using those HTTP configurations you can customize API response.)

Conclusion

We can use these external APIs to build amazing IoT applications. For example, we use ExchangeRate API to build a cool gadget that is helpful to traders. It was a team project and my team is Anuradha Thishani, Harindu Ravin Jayarathne, and Dhanuka Marasinghe. Visit the GitHub repo for more information.

Overview of GetXchange

There are COVID-19 data APIs, GeoLocation APIs, Google APIs, and so on. Keep in mind that we need to be grateful to those free API services to their service and it is our responsibility to not overwhelm their servers with inappropriate high frequent continuous requesting. Also please note that there are paid APIs that have more capabilities out there; you can use them as well.

Thank you.

--

--

Thushara Sampath

Undergraduate | Electronic and Telecommunication Engineering at University of Moratuwa , Sri Lanka