Quick setup examples

Go (HTTP) Quickstart

Call the Unison brain from Go using the standard net/http package — no third-party dependencies.

Unison exposes a plain REST API, so net/http and encoding/json from the standard library are all you need.

Prerequisites

export UNISON_TOKEN=usk_live_...

Go 1.21 or later recommended.

Recall — fetch prompt-ready context

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
)

const base = "https://brain.unisonlabs.ai"

type ContextResponse struct {
	WeakEvidence bool   `json:"weakEvidence"`
	ContextMd    string `json:"contextMd"`
}

func recall(query string) (string, error) {
	params := url.Values{"q": {query}, "k": {"5"}, "mode": {"auto"}}
	req, _ := http.NewRequest("GET", base+"/v1/brain/context?"+params.Encode(), nil)
	req.Header.Set("Authorization", "Bearer "+os.Getenv("UNISON_TOKEN"))

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	var result ContextResponse
	body, _ := io.ReadAll(resp.Body)
	json.Unmarshal(body, &result)

	if result.WeakEvidence {
		return "", nil
	}
	return result.ContextMd, nil
}

func main() {
	memory, err := recall("payment service architecture")
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(memory)
}

Persist — write memory to the brain

package main

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

func persist() error {
	payload := map[string]any{
		"items": []map[string]any{
			{
				"type": "conversation",
				"turns": []map[string]string{
					{"role": "user",      "content": "What queue library should we use?"},
					{"role": "assistant", "content": "Switched to pgmq — simpler ops than Redis."},
				},
				"sourceRef":  "session-1",
				"visibility": "private",
			},
		},
	}

	body, _ := json.Marshal(payload)
	req, _ := http.NewRequest("POST", base+"/v1/brain/ingest", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("UNISON_TOKEN"))
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	var result map[string]any
	json.NewDecoder(resp.Body).Decode(&result)
	fmt.Printf("jobId: %v\n", result["items"].([]any)[0].(map[string]any)["jobId"])
	return nil
}

On this page