moderyo / sdk
Official PHP SDK for Moderyo Content Moderation API
v2.0.7
2026-02-17 20:58 UTC
Requires
- php: ^8.4
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- psr/http-client: ^1.0
- psr/log: ^3.0
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
README
Official PHP SDK for the Moderyo Content Moderation API.
Requirements
- PHP 8.4 or higher
- Guzzle HTTP 7.x
Installation
composer require moderyo/sdk
Quick Start
use Moderyo\ModeryoClient; $client = new ModeryoClient('your-api-key'); $result = $client->moderate('Hello, this is a friendly message!'); if ($result->isBlocked) { echo "BLOCKED: " . ($result->policyDecision?->reason ?? 'Policy violation') . "\n"; } elseif ($result->isFlagged) { echo "FLAGGED for review\n"; } else { echo "ALLOWED\n"; }
Configuration
API Key String
$client = new ModeryoClient('your-api-key');
API Key with Options
$client = new ModeryoClient('your-api-key', [ 'baseUrl' => 'https://api.moderyo.com', 'timeout' => 60, 'maxRetries' => 5, ]);
ModeryoConfig Object
use Moderyo\ModeryoConfig; use Moderyo\ModeryoClient; $config = new ModeryoConfig([ 'apiKey' => 'your-api-key', 'baseUrl' => 'https://api.moderyo.com', 'timeout' => 30, 'maxRetries' => 3, 'retryDelay' => 1.0, 'defaultModel' => 'omni-moderation-latest', ]); $client = new ModeryoClient($config);
Environment Variables
// Set MODERYO_API_KEY and optionally MODERYO_BASE_URL $client = ModeryoClient::fromEnv();
Moderation
Basic
$result = $client->moderate('Text to check'); echo "Blocked: " . ($result->isBlocked ? 'Yes' : 'No') . "\n"; echo "Flagged: " . ($result->isFlagged ? 'Yes' : 'No') . "\n"; echo "Allowed: " . ($result->isAllowed ? 'Yes' : 'No') . "\n";
With Options
$result = $client->moderate('Text to check', [ 'model' => 'omni-moderation-latest', 'longTextMode' => true, 'mode' => 'enforce', 'risk' => 'balanced', ]);
Batch
$batch = $client->moderateBatch(['Hello', 'Bad text', 'Spam']); echo "Blocked: " . count($batch->getBlocked()) . "\n"; echo "Has blocked: " . ($batch->hasBlocked() ? 'Yes' : 'No') . "\n";
Error Handling
use Moderyo\Exceptions\AuthenticationException; use Moderyo\Exceptions\RateLimitException; use Moderyo\Exceptions\ValidationException; use Moderyo\Exceptions\QuotaExceededException; use Moderyo\Exceptions\NetworkException; use Moderyo\Exceptions\ModeryoException; try { $result = $client->moderate($text); } catch (AuthenticationException $e) { // Invalid API key (401) } catch (RateLimitException $e) { sleep((int) $e->retryAfter); } catch (ValidationException $e) { // Invalid input (400/422) } catch (QuotaExceededException $e) { // Plan quota exceeded (402) } catch (NetworkException $e) { // Connection/timeout after retries } catch (ModeryoException $e) { echo "Error: " . $e->getMessage() . "\n"; }
Laravel Integration
Add MODERYO_API_KEY=your-key to .env. Service provider auto-discovers.
use Moderyo\Laravel\Facades\Moderyo; $result = Moderyo::moderate('Check this text');
Running Tests
composer install vendor/bin/phpunit
Links
- Packagist: packagist.org/packages/moderyo/sdk
- Documentation: docs.moderyo.com/sdk/php
- Playground: playground-examples/php
- Website: moderyo.com
License
MIT - see LICENSE.