This example project provides a Python REST API server that interacts with the InfluxDB v2 HTTP API.
The project uses the Flask framework and the InfluxDB v2 API client library for Python to demonstrate how to build an app that collects, stores, and queries IoT device data.
After you have set up and run your iot-api-python
API server, you can consume the API using the iot-api-ui standalone React frontend.
This application demonstrates how you can use InfluxDB client libraries to do the following:
- Create and manage InfluxDB authorizations (API tokens and permissions).
- Write and query device metadata in InfluxDB.
- Write and query telemetry data in InfluxDB.
- Generate data visualizations with the InfluxDB Giraffe library.
To learn how to build this app from scratch, follow the InfluxDB v2 OSS tutorial or InfluxDB Cloud tutorial. The app is an adaptation of InfluxData IoT Center, simplified to accompany the IoT Starter tutorial.
For help, refer to the tutorials and InfluxDB documentation or use the following resources:
To report a problem, submit an issue to this repo or to the influxdata/docs-v2
repo.
Follow the tutorial instructions to setup your InfluxDB organization, API token, and buckets:
Next, clone and run the API server.
-
Clone this repository to your machine.
-
Change to the directory--for example, enter the following command in your terminal:
cd ./iot-api-python
-
Set environment variables for
INFLUX_TOKEN
andINFLUX_ORG
--for example, enter the following commands in your terminal:export INFLUX_TOKEN=<INFLUX_TOKEN> export INFLUX_ORG=<INFLUX_ORG_ID>
Replace the following:
<INFLUX_TOKEN>
: your InfluxDB API token with permission to query (read) buckets and create (write) authorizations for devices.<INFLUX_ORG_ID>
: your InfluxDB organization ID.
-
If you need to adjust the default URL or bucket names to match your InfluxDB instance, edit the settings in
./config.ini
:[APP] INFLUX_URL = <INFLUX_URL> INFLUX_BUCKET = iot_center INFLUX_BUCKET_AUTH = iot_center_devices
Replace the following:
<INFLUX_URL>
: your InfluxDB instance URL--for example, the default OSS URLhttp://localhost:8086
.
-
Create and activate a Python virtual environment for the project. Enter the following commands into your terminal:
# Create a new virtual environment named "virtualenv" # Python 3.8+ python -m venv virtualenv # Activate the virtualenv (OS X & Linux) source virtualenv/bin/activate
-
Install
pdm
package manager for your system. On Linux or macOS, enter the following command in your terminal:curl -sSL https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py | python3 -
On Windows, enter the following command in your terminal:
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py -UseBasicParsing).Content | python -
-
Use
pdm
to install dependencies. Enter the following command in your terminal:pdm install
-
If you use a bash terminal, you can set an environment variable to use dependencies in
pdm.lock
when you run your application scripts--for example, enter the following command in your bash terminal:eval "$(pdm --pep582)"
-
To start the Flask application, enter the following command in your terminal:
flask run -h localhost -p 3001
To view the application, visit http://localhost:3001 in your browser.
-
Optional: Run the iot-api-ui React frontend to interact with your IoT Starter API server.
{"error":"failed to load data: HttpError: failed to initialize execute state: could not find bucket \"iot_center_devices\""}
Solution: create buckets or adjust the defaults in config.ini
to match your InfluxDB instance.