JavaScript / TypeScript
Use the Aguardic REST API from JavaScript and TypeScript applications.
Using the REST API
Aguardic provides a REST API that works with any HTTP client. No SDK installation is required — use fetch, axios, or any library you prefer.
Helper Functions
Create a simple wrapper for common operations:
const AGUARDIC_API_KEY = process.env.AGUARDIC_API_KEY;
const BASE_URL = "https://api.aguardic.com/v1";
async function aguardic(path: string, body?: Record<string, any>) {
const res = await fetch(`${BASE_URL}${path}`, {
method: body ? "POST" : "GET",
headers: {
Authorization: `Bearer ${AGUARDIC_API_KEY}`,
"Content-Type": "application/json",
},
body: body ? JSON.stringify(body) : undefined,
});
if (!res.ok) {
const error = await res.json();
throw new Error(error.errors?.[0]?.message ?? "Request failed");
}
const json = await res.json();
return json.data;
}Evaluate Content
const result = await aguardic("/evaluate", {
input: { content: "User message to evaluate" },
targetKey: "chat-message",
});
if (result.outcome === "BLOCK") {
console.log("Blocked:", result.violations);
} else if (result.outcome === "WARN") {
console.log("Warning:", result.violations);
}Manage Sessions
// Create a session
const session = await aguardic("/evaluation-sessions", {
metadata: { agentId: "support-bot" },
});
// Evaluate within the session
const result = await aguardic("/evaluate", {
sessionId: session.id,
input: { tool: "send_email", args: { to: "user@example.com" } },
targetKey: "send_email",
});
// End the session
const summary = await aguardic(`/evaluation-sessions/${session.id}/end`, {
status: "COMPLETED",
});TypeScript Types
Define types for API responses:
interface EvaluateResult {
outcome: "ALLOW" | "WARN" | "BLOCK" | "APPROVAL_REQUIRED";
enforcementAction: string;
evaluationRunId: string | null;
reviewRequestId: string | null;
pollUrl: string | null;
sessionId: string | null;
violations: Violation[];
}
interface Violation {
id: string;
ruleId: string;
ruleName: string;
severity: "LOW" | "MEDIUM" | "HIGH" | "CRITICAL";
resolvedAction: string;
explanation: string;
field: string | null;
snippet: string | null;
}
interface Session {
id: string;
status: "ACTIVE" | "COMPLETED" | "TERMINATED";
actionCount: number;
dataTags: string[];
toolsUsed: string[];
}Next Steps
- Evaluate API — Full endpoint reference
- Sessions API — Session management reference
- Agent Integration — Govern AI agent actions