OriginChainDB docs
sdks · go

Go SDK

No third-party dependencies, a context on every call, and a client that is safe to share across goroutines. Covers the query surfaces; the schema and row helpers are not wrapped yet.

Install

go get github.com/originchain-ai/originchain-go

The module depends only on the standard library.

Connect

Build one client and share it. It is safe for concurrent use, reuses connections through the underlying transport, and has no Close method because it holds nothing exclusive. The default HTTP client times out after 30 seconds; supply your own to change that.

import (
    "context"
    "os"

    "github.com/originchain-ai/originchain-go"
)

ctx := context.Background()
db := originchain.NewClient(originchain.Config{
    BaseURL: os.Getenv("OC_BASE_URL"),
    Bearer:  os.Getenv("OC_BEARER"),
})
FieldMeaning
BaseURLengine endpoint - required, and the constructor panics without it
Bearerbearer token
Tenantoptional - derived from the endpoint hostname when empty
HTTPsupply your own client; the default has a 30 second timeout
the panic is deliberate

NewClient panics on an empty BaseURL rather than returning an error, because a client with no endpoint can never do useful work and the mistake is always a wiring bug at startup, not a runtime condition.

Query

SQL

resp, err := db.SQL(ctx,
    "SELECT name, price FROM shop.products WHERE price > $1", 500)
if err != nil {
    return err
}
for _, row := range resp.Rows {
    fmt.Println(row["name"], row["price"])
}

row, err := db.SQLOne(ctx, "SELECT count(*) AS n FROM shop.products")

Full-text

err := db.FTSIndex(ctx, "shop.products", "name", originchain.FTSIndexRequest{
    PK:   "sku-1",
    Text: "Aeron chair",
})

hits, err := db.FTSSearch(ctx, "shop.products", "name", originchain.FTSSearchRequest{
    Q:     "chair",
    Limit: 10,
})

Vector

err := db.VectorPut(ctx, "shop.products", originchain.VectorPutRequest{
    PK:     "sku-1",
    Vector: embedding,
})

near, err := db.VectorTopK(ctx, "shop.products", originchain.VectorTopKRequest{
    Vector: query,
    K:      10,
})

Graph

Traversals hang off db.Graph():

n, err := db.Graph().Neighbors(ctx, "shop", originchain.NeighborsRequest{
    Rel: "bought_with",
    PK:  "sku-1",
})

p, err := db.Graph().Dijkstra(ctx, "shop", originchain.DijkstraRequest{
    Rel:  "ships_to",
    From: "a",
    To:   "b",
})

Ask and usage

ans, err := db.Ask(ctx, "which products sold best last week?")
u, err := db.Usage(ctx)

What this client does not wrap yet

Schema registration, the row helpers, vector delete, plan queries and the health check have no Go method today. They are ordinary HTTP calls - build them against the HTTP API reference with the same bearer, or use the Python client where the helper already exists.

Errors

Errors come back typed. Unwrap them the usual way:

var apiErr *originchain.APIError
if _, err := db.SQL(ctx, "SELECT 1"); errors.As(err, &apiErr) {
    if apiErr.Status == 429 {
        backOff()
    }
}
TypeRaised when
*APIErrorany non-2xx response - carries the status and the parsed body
*AddonRequiredErrorthe call needs an add-on the account does not have

Retries and idempotency

Every mutating method attaches a fresh UUIDv4 Idempotency-Key generated from crypto/rand, and the engine caches the result server-side. This client does not retry; your own retry policy takes over.

That is the safe arrangement: your loop can re-issue the same call and the write still lands once. Set the header yourself when the retry has to survive a restart.

Source

go get github.com/originchain-ai/originchain-go · originchain-ai/originchain-go · the HTTP API underneath