Skip to content

C# SDK

Terminal window
dotnet add package Sieve.Moderation
using Sieve.Moderation;
var client = new SieveClient(Environment.GetEnvironmentVariable("SIEVE_API_KEY"));
var result = await client.Moderate.TextAsync(new TextModerationRequest {
Text = "Message to moderate",
Context = "chat",
Username = "player123"
});
if (result.Action == ModerationAction.Block) {
Console.WriteLine($"Blocked for: {result.PrimaryCategory}");
}

Until the SDK is available, use HttpClient to call the Sieve API:

using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
public class ModerationResult
{
[JsonPropertyName("action")]
public string Action { get; set; }
[JsonPropertyName("primary_category")]
public string PrimaryCategory { get; set; }
[JsonPropertyName("scores")]
public Dictionary<string, double> Scores { get; set; }
[JsonPropertyName("tier")]
public int Tier { get; set; }
[JsonPropertyName("latency_ms")]
public int LatencyMs { get; set; }
}
public class SieveClient
{
private readonly HttpClient _http;
public SieveClient(string apiKey)
{
_http = new HttpClient();
_http.BaseAddress = new Uri("https://api.getsieve.dev");
_http.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
}
public async Task<ModerationResult> ModerateTextAsync(
string text, string context = "default", string username = null)
{
var response = await _http.PostAsJsonAsync("/v1/moderate/text", new {
text,
context,
username
});
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<ModerationResult>();
}
}
// Usage
var client = new SieveClient(Environment.GetEnvironmentVariable("SIEVE_API_KEY"));
var result = await client.ModerateTextAsync("Hello world", "chat", "player123");
switch (result.Action)
{
case "block":
Console.WriteLine($"Blocked for: {result.PrimaryCategory}");
break;
case "flag":
Console.WriteLine($"Flagged for review: {result.PrimaryCategory}");
break;
case "allow":
Console.WriteLine("Content is clean");
break;
}
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using System.Text;
public class SieveModeration : MonoBehaviour
{
private const string API_URL = "https://api.getsieve.dev/v1/moderate/text";
private string _apiKey;
void Start()
{
_apiKey = "mod_live_your_key"; // Load from config in production
}
public IEnumerator ModerateChat(string message, string username,
System.Action<string> onResult)
{
var json = JsonUtility.ToJson(new ModerationRequest {
text = message,
context = "chat",
username = username
});
var request = new UnityWebRequest(API_URL, "POST");
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", $"Bearer {_apiKey}");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
{
var result = JsonUtility.FromJson<ModerationResponse>(
request.downloadHandler.text);
onResult(result.action);
}
else
{
// Fail open
onResult("allow");
}
}
[System.Serializable]
private class ModerationRequest
{
public string text;
public string context;
public string username;
}
[System.Serializable]
private class ModerationResponse
{
public string action;
public string primary_category;
}
}
// Program.cs - Register as a service
builder.Services.AddSingleton(sp =>
new SieveClient(builder.Configuration["Sieve:ApiKey"]));
// CommentController.cs
[ApiController]
[Route("api/[controller]")]
public class CommentController : ControllerBase
{
private readonly SieveClient _sieve;
public CommentController(SieveClient sieve) => _sieve = sieve;
[HttpPost]
public async Task<IActionResult> CreateComment([FromBody] CreateCommentRequest req)
{
var modResult = await _sieve.ModerateTextAsync(
req.Body, "comment", User.Identity.Name);
if (modResult.Action == "block")
return UnprocessableEntity("Comment violates community guidelines.");
// Save comment
var comment = await _commentService.CreateAsync(req);
return Ok(comment);
}
}