Skip to main content
The @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

Create one client per process. 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

Call guard 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.
Your code must return or throw when isBlocked is true. It must also forward transformedPayload when present. Otherwise, a finding is recorded in Activity but the original payload still reaches the model.

4. Verify

  1. Run one request through a guarded path with the policy in Observe mode.
  2. Confirm the event in TrustGuard Activity, under the consumerId you passed.
  3. Confirm both directions are there. One event per turn means the output call is missing.
A standalone smoke test, with a jailbreak rule in Enforce mode:

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 as consumerId; gates match it as consumer.id
  • session_id: sessionId, or a synthesized value
  • attributes on the evaluation body: consumer.{name,tag,type}, model.{name,provider}, collector.type, source.application, tool.{name,command,arguments}. Nested form: { "source": { "application": "…" } }
Each decision appears in Activity under its consumer_id. Use a consistent identifier for per-user reporting and policy conditions.

Troubleshooting