Skip to main content

Overview

Security is critical when integrating AGI Agents. Follow these best practices to protect API keys and user data.

Protect API Keys

Never expose API keys in client-side code, public repositories, or logs.

Use Environment Variables

// Good - Environment Variables
const API_KEY = process.env.AGI_API_KEY;

if (!API_KEY) {
  throw new Error('AGI_API_KEY environment variable not set');
}

const headers = { 'Authorization': `Bearer ${API_KEY}` };

Use .env Files (Never Commit)

// Install: npm install dotenv
import 'dotenv/config';

const API_KEY = process.env.AGI_API_KEY;

Validate User Input

Always validate user input before sending to agents.
function sanitizeInput(userInput) {
  if (typeof userInput !== 'string') {
    throw new Error('Input must be string');
  }
  
  if (userInput.length > 5000) {
    throw new Error('Input too long (max 5000 chars)');
  }
  
  // Remove control characters
  return userInput.replace(/[\x00-\x1f\x7f-\x9f]/g, '');
}

// Usage
try {
  const safeInput = sanitizeInput(userQuery);
  await client.sendMessage(sessionId, safeInput);
} catch (e) {
  return { error: `Invalid input: ${e.message}`, status: 400 };
}

Validate URLs

Ensure URLs are safe before sending to agents.
function validateUrl(url) {
  if (url.length > 2000) {
    throw new Error('URL too long');
  }
  
  // Must start with http:// or https://
  if (!/^https?:\/\//.test(url)) {
    throw new Error('URL must start with http:// or https://');
  }
  
  // Block localhost and internal IPs
  if (/(localhost|127\.0\.0\.1|192\.168\.|10\.)/.test(url)) {
    throw new Error('Internal URLs not allowed');
  }
  
  return true;
}

Best Practices

Use Environment Variables

Never hardcode API keys in your code

Validate Input

Always validate and sanitize user input

Use HTTPS

Always use HTTPS endpoints for webhooks

Don't Log Keys

Never log API keys or sensitive data