August 24, 2023

Adding 3rd-Party API Data into Viam

Written by
Saul N. Jinnear
Consulting Engineer

Viam’s data platform is super cool and useful for storing data generated by your robot. But, there are times where it could be helpful to store some data from external sources alongside that generated by your robot.

For example, I’ve got a plant monitoring bot that I’ve built and I’ve got loads of data from the sensors surrounding my plant. I do not, however, have weather data to go with it. I know I can get weather data from weatherapi.com. But how can I get that store alongside the data from my robot?

Stick with me kid! We’ll get this thing going in no time!!!

Adding external data to Viam’s data platform is nice and easy. All we’ve got to do is wrap the API calls we want to make in a simple modular sensor component.

The Process

I started out by making a very small python script that simply queries weatherapi.com for the current weather.

With the logic refined and functioning, I set out to create a modular sensor. How could I do this with as little effort as possible? I just copied the simple module example from https://github.com/viamrobotics/viam-python-sdk/tree/main/examples/simple_module. Then I modified the class name and model to make this a sensor of my own.

Then to make this sensor function as an API-querier, I moved the logic from my initial script into the get_readings function. Sweet! Now this component can query that API and return the data via get_readings().

The data service can now be configured to query this endpoint at whatever cadence I see fit. Almost too easy!

I wanted to commit this to the repo where others could find and use it, so I moved the api_key from being hardcoded (as it was in my original script) into an attribute for the component. I did similar work to move the other query params (in this case “zipcode”) out to a config attribute as well.

What an exciting adventure we’ve just gone on together!!

Final code


Get started with Viam today!