InfluxDB and C#
Download full source code.
I recently started playing with InfluxDB, and as with many of these things, it took a little longer to get going than expected. So, to save the next person some time, here is how to do it.
Docker Setup
It starts with Docker. I love Docker for this kind of thing. Get the image, start it, use it, stop it when done, and delete it later. No need to install it to the operating system. No chance it is going to leave anything behind when uninstalled. It is also easy to run multiple versions of an application.
Here is the single-line command to run InfluxDB in a Docker container -
docker run --name influxdb --publish 8086:8086 --mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2 --mount type=volume,source=influxdb2-config,target=/etc/influxdb2 --env DOCKER_INFLUXDB_INIT_MODE=setup --env DOCKER_INFLUXDB_INIT_USERNAME=admin --env DOCKER_INFLUXDB_INIT_PASSWORD=password --env DOCKER_INFLUXDB_INIT_ORG=bryan_org --env DOCKER_INFLUXDB_INIT_BUCKET=test_bucket influxdb
But let’s break it down a bit -
docker run --name influxdb --publish 8086:8086 \
--mount type=volume,source=influxdb2-data,target=/var/lib/influxdb2 \
--mount type=volume,source=influxdb2-config,target=/etc/influxdb2 \
--env DOCKER_INFLUXDB_INIT_MODE=setup \
--env DOCKER_INFLUXDB_INIT_USERNAME=admin \
--env DOCKER_INFLUXDB_INIT_PASSWORD=password \
--env DOCKER_INFLUXDB_INIT_ORG=bryan_org \
--env DOCKER_INFLUXDB_INIT_BUCKET=test_bucket \
influxdb
- lines 1 to 3 configure the port, and volumes the container will use
- lines 4 to 8 set up the container and set environment variables to create an admin account and the org and bucket you will use in the C# application
Run that command.
Getting a token to access Influx from C#
To give a C# application access to Influx, it needs to send a token with its requests.
Go to a browser and open https://localhost:8086.
Log in with admin/password
(you set these in the Docker command).
On the left menu, click Load Data -> API Tokens -> Generate API Token -> Custom API Token.

I tried the All Access
token, but it didn’t work for me from inside the C# application.
In the new window, give the token a name, assign it to the test_bucket
with read/write
access, and click Generate.

Note this token, you won’t see it again.

The C# Application
You are going to use the token you just generated, along with the bucket and org names from the Docker command.
Create a console application and add the InfluxDB.Client
NuGet package.
Replace the Program.cs
with the following -
1using InfluxDB.Client;
2using InfluxDB.Client.Api.Domain;
3using InfluxDB.Client.Writes;
4using System.Diagnostics;
5
6Random rnd = new Random();
7// don't put your token in your source code
8string token = "Pc2F929JWJ-soUAHUmJH5d2KyS3I7Yl94QgFn4S3279bySVW4iqt9QQEDifF6sB1OxBv1dEUuPy0e6IrkMZiMA==";
9string orgName = "bryan_org";
10string bucketName = "test_bucket";
11
12var client = new InfluxDBClient("http://localhost:8086", token);
13
14// wipe everything from the past week
15var deleteApi = client.GetDeleteApi();
16await deleteApi.Delete(DateTime.UtcNow.AddDays(-7), DateTime.UtcNow, "", bucketName, orgName);
17
18// set up some cities and temperatures
19string[] cities = ["Boston", "Hartford", "Portsmouth", "Portland"];
20double[] temperatures = [70, 80, 65, 60];
21
22// start a hour ago so we can see some data in the UI
23var startTime = DateTime.UtcNow.AddHours(-1);
24
25var writeApi = client.GetWriteApi();
26
27// add some data for the past hour
28for (int loop = 1; loop <= 3600; loop++)
29{
30 var pointData = PointData.Measurement("City_Temperature")
31 .Tag("City", cities[loop % cities.Length])
32 .Field("Temperature", temperatures[loop % temperatures.Length] + rnd.NextDouble())
33 .Timestamp(startTime.AddSeconds(loop), WritePrecision.Ms);
34
35 writeApi.WritePoint(pointData, bucketName, orgName);
36}
37
38Console.WriteLine("Flushing...");
39writeApi.Flush();
40await Task.Delay(2000); // this is important, calling Flush() without waiting will result in incomplete data being saved, it probably shouldn't but it does!
41Console.WriteLine("Flushed");
There are a few things in the code I want to point out -
- Line 8, the token generated above, DON’T DO THIS in a real app
- Lines 15, 16, delete old data for the sake of clarity
- Line 23, all entries in Influx need a timestamp, here I start the timestamp one hour in the past
- Lines 28 - 36, write an hour’s worth of temperature data for four cities starting one hour ago
- Lines 39, 40 flush the data explicitly, and give it some time to flush
Run the application.
It will generate an hour’s worth of data, one entry every second.
Go to the Data Explorer in the InfluxDB UI, select the test_bucket
bucket, City_Temperature
measurement, choose one or more from the City
tag, and click the Submit
button.

That’s it for now. This should help you get going with InfluxDB and C#.
Download full source code.