---
title: InfluxDB v2 Go client library
description: The InfluxDB v2 Go client library integrates with Go applications to write data to an InfluxDB Cloud Serverless bucket.
url: https://docs.influxdata.com/influxdb3/cloud-serverless/reference/client-libraries/v2/go/
estimated_tokens: 3896
product: InfluxDB Cloud Serverless
version: cloud-serverless
---

# InfluxDB v2 Go 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 Serverless cluster.

[InfluxDB 3 client libraries](/influxdb3/cloud-serverless/client-libraries/v3/) are available that integrate with your code to write and query data stored in InfluxDB Cloud Serverless.

InfluxDB 3 supports many different tools for [**writing**](/influxdb3/cloud-serverless/write-data/) and [**querying**](/influxdb3/cloud-serverless/query-data/) data. [**Compare tools you can use**](/influxdb3/cloud-serverless/get-started/#tools-to-use) to interact with InfluxDB Cloud Serverless.

The [InfluxDB Go client library](https://github.com/influxdata/influxdb-client-go) integrates with Go applications to write data to an InfluxDB Cloud Serverless bucket.

This guide presumes some familiarity with Go and InfluxDB. If just getting started, see [Get started with InfluxDB](/influxdb3/cloud-serverless/get-started/).

## Before you begin

1. [Install Go 1.13 or later](https://golang.org/doc/install).
    
2. Add the client package your to your project dependencies.
    
    ```sh
    # Add InfluxDB Go client package to your project go.mod
    go get github.com/influxdata/influxdb-client-go/v2
    ```
    
3. Ensure that InfluxDB is running and you can connect to it. For information about what URL to use to connect to InfluxDB Cloud, see [InfluxDB URLs](/influxdb3/cloud-serverless/reference/urls/).
    

## Boilerplate for the InfluxDB Go Client Library

Use the Go library to write and query data from InfluxDB.

1. In your Go program, import the necessary packages and specify the entry point of your executable program.
    
    ```go
    package main
    
    import (
        "context"
        "fmt"
        "time"
    
        "github.com/influxdata/influxdb-client-go/v2"
    )
    ```
    
2. Define variables for your InfluxDB [bucket](/influxdb3/cloud-serverless/organizations/buckets/) and [token](/influxdb3/cloud-serverless/security/tokens/). For `org`, pass a non-empty string (this parameter is ignored by InfluxDB, but required by the client).
    
    ```go
    bucket := "BUCKET_NAME"
    org := "ignored"
    token := "API_TOKEN"
    // Store the URL of your InfluxDB instance
    url := "https://cloud2.influxdata.com"
    ```
    
3. Create the the InfluxDB Go client and pass in the `url` and `token` parameters.
    
    ```go
    client := influxdb2.NewClient(url, token)
    ```
    
4. Create a **write client** with the `WriteAPIBlocking` method and pass in the `org` and `bucket` parameters.
    
    ```go
    writeAPI := client.WriteAPIBlocking(org, bucket)
    ```
    

## Write data to InfluxDB with Go

Use the Go library to write data to InfluxDB.

1. Create a [point](/influxdb3/cloud-serverless/reference/glossary/#point) and write it to InfluxDB using the `WritePoint` method of the API writer struct.
    
2. Close the client to flush all pending writes and finish.
    
    ```go
    p := influxdb2.NewPoint("stat",
      map[string]string{"unit": "temperature"},
      map[string]interface{}{"avg": 24.5, "max": 45},
      time.Now())
    writeAPI.WritePoint(context.Background(), p)
    client.Close()
    ```
    

### Complete example write script

```go
package main

import (
      "context"
      "fmt"
      "time"

      "github.com/influxdata/influxdb-client-go/v2"
)

func main() {
    bucket := "BUCKET_NAME"
    org := "ignored"
    token := "API_TOKEN"
    // Store the URL of your InfluxDB instance
    url := "https://cloud2.influxdata.com"
    // Create new client with default option for server url authenticate by token
    client := influxdb2.NewClient(url, token)
    // User blocking write client for writes to desired bucket
    writeAPI := client.WriteAPIBlocking(org, bucket)
    // Create point using full params constructor
    p := influxdb2.NewPoint("stat",
        map[string]string{"unit": "temperature"},
        map[string]interface{}{"avg": 24.5, "max": 45},
        time.Now())
    // Write point immediately
    writeAPI.WritePoint(context.Background(), p)
    // Ensures background processes finishes
    client.Close()
}
```
