Documentation

Python client library

Use InfluxDB v3 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 Serverless cluster.

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

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

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
    
  2. InfluxDB Cloud Serverless region URL using the HTTPS protocol–for example: https://cloud2.influxdata.com.

  3. InfluxDB organization ID.

  4. Name of the bucket to write to.

  5. InfluxDB API token with permission to write to the bucket. 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 Serverless bucket.

  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
    
  3. Define variables for your bucket name, organization, and token.

    bucket = "BUCKET_NAME"
    org = "INFLUX_ORG"
    # INFLUX_TOKEN is an environment variable you created for your API WRITE token
    token = os.getenv('INFLUX_TOKEN')
    url="https://cloud2.influxdata.com"
    
  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
    )
    

    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)
    
  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=bucket, org=org, record=p)
    

Complete example write script

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

bucket = "BUCKET_NAME"
org = "INFLUX_ORG"
# INFLUX_TOKEN is an environment variable you created for your API WRITE token
token = os.getenv('INFLUX_TOKEN')
url="https://cloud2.influxdata.com"

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=bucket, org=org, record=p)

Query data from InfluxDB with Python

To query your InfluxDB Cloud Serverless bucket, use the Python client library for InfluxDB v3.


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 v3 enhancements and InfluxDB Clustered is now generally available

New capabilities, including faster query performance and management tooling advance the InfluxDB v3 product line. InfluxDB Clustered is now generally available.

InfluxDB v3 performance and features

The InfluxDB v3 product line has seen significant enhancements in query performance and has made new management tooling available. These enhancements include an operational dashboard to monitor the health of your InfluxDB cluster, single sign-on (SSO) support in InfluxDB Cloud Dedicated, and new management APIs for tokens and databases.

Learn about the new v3 enhancements


InfluxDB Clustered general availability

InfluxDB Clustered is now generally available and gives you the power of InfluxDB v3 in your self-managed stack.

Talk to us about InfluxDB Clustered

InfluxDB Cloud Serverless