promptdefend
v1.0.0
Published
Node.js SDK for the Prompt Defend AI Security API - 16-Layer Guardrail Protection
Maintainers
Readme
Prompt Defend Node.js SDK
The official Node.js SDK for the Prompt Defend AI Security API. Protect your AI applications with our 16-layer guardrail system.
Installation
npm install promptdefendOr with yarn:
yarn add promptdefendQuick Start
const { PromptDefend } = require('promptdefend');
// Initialize the client with your API key
const client = new PromptDefend({ apiKey: 'your-api-key' });
// Scan a prompt for security issues
const result = await client.scan('What is the weather today?');
if (result.safe) {
console.log('✅ Prompt is safe to process');
} else {
console.log(`⚠️ Warning: ${result.reason}`);
}Features
- 🛡️ 16-Layer Protection - Comprehensive guardrail system
- 🔒 Prompt Injection Detection - Block jailbreak and injection attempts
- ⚡ Sub-millisecond Latency - Average response time ~0.7ms
- 📦 TypeScript Support - Full type definitions included
- 🔧 Error Handling - Comprehensive error classes
- 🌐 ESM & CommonJS - Works with both module systems
Usage
ESM (ES Modules)
import { PromptDefend } from 'promptdefend';
const client = new PromptDefend({ apiKey: 'your-api-key' });
const result = await client.scan('Hello, how are you?');CommonJS
const { PromptDefend } = require('promptdefend');
const client = new PromptDefend({ apiKey: 'your-api-key' });
const result = await client.scan('Hello, how are you?');TypeScript
import { PromptDefend, ScanResult, PromptDefendOptions } from 'promptdefend';
const options: PromptDefendOptions = {
apiKey: 'your-api-key',
baseUrl: 'https://api.promptdefend.dev'
};
const client = new PromptDefend(options);
const result: ScanResult = await client.scan('Test prompt');
console.log(result.safe); // boolean
console.log(result.reason); // string
console.log(result.details); // objectCustom Configuration
const { PromptDefend } = require('promptdefend');
const client = new PromptDefend({
apiKey: 'your-api-key',
baseUrl: 'https://api.promptdefend.dev', // Optional custom endpoint
timeout: 60000 // Custom timeout in milliseconds
});Error Handling
The SDK provides specific error classes for different error types:
const {
PromptDefend,
PromptDefendError,
AuthenticationError,
NetworkError,
APIError
} = require('promptdefend');
const client = new PromptDefend({ apiKey: 'your-api-key' });
try {
const result = await client.scan('Test prompt');
} catch (error) {
if (error instanceof AuthenticationError) {
console.error(`Invalid API key: ${error.message}`);
} else if (error instanceof NetworkError) {
console.error(`Network issue: ${error.message}`);
} else if (error instanceof APIError) {
console.error(`API error (status ${error.statusCode}): ${error.message}`);
} else if (error instanceof PromptDefendError) {
console.error(`General error: ${error.message}`);
}
}Response Object
The scan() method returns a ScanResult object with the following properties:
| Property | Type | Description |
|----------|------|-------------|
| safe | boolean | Whether the prompt is safe to process |
| reason | string | Explanation of the safety assessment |
| details | object | Full detection details from all 16 layers |
| fastPath | boolean | Whether allowlist short-circuit was used |
API Reference
PromptDefend Class
Constructor Options
interface PromptDefendOptions {
apiKey: string; // Required: Your API key
baseUrl?: string; // Optional: Custom API base URL
timeout?: number; // Optional: Request timeout in milliseconds (default: 30000)
}Methods
| Method | Description |
|--------|-------------|
| scan(promptText: string): Promise<ScanResult> | Scan a prompt and return results |
Requirements
- Node.js 18.0.0 or higher
License
This SDK is proprietary software. See LICENSE for details.
Support
© 2026 Prompt Defend. All Rights Reserved.
