Skip to content

Go Integration

The Go SDK (github.com/hybridb/sdk-go) is coming soon. Until then, use the REST API directly.

Setup

bash
go get github.com/go-resty/resty/v2

Client wrapper

go
// stellrai/client.go
package stellrai

import (
    "errors"
    "fmt"
    "os"

    "github.com/go-resty/resty/v2"
)

var (
    baseURL = getEnv("HDB_BASE_URL", "https://hybridb.stellrai.com")
    apiKey  = mustEnv("HDB_API_KEY")
    client  = newClient()
)

func newClient() *resty.Client {
    return resty.New().
        SetBaseURL(baseURL).
        SetHeader("Authorization", "Bearer "+apiKey).
        SetHeader("Content-Type", "application/json")
}

func getEnv(key, fallback string) string {
    if v := os.Getenv(key); v != "" {
        return v
    }
    return fallback
}

func mustEnv(key string) string {
    v := os.Getenv(key)
    if v == "" {
        panic(fmt.Sprintf("environment variable %s is required", key))
    }
    return v
}

// --- Types ---

type DecisionResponse struct {
    DecisionID   string   `json:"decisionId"`
    Outcome      string   `json:"outcome"`
    Score        *float64 `json:"score"`
    Rationale    *string  `json:"rationale"`
    RulesApplied []string `json:"rulesApplied"`
    ExecutionMs  *int     `json:"executionMs"`
}

type ExecutionResponse struct {
    ID          string `json:"id"`
    PipelineID  string `json:"pipelineId"`
    Status      string `json:"status"`
    StartedAt   string `json:"startedAt"`
    CompletedAt string `json:"completedAt"`
}

type apiEnvelope[T any] struct {
    Success bool   `json:"success"`
    Data    T      `json:"data"`
    Error   *struct {
        Code    string `json:"code"`
        Message string `json:"message"`
    } `json:"error"`
}

func checkEnvelope[T any](env apiEnvelope[T]) error {
    if !env.Success && env.Error != nil {
        return fmt.Errorf("[%s] %s", env.Error.Code, env.Error.Message)
    }
    return nil
}

// --- Methods ---

func RequestDecision(decisionType, action string, inputData map[string]any, idempotencyKey string) (*DecisionResponse, error) {
    body := map[string]any{
        "decisionType": decisionType,
        "action":       action,
        "inputData":    inputData,
    }
    if idempotencyKey != "" {
        body["idempotencyKey"] = idempotencyKey
    }

    var env apiEnvelope[DecisionResponse]
    _, err := client.R().SetBody(body).SetResult(&env).Post("/api/v1/decisions")
    if err != nil {
        return nil, err
    }
    if err := checkEnvelope(env); err != nil {
        return nil, err
    }
    return &env.Data, nil
}

func TriggerPipeline(name string, input map[string]any, decisionID, idempotencyKey string) (*ExecutionResponse, error) {
    body := map[string]any{"input": input}
    if decisionID != "" {
        body["decisionId"] = decisionID
    }
    if idempotencyKey != "" {
        body["idempotencyKey"] = idempotencyKey
    }

    var env apiEnvelope[ExecutionResponse]
    _, err := client.R().SetBody(body).SetResult(&env).Post("/api/v1/pipelines/" + name + "/execute")
    if err != nil {
        return nil, err
    }
    if err := checkEnvelope(env); err != nil {
        return nil, err
    }
    return &env.Data, nil
}

func Rollback(executionID, reason string) error {
    var env apiEnvelope[map[string]any]
    _, err := client.R().
        SetBody(map[string]string{"reason": reason}).
        SetResult(&env).
        Post("/api/v1/executions/" + executionID + "/rollback")
    if err != nil {
        return err
    }
    return checkEnvelope(env)
}

Usage

go
// payment/service.go
package payment

import (
    "fmt"
    "your-module/stellrai"
)

func ProcessPayment(userID string, amount int, orderID string) error {
    // 1. Request decision
    decision, err := stellrai.RequestDecision(
        "payment.authorize",
        "payment.initiate",
        map[string]any{
            "actorId": "user:" + userID,
            "amount":  amount,
        },
        "decision-"+orderID,
    )
    if err != nil {
        return fmt.Errorf("decision request failed: %w", err)
    }

    if decision.Outcome == "blocked" {
        return fmt.Errorf("payment blocked by policy")
    }

    // 2. Execute pipeline
    execution, err := stellrai.TriggerPipeline(
        "payment.execute.standard",
        map[string]any{"amount": amount},
        decision.DecisionID,
        "exec-"+orderID,
    )
    if err != nil {
        return fmt.Errorf("pipeline execution failed: %w", err)
    }

    fmt.Printf("Execution complete: %s\n", execution.ID)
    return nil
}

Go SDK coming soon

A native sdk-go module with context support, structured errors, and full type coverage is in development. This page will be updated when it ships.

hybriDB is the kernel inside Stellrai.