AgentShieldAgentShield/Documentation
Get API Key

JavaScript / TypeScript SDK

Official SDK for Node.js, browsers, and edge runtimes. Use intercept() with your existing LLM.

Installation

npm install agentshield-ai-sdk

Recommended: intercept

import { createClient } from "agentshield-ai-sdk"

const agentshield = createClient({
  apiKey: process.env.AGENTSHIELD_API_KEY!,
  // baseUrl: "http://localhost:3000", // optional for local dev
})

const result = await agentshield.intercept({
  input: userMessage,
  output: aiResponse,
})

if (result.blocked) {
  console.log("Blocked:", result.reason, result.violationType)
  return "I cannot provide that information."
}

return result.output ?? aiResponse

Express example

import { createClient } from "agentshield-ai-sdk"

const agentshield = createClient({
  apiKey: process.env.AGENTSHIELD_API_KEY!,
})

app.post("/chat", async (req, res) => {
  const { message } = req.body
  const aiResponse = await llm.generate(message)

  try {
    const result = await agentshield.intercept({
      input: message,
      output: aiResponse,
    })

    if (result.blocked) {
      return res.status(200).json({
        message: "Response blocked for safety reasons.",
        reason: result.reason,
      })
    }

    return res.json({ message: result.output ?? aiResponse })
  } catch (error) {
    if (error.code === "RATE_LIMIT") {
      return res.status(429).json({ error: "Too many requests" })
    }
    throw error
  }
})

Legacy API

shield() is still exported but deprecated. Prefer createClient().