SDK Integration Examples

Plug bRRAIn into your stack

Reference integrations for the most common Go, Node.js, and Python frameworks. Copy, adapt, ship.

Web frameworks

Go

Gin

Inject a bRRAIn client into a Gin route group and use it to retrieve on every request.

r := gin.Default()
mem := sdk.NewClient(os.Getenv("BRRAIN_API_KEY"))

r.GET("/search", func(c *gin.Context) {
    results, _ := mem.Workspace("app").Retrieve(sdk.Query{
        Search: c.Query("q"),
        Limit:  20,
    })
    c.JSON(200, results)
})
Full Gin guide →
Go

Echo

Middleware that enriches requests with user-scoped workspace context.

e := echo.New()
mem := sdk.NewClient(os.Getenv("BRRAIN_API_KEY"))

e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
    return func(c echo.Context) error {
        uid := c.Request().Header.Get("X-User-ID")
        ws, _ := mem.Workspace("user:" + uid)
        c.Set("memory", ws)
        return next(c)
    }
})
Full Echo guide →
Go

net/http (stdlib)

Zero-dependency integration using only the standard library.

mux := http.NewServeMux()
mem := sdk.NewClient(os.Getenv("BRRAIN_API_KEY"))

mux.HandleFunc("/record", func(w http.ResponseWriter, r *http.Request) {
    var body map[string]any
    json.NewDecoder(r.Body).Decode(&body)
    id, _ := mem.Workspace("app").Store(body)
    fmt.Fprintf(w, "%s", id)
})
Full stdlib guide →
Node.js

Express

Attach a Node SDK client to req via middleware.

import express from 'express';
import { BRRAInClient } from '@brrain/sdk';

const app = express();
const mem = new BRRAInClient(process.env.BRRAIN_API_KEY);

app.use((req, _res, next) => {
  req.memory = mem.workspace('app');
  next();
});

app.post('/notes', async (req, res) => {
  const id = await req.memory.store(req.body);
  res.json({ id });
});
Full Express guide →
Node.js

Fastify

Register the SDK as a Fastify plugin with decorators.

import Fastify from 'fastify';
import { BRRAInClient } from '@brrain/sdk';

const app = Fastify();
const mem = new BRRAInClient(process.env.BRRAIN_API_KEY);

app.decorate('memory', mem.workspace('app'));

app.get('/search', async (req) => {
  return app.memory.retrieve({ search: req.query.q, limit: 20 });
});
Full Fastify guide →
Python

Flask

Store lead-gen form submissions as first-class memory records.

from flask import Flask, request, jsonify
from brrain_sdk import BRRAInClient
import os

app = Flask(__name__)
mem = BRRAInClient(os.environ["BRRAIN_API_KEY"])
ws = mem.workspace("leads")

@app.post("/leads")
def create_lead():
    id = ws.store(request.json, context={"channel": "web"})
    return jsonify(id=id)
Full Flask guide →
Python

FastAPI

Dependency-injected workspace for typed route handlers.

from fastapi import FastAPI, Depends
from brrain_sdk import BRRAInClient
import os

mem = BRRAInClient(os.environ["BRRAIN_API_KEY"])
def get_workspace():
    return mem.workspace("app")

app = FastAPI()

@app.get("/search")
async def search(q: str, ws = Depends(get_workspace)):
    return await ws.retrieve(search=q, limit=20)
Full FastAPI guide →

Database adapters

Dual-write or shadow-write patterns to bring bRRAIn alongside your existing store.

Adapter

PostgreSQL

Capture row-level writes via LISTEN/NOTIFY and mirror them to bRRAIn.

listener.Listen("record_changes", func(n *pq.Notification) {
    var row map[string]any
    json.Unmarshal([]byte(n.Extra), &row)
    mem.Workspace("app").Store(row, sdk.WithContext(map[string]string{
        "source": "postgres",
    }))
})
Full Postgres guide →
Adapter

MongoDB

Tail the oplog and replicate inserts/updates into the bRRAIn graph.

cursor, _ := coll.Watch(ctx, mongo.Pipeline{}, options.ChangeStream())
for cursor.Next(ctx) {
    var evt struct{ FullDocument bson.M }
    cursor.Decode(&evt)
    mem.Workspace("app").Store(evt.FullDocument)
}
Full MongoDB guide →

Message queue adapters

Treat event streams as a firehose for long-term memory.

Adapter

Kafka

Consume from a topic and store each message as a record with offset provenance.

for {
    msg, err := reader.ReadMessage(ctx)
    if err != nil { break }
    mem.Workspace("events").Store(msg.Value, sdk.WithContext(map[string]string{
        "topic":  msg.Topic,
        "offset": strconv.FormatInt(msg.Offset, 10),
    }))
}
Full Kafka guide →
Adapter

RabbitMQ

Bind a consumer to a queue and fan events into bRRAIn.

msgs, _ := ch.Consume("events", "", true, false, false, false, nil)
for d := range msgs {
    mem.Workspace("events").Store(d.Body, sdk.WithContext(map[string]string{
        "exchange":    d.Exchange,
        "routing_key": d.RoutingKey,
    }))
}
Full RabbitMQ guide →

Missing your framework?

The SDK is plain HTTP under the hood — adapt these patterns to any language or framework.