Node.js

Node.js Integration

Secure your Express.js, NestJS, or Fastify applications with real-time content analysis middleware.

Prerequisites

Node.js v18+
SafeComms API Key Get Key →
npm or yarn

1. Installation

Start by installing the SafeComms SDK into your project using your preferred package manager.

$ npm install safecomms

2. Initialize Client

Next, initialize the SafeComms client with your API key. This creates a connection to our analysis engine.

server.js
import express from 'express';
import { SafeCommsClient } from 'safecomms';

const app = express();

// Initialize with your API Key
const moderator = new SafeCommsClient({
  apiKey: process.env.SAFECOMMS_API_KEY
});

3. Add Middleware

Add the middleware to your application pipeline. This will automatically scan incoming request bodies for harmful content.

server.js
app.use(express.json());

app.post('/api/comments', async (req, res) => {
  const { content } = req.body;

  try {
    // Check content
    const result = await moderator.moderateText({
      content: content,
      // pii: true, // Enable PII detection (Starter Tier+)
    });

    if (!result.isClean) {
      return res.status(400).json(result);
    }

    // Content is safe
    res.json({ success: true });
  } catch (error) {
    res.status(500).json({ error: 'Moderation check failed' });
  }
});

4. Verify & Test

Finally, verify your integration is working correctly by sending a test request.

Terminal
curl -X POST http://localhost:3000/api/comments \
  -H "Content-Type: application/json" \
  -d '{"content": "This is some sample text with profanity"}'
Expected Output (400 Bad Request)
{
  "id": "req_123abc",
  "isClean": false,
  "severity": "Critical",
  "categoryScores": {
    "profanity": 0.98,
    "toxicity": 0.85
  },
  "reason": "Content contains profanity"
}

5. Complete Example

Here is the full code block ready to copy and paste.

server.js
import express from 'express';
import { SafeCommsClient } from 'safecomms';

const app = express();

// Initialize with your API Key
const moderator = new SafeCommsClient({
  apiKey: process.env.SAFECOMMS_API_KEY
});

app.use(express.json());

// Middleware to check comments before saving
app.post('/api/comments', async (req, res) => {
  const { content } = req.body;

  try {
    // 1. Check content
    const result = await moderator.moderateText({
      content: content,
      // pii: true, // Enable PII detection (Starter Tier+)
    });

    // 2. Act on result
    if (!result.isClean) {
      return res.status(400).json(result);
    }

    // 3. Content is safe, proceed to save...
    // await db.comments.create({ content });
    
    res.json({ success: true });
  } catch (error) {
    console.error(error);
    res.status(500).json({ error: 'Moderation check failed' });
  }
});

app.listen(3000, () => console.log('Server running on port 3000'));

Configuration & Tuning

Need to adjust sensitivity or allow certain words? You don't need to change your code. Head to the dashboard to configure your moderation profile globally.