This example project provides a Node.js REST API server that interacts with the InfluxDB v2 HTTP API.
The project uses the Next.js framework and the InfluxDB v2 API client library for JavaScript 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-js
API server, you can consume your 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-js
-
Add a
.env.local
file that contains the following configuration variables:# Local environment secrets INFLUX_TOKEN=INFLUXDB_ALL_ACCESS_TOKEN INFLUX_ORG=INFLUXDB_ORG_ID
Replace the following:
INFLUXDB_ALL_ACCESS_TOKEN
with your InfluxDB All Access token.INFLUXDB_ORG_ID
with your InfluxDB organization ID.
-
If you need to adjust the default URL or bucket names to match your InfluxDB instance, edit the settings in
.env.development
or set them in.env.local
(to override.env.development
). -
If you haven't already, follow the Node.js installation instructions to install
node
for your operating system. -
To check the installed
node
version, enter the following command in your terminal:node --version
-
Follow the Yarn installation instructions to install the
yarn
package manager for your version of Node.js. -
To check the installed
yarn
version, enter the following command in your terminal:yarn --version
-
Run
yarn
to install the project dependencies:yarn
-
In your terminal, enter the following command to start the application in development mode:
yarn dev -p 5200
The application server starts with the following output:
> dev > next dev ready - started server on 0.0.0.0:5200, url: http://localhost:5200
-
In your browser, visit http://localhost:5200/api/devices to view the API server output.
-
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 .env.development
to match your InfluxDB instance.
To learn more about Next.js, see following resources:
- Next.js Documentation - learn about Next.js features and API.
- Learn Next.js - an interactive Next.js tutorial.