Go

Go Integration

Secure your Go microservices with high-performance content moderation.

Prerequisites

1. Installation

Start by installing the SafeComms SDK into your project using go get.

$ go get github.com/safecomms/safecomms-go

2. Initialize Client

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

main.go
import (
    "os"
    "github.com/safecomms/safecomms-go"
)

// Initialize with your API Key
client := safecomms.NewClient(os.Getenv("SAFECOMMS_API_KEY"), "")

3. Add Handler Logic

Add the moderation logic to your http handler. This will scan the incoming request content before processing it further.

main.go
func handleComment(w http.ResponseWriter, r *http.Request) {
    var req CommentRequest
    if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }

    // Check content
    result, err := client.ModerateText(safecomms.ModerateTextRequest{
        Content: req.Content,
        // Pii: true, // Enable PII detection (Starter Tier+)
    })

    if err != nil {
        http.Error(w, "Moderation check failed", http.StatusInternalServerError)
        return
    }

    if !result.IsClean {
        w.WriteHeader(http.StatusBadRequest)
        json.NewEncoder(w).Encode(result)
        return
    }

    // Content is safe
    json.NewEncoder(w).Encode(map[string]bool{"success": true})
}

4. Verify & Test

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

Terminal
curl -X POST http://localhost:8080/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.

main.go
package main

import (
    "encoding/json"
    "log"
    "net/http"
    "os"

    "github.com/safecomms/safecomms-go"
)

type CommentRequest struct {
    Content string `json:"content"`
}

func main() {
    // Initialize with your API Key
    client := safecomms.NewClient(os.Getenv("SAFECOMMS_API_KEY"), "")

    http.HandleFunc("/api/comments", func(w http.ResponseWriter, r *http.Request) {
        if r.Method != http.MethodPost {
            http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
            return
        }

        var req CommentRequest
        if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
            http.Error(w, err.Error(), http.StatusBadRequest)
            return
        }

        // 1. Check content
        result, err := client.ModerateText(safecomms.ModerateTextRequest{
            Content: req.Content,
            // Pii: true, // Enable PII detection (Starter Tier+)
        })

        if err != nil {
            log.Printf("Error moderating text: %v", err)
            http.Error(w, "Moderation check failed", http.StatusInternalServerError)
            return
        }

        // 2. Act on result
        if !result.IsClean {
            w.Header().Set("Content-Type", "application/json")
            w.WriteHeader(http.StatusBadRequest)
            json.NewEncoder(w).Encode(result)
            return
        }

        // 3. Content is safe, proceed to save...
        // db.SaveComment(req.Content)

        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(map[string]bool{"success": true})
    })

    log.Println("Server running on port 8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

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.