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.
| Setting | TypeScript | Python | Go | Default |
|---|---|---|---|---|
| API key | apiKey | api_key | WithAPIKey | SAFEGUARD_API_KEY |
| Base URL | baseUrl | base_url | WithBaseURL | https://api.safeguard.sh/gpt/v1/ai |
| Product | productId | product_id | WithProductID | the key owner's active product |
| Timeout | timeoutMs | timeout (seconds) | context deadline | 120000 ms, 60 s, none |
| Retries | maxRetries | max_retries | WithMaxRetries | 2 |
| Your own HTTP client | fetch | http_client | WithHTTPClient | the 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
trueUse 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.