For the complete documentation index, see llms.txt. This page is also available as Markdown.

3.3 Golang Examples

Go code examples for interacting with the JSONAir API.

The following examples use only the Go standard library. No third-party HTTP client is required.


Authentication — Get a Bearer Token

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

type tokenResponse struct {
    AccessToken string `json:"access_token"`
    ExpiresIn   int    `json:"expires_in"`
}

func getToken(baseURL, pat string) (string, error) {
    body, err := json.Marshal(map[string]string{"token": pat})
    if err != nil {
        return "", err
    }

    resp, err := http.Post(
        baseURL+"/api/v1/jsonair/auth/token",
        "application/json",
        bytes.NewReader(body),
    )
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()

    if resp.StatusCode != http.StatusOK {
        return "", fmt.Errorf("auth failed: HTTP %d", resp.StatusCode)
    }

    var tr tokenResponse
    if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
        return "", err
    }

    return tr.AccessToken, nil
}

Fetch Configuration Data


Putting It Together — Poll with Re-Authentication

This pattern mirrors what the JSONAir agent does: authenticate once, poll on an interval, and re-authenticate automatically when the JWT expires.


Fetch the Reload Key

Last updated