Connecting via Golang works with any golang PostgreSQL library; in this example we use pgx.

go get

go build

Grab your API key:

  • Log into
  • Click the green "Connect" button in the upper left
  • Click on your API key to copy it

BITDOTIO_APIKEY=<your api key> ./bitdotio-golang-example

package main

import (


func main() {
    apiKey, ok := os.LookupEnv("BITDOTIO_APIKEY")
    if !ok {
        panic("BITDOTIO_APIKEY not set in environment")
    userName := "adam"
    repoName := "sensors"
    connectUrl := fmt.Sprintf("postgres://%s:%[email protected]:5432/%s", userName, apiKey, repoName)
    conn, err := pgx.Connect(context.Background(), connectUrl)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
    defer conn.Close(context.Background())

    fullRepoName := fmt.Sprintf(`"%s/%s"`, userName, repoName)
    // Let's see when we have bad AQI in San Francisco. 
    sqlQuery := fmt.Sprintf(`SELECT datetime, pm_10 FROM %s.measurements WHERE pm_10 >= 20.0 AND pm_10 <= 1000 ORDER BY datetime;`, fullRepoName)
    rows, err := conn.Query(context.Background(), sqlQuery)

    if err != nil {
        fmt.Println("query error")

    defer rows.Close()

    for rows.Next() {
        var datetime time.Time
        var pm_10 float32

        err = rows.Scan(&datetime, &pm_10)

        if err != nil {
        fmt.Println(datetime, pm_10)

    err = rows.Err()
    if err != nil {

You can always see the latest version of this code at

Did this page help you?