Safeguard Docs
Safeguard AI SDKs

Quickstart

Ask Safeguard AI a question, continue the conversation, stream an answer and list conversations, in TypeScript, Python and Go.

Quickstart

Each program below does the same four things: asks a question, continues the conversation, streams an answer and lists conversations. Set SAFEGUARD_API_KEY first (create a key) and install the SDK.

TypeScript

Save as quickstart.mjs and run node quickstart.mjs.

import { Safeguard } from "@safeguard-sh/sdk";

const client = new Safeguard(); // reads SAFEGUARD_API_KEY

// Ask a question. A new conversation is created for you.
const first = await client.query("In two sentences, what is an SBOM?");
console.log(first.answer);

// Continue the same conversation.
const next = await client.query("Name two common SBOM formats.", {
  conversationId: first.conversationId,
});
console.log(next.answer);

// Stream an answer as it is produced.
for await (const ev of client.stream("In one sentence, what is reachability analysis?")) {
  if (ev.type === "final") console.log(ev.answer);
}

// List your conversations, newest activity first.
const page = await client.conversations.list({ limit: 3 });
console.log(`${page.total} conversations; latest: ${page.conversations[0]?.title}`);

Python

Save as quickstart.py and run python quickstart.py.

from safeguard_sdk import Final, Safeguard

client = Safeguard()  # reads SAFEGUARD_API_KEY

# Ask a question. A new conversation is created for you.
first = client.query("In two sentences, what is an SBOM?")
print(first.answer)

# Continue the same conversation.
follow_up = client.query("Name two common SBOM formats.", conversation_id=first.conversation_id)
print(follow_up.answer)

# Stream an answer as it is produced.
with client.stream("In one sentence, what is reachability analysis?") as events:
    for event in events:
        if isinstance(event, Final):
            print(event.answer)

# List your conversations, newest activity first.
page = client.conversations.list(limit=3)
print(f"{page.total} conversations; latest: {page.conversations[0].title}")

Go

Save as main.go in a module that has run go get safeguard.sh/sdk@v0.1.0, then go run ..

package main

import (
	"context"
	"fmt"
	"log"

	safeguard "safeguard.sh/sdk"
)

func main() {
	ctx := context.Background()
	client, err := safeguard.NewClient() // reads SAFEGUARD_API_KEY
	if err != nil {
		log.Fatal(err)
	}

	// Ask a question. A new conversation is created for you.
	first, err := client.Query(ctx, "In two sentences, what is an SBOM?", nil)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(first.Answer)

	// Continue the same conversation.
	next, err := client.Query(ctx, "Name two common SBOM formats.",
		&safeguard.QueryOptions{ConversationID: first.ConversationID})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(next.Answer)

	// Stream an answer as it is produced.
	s, err := client.Stream(ctx, "In one sentence, what is reachability analysis?", nil)
	if err != nil {
		log.Fatal(err)
	}
	defer s.Close()
	for s.Next() {
		if ev, ok := s.Event().(safeguard.FinalEvent); ok {
			fmt.Println(ev.Answer)
		}
	}
	if err := s.Err(); err != nil {
		log.Fatal(err)
	}

	// List your conversations, newest activity first.
	page, err := client.ListConversations(ctx, safeguard.ListOptions{Limit: 3})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%d conversations; latest: %s\n", page.Total, page.Conversations[0].Title)
}

What you get

Example output (shortened, and the wording of an answer varies from run to run):

An SBOM (Software Bill of Materials) is a formal, machine-readable inventory of ...
The two most common SBOM formats are CycloneDX ... and SPDX ...
Reachability analysis is a technique that determines whether vulnerable code ...
20 conversations; latest: In one sentence, what is reachability analysis?

A query without a conversation id starts a new conversation and titles it from the prompt. Pass the id back to continue it. See Conversations.

On this page