InfluxDB Docs

Python client library

Use the InfluxDB Python client library to integrate InfluxDB into Python scripts and applications.

This guide presumes some familiarity with Python and InfluxDB. If just getting started, see Getting started with InfluxDB.

Before you begin

  1. Install the InfluxDB Python library:

    pip install influxdb-client
  2. Ensure that InfluxDB is running. If running InfluxDB locally, visit http://localhost:9999. (If using InfluxDB Cloud, visit the URL of your InfluxDB Cloud UI. For example: https://us-west-2-1.aws.cloud2.influxdata.com.)

Write data to InfluxDB with Python

We are going to write some data in line protocol using the Python library.

In your Python program, import the InfluxDB client library and use it to write data to InfluxDB.

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS

Next, we define a few variables with the name of your bucket, organization, and token.

bucket = "<my-bucket>"
org = "<my-org>"
token = "<my-token>"

In order to write data, we need to create a few objects: a client, and a writer. The InfluxDBClient object takes three named parameters: url, org, and token. Here, we simply pass the three variables we have already defined.

client = InfluxDBClient(
    url="http://localhost:9999",
    token=token,
    org=org
)

The InfluxDBClient object has a write_api method, used for configuration. Instantiate a writer object using the client object and the write_api method. Use the write_api method to configure the writer object.

write_api = client.write_api(write_options=SYNCHRONOUS)

We need two more lines for our program to write data. Create a point object and write it to InfluxDB using the write method of the API writer object. The write method requires three parameters: bucket, org, and record.

p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api.write(bucket=bucket, org=org, record=p)

For more information, see the Python client README on GitHub.

Complete example write script

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS

bucket = "<my-bucket>"
org = "<my-org>"
token = "<my-token>"

client = influxdb_client.InfluxDBClient(
    url="http://localhost:9999",
    token=token,
    org=org
)

write_api = client.write_api(write_options=SYNCHRONOUS)

p = influxdb_client.Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
write_api.write(bucket=bucket, org=org, record=p)

New! Cloud or OSS?