@neuraltrust/trustguard-sdk package evaluates model traffic from JavaScript
and TypeScript applications. Call it before and after the model request to apply
the Input and Output phases of your policy. The application can include context
such as the authenticated user, tenant, and source document in each evaluation.
The SDK protects only the call sites where you add it. For route-level coverage,
use Node.js middleware. For enforcement across
multiple clients, use TrustGate.
Integration capabilities
Before you start
Create the policy in Observe mode. Observe records decisions in Activity without enforcing
them. Review the results, then switch the policy to Enforce. See
Policies.
1. Install
2. Create the client
baseUrl is the workspace URL from the console; apiKey
is the collector’s tgk_… key, which belongs in the environment or your secret
store rather than in source.
3. Guard both directions
Callguard twice per turn: once before the model request and once after it:
direction selects the policy phase: input runs the Input phase and output
runs the Output phase. It defaults to input when omitted. Always set it so
that Output rules are not skipped inadvertently.
consumerId is the actor findings are grouped under in Activity, and gates
match it as consumer.id. sessionId is the conversation key; it is synthesized
if you leave it out, which loses the grouping between turns.
4. Verify
- Run one request through a guarded path with the policy in Observe mode.
- Confirm the event in TrustGuard Activity, under the
consumerIdyou passed. - Confirm both directions are there. One event per turn means the
outputcall is missing.
Reference
Coverage
Use the SDK when model calls are made in JavaScript or TypeScript code and the
policy requires application context, such as the authenticated user, tenant, or
retrieved document. Use a gateway when enforcement
must not depend on each call site being instrumented.
⚠️ Your application enforces the verdict. Blocking and redaction work only
when your code handles the returned status and transformed payload. Tool coverage
also requires an evaluation around your tool dispatch with
protocol: "mcp".
Ask. status: "ask" is advisory for an application collector. Your code must
allow, deny, or request confirmation.
Limits. Coverage is per call site. Uninstrumented background jobs, queue
consumers, and services are not inspected. To protect HTTP routes by configuration,
use Node.js middleware. The SDK does not send tool
declarations, so tool-poisoning detection does not apply. Route MCP through
TrustGate to cover it.
What is evaluated
Every call is
POST /v1/evaluate with the collector
tgk_… key, and the policy’s detectors decide the
verdict. payload accepts the minimal { "input": "…" } shape above or a full
OpenAI, Anthropic, Gemini, or MCP provider body.
Configuration
Client.guard options, and the field each one becomes on the wire.
Additional routing context belongs in
attributes on the evaluation body. See
Attributes.
The verdict. isBlocked is the block status; transformedPayload is the
rewritten payload, absent unless a Transform rule changed it. The underlying
response also carries status, findings[], traceId, and requestId. The full
contract is documented in the Evaluate API. When
findings return different statuses, the most restrictive wins: block > ask >
transform > report > allow.
Evaluation failures. TrustGuard returns 200 for every detection,
including a block, so a non-200 means the check itself failed:
Decide whether an unreachable TrustGuard should allow or deny the request, and
handle that case explicitly.
Attributes
consumer_id: the value passed asconsumerId; gates match it asconsumer.idsession_id:sessionId, or a synthesized valueattributeson the evaluation body:consumer.{name,tag,type},model.{name,provider},collector.type,source.application,tool.{name,command,arguments}. Nested form:{ "source": { "application": "…" } }
consumer_id. Use a consistent
identifier for per-user reporting and policy conditions.
Troubleshooting
Related
- Python SDK: use the same API from Python
- Node.js middleware: cover Express and Next.js routes by configuration instead of per call site
- REST API: use the same contract without a dependency
- Evaluate API: request and response fields and status codes
- Policies: Observe and Enforce modes, phases, and gates
- Collectors: catalog, API keys, and policy routing
- Coverage: compare available collectors
@neuraltrust/trustguard-sdk: the package on npm