Safeguard Docs
Safeguard AI SDKs

Configuration

Configure the Safeguard AI SDKs: API key, base URL, product context, timeouts and retries.

Configuration

Every option is optional. With no options the client reads SAFEGUARD_API_KEY and talks to https://api.safeguard.sh/gpt/v1/ai.

SettingTypeScriptPythonGoDefault
API keyapiKeyapi_keyWithAPIKeySAFEGUARD_API_KEY
Base URLbaseUrlbase_urlWithBaseURLhttps://api.safeguard.sh/gpt/v1/ai
ProductproductIdproduct_idWithProductIDthe key owner's active product
TimeouttimeoutMstimeout (seconds)context deadline120000 ms, 60 s, none
RetriesmaxRetriesmax_retriesWithMaxRetries2
Your own HTTP clientfetchhttp_clientWithHTTPClientthe platform's

There is no organization or tenant option. The key fixes the tenant, the organization and the user, and the SDKs send nothing else to identify you. To work as someone else or in another organization, use a key created by that member there.

Product context

context() returns what the key can see: tenant, organization, user, the products the owner can use, the default product and the scopes. Pass one of those product ids as the product to ask in that product's context. Without one, the owner's active product is used.

Timeouts

For one-shot calls the timeout covers the whole request. For streams it covers the wait for the response and then each wait for the next chunk, so a long answer that keeps producing events is not cut off. In Go, put deadlines on the context and do not set Timeout on the HTTP client, because streams are long.

TypeScript

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

const client = new Safeguard({
  // apiKey: "sg_sk_...",          // default: the SAFEGUARD_API_KEY environment variable
  baseUrl: "https://api.safeguard.sh/gpt/v1/ai", // the default
  timeoutMs: 120_000, // the default; 0 turns the timeout off
  maxRetries: 2, // the default; 0 turns retries off
});

// The key decides the tenant, organization and user. Ask the API what this key can see.
const ctx = await client.context();
console.log(ctx.scopes, ctx.products.map((p) => `${p.id}:${p.name}`), ctx.productId);

// To work in a particular product, pass its id (from ctx.products) on the client or on a call.
const forProduct = new Safeguard({ productId: ctx.productId ?? undefined });
console.log(forProduct.productId === ctx.productId);

Python

from safeguard_sdk import Safeguard

client = Safeguard(
    # api_key="sg_sk_...",                       # default: the SAFEGUARD_API_KEY environment variable
    base_url="https://api.safeguard.sh/gpt/v1/ai",  # the default
    timeout=60,  # seconds; the default
    max_retries=2,  # the default; 0 turns retries off
)

# The key decides the tenant, organization and user. Ask the API what this key can see.
ctx = client.context()
print(ctx.scopes, [f"{p.id}:{p.name}" for p in ctx.products], ctx.product_id)

# To work in a particular product, pass its id (from ctx.products) on the client or on a call.
for_product = Safeguard(product_id=ctx.product_id)
print(for_product.product_id == ctx.product_id)

Go

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"time"

	safeguard "safeguard.sh/sdk"
)

func main() {
	// Every option is optional. The values below are the defaults, except the HTTP client.
	client, err := safeguard.NewClient(
		// safeguard.WithAPIKey("sg_sk_..."),  // default: the SAFEGUARD_API_KEY environment variable
		safeguard.WithBaseURL("https://api.safeguard.sh/gpt/v1/ai"),
		safeguard.WithMaxRetries(2),
		safeguard.WithHTTPClient(&http.Client{}), // no Timeout here: use context deadlines, streams are long
	)
	if err != nil {
		log.Fatal(err)
	}

	// Timeouts are context deadlines.
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	// The key decides the tenant, organization and user. Ask the API what this key can see.
	info, err := client.Context(ctx)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(info.Scopes, len(info.Products), info.ProductID)
}

Example output (shortened; the products are the ones your key's owner can use):

[ 'ai:chat' ] [ '6:GRC', '3:TPRM', '4:OSM', '2:PORTAL', '1:ESSCM', '5:CONSOLE' ] 6
true

Use baseUrl only to point at a proxy or a Safeguard deployment you were given. Python and Go accept your own HTTP client for proxies and custom certificates; the SDK does not close a client you pass.

On this page