Documentation

Python client library

Use InfluxDB 3 clients

The /api/v2/query API endpoint and associated tooling, such as InfluxDB v2 client libraries and the influx CLI, can’t query an InfluxDB Cloud Dedicated cluster.

InfluxDB 3 client libraries and Flight SQL clients are available that integrate with your code to write and query data stored in InfluxDB Cloud Dedicated.

InfluxDB 3 supports many different tools for writing and querying data. Compare tools you can use to interact with InfluxDB Cloud Dedicated.

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 Get started with InfluxDB.

Before you begin

You’ll need the following prerequisites:

  1. Install the InfluxDB Python library:

    pip install influxdb-client
    
    • Copy
    • Fill window
  2. InfluxDB Cloud Dedicated cluster URL using the HTTPS protocol–for example:

    https://cluster-id.a.influxdb.io
    
    • Copy
    • Fill window
  3. Name of the database to write to.

  4. InfluxDB database token with permission to write to the database. For security reasons, we recommend setting an environment variable to store your token and avoid exposing the raw token value in your script.

Write data to InfluxDB with Python

Follow the steps to write line protocol data to an InfluxDB Cloud Dedicated database.

  1. In your editor, create a file for your Python program–for example: write.py.

  2. In the file, import the InfluxDB client library.

    import influxdb_client
    from influxdb_client.client.write_api import SYNCHRONOUS
    import os
    
    • Copy
    • Fill window
  3. Define variables for your database name, organization (required, but ignored), and database token.

    database = "DATABASE_NAME"
    org = "ignored"
    # INFLUX_TOKEN is an environment variable you created for your database WRITE token
    token = os.getenv('INFLUX_TOKEN')
    url="https://cluster-id.a.influxdb.io"
    
    • Copy
    • Fill window
  4. To instantiate the client, call the influxdb_client.InfluxDBClient() method with the following keyword arguments: url, org, and token.

    client = influxdb_client.InfluxDBClient(
       url=url,
       token=token,
       org=org
    )
    
    • Copy
    • Fill window

    The InfluxDBClient object has a write_api method used for configuration.

  5. Instantiate a write client by calling the client.write_api() method with write configuration options.

    write_api = client.write_api(write_options=SYNCHRONOUS)
    
    • Copy
    • Fill window
  6. 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=database, org=org, record=p)
    
    • Copy
    • Fill window

Complete example write script

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
import os

database = "DATABASE_NAME"
org = "ignored"
# INFLUX_TOKEN is an environment variable you created for your database WRITE token
token = os.getenv('INFLUX_TOKEN')
url="https://cluster-id.a.influxdb.io"

client = influxdb_client.InfluxDBClient(
    url=url,
    token=token,
    org=org
)

# Write script
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=database, org=org, record=p)
  • Copy
  • Fill window

Query data from InfluxDB with Python

The InfluxDB v2 Python client can’t query InfluxDB Cloud Dedicated. To query your dedicated instance, use a Python Flight SQL client with gRPC.


Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: