# ServerKit API Reference This document provides complete REST API documentation for ServerKit. --- ## Overview **Base URL:** `http://localhost:47927/api/v1` **Content Type:** All requests and responses use `application/json` **Authentication:** JWT Bearer tokens (except where noted) --- ## Authentication ### Login Authenticate and receive access tokens. ```http POST /auth/login ``` **Rate Limit:** 5 requests per minute **Request Body:** ```json { "email": "user@example.com", "password": "your-password" } ``` **Response (200):** ```json { "user": { "id": 1, "email": "user@example.com", "username": "admin", "role": "admin" }, "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "refresh_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." } ``` **Response (2FA Required):** ```json { "requires_2fa": true, "temp_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...", "message": "Two-factor authentication required" } ``` --- ### Register Create a new user account. The first registered user becomes admin. ```http POST /auth/register ``` **Rate Limit:** 3 requests per minute **Request Body:** ```json { "email": "user@example.com", "username": "newuser", "password": "secure-password" } ``` **Response (201):** ```json { "message": "User registered successfully", "user": { "id": 1, "email": "user@example.com", "username": "newuser", "role": "admin" }, "access_token": "...", "refresh_token": "..." } ``` --- ### Refresh Token Get a new access token using a refresh token. ```http POST /auth/refresh Authorization: Bearer ``` **Response (200):** ```json { "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." } ``` --- ### Get Current User ```http GET /auth/me Authorization: Bearer ``` **Response (200):** ```json { "user": { "id": 1, "email": "user@example.com", "username": "admin", "role": "admin", "totp_enabled": true } } ``` --- ### Update Current User ```http PUT /auth/me Authorization: Bearer ``` **Request Body:** ```json { "username": "newusername", "email": "newemail@example.com", "password": "new-password" } ``` --- ## Two-Factor Authentication ### Setup 2FA Generate a TOTP secret and QR code for setup. ```http POST /2fa/setup Authorization: Bearer ``` **Response (200):** ```json { "secret": "BASE32SECRET", "qr_code": "data:image/png;base64,...", "backup_codes": ["12345678", "87654321", ...] } ``` --- ### Enable 2FA Verify and enable 2FA after setup. ```http POST /2fa/enable Authorization: Bearer ``` **Request Body:** ```json { "code": "123456" } ``` --- ### Verify 2FA Complete login with 2FA code. ```http POST /2fa/verify Authorization: Bearer ``` **Request Body:** ```json { "code": "123456" } ``` **Response (200):** ```json { "user": {...}, "access_token": "...", "refresh_token": "..." } ``` --- ### Disable 2FA ```http POST /2fa/disable Authorization: Bearer ``` **Request Body:** ```json { "code": "123456" } ``` --- ## System Metrics All system endpoints require admin role. ### Get All Metrics ```http GET /system/metrics Authorization: Bearer ``` **Response (200):** ```json { "cpu": { "percent": 15.2, "count": 4, "freq_current": 2400.0 }, "memory": { "total": 8589934592, "available": 4294967296, "percent": 50.0, "used": 4294967296 }, "disk": { "total": 107374182400, "used": 53687091200, "free": 53687091200, "percent": 50.0 }, "network": { "bytes_sent": 1234567890, "bytes_recv": 9876543210 } } ``` --- ### Get CPU Metrics ```http GET /system/cpu ``` ### Get Memory Metrics ```http GET /system/memory ``` ### Get Disk Metrics ```http GET /system/disk ``` ### Get Network Metrics ```http GET /system/network ``` ### Health Check No authentication required. ```http GET /system/health ``` **Response (200):** ```json { "status": "healthy", "service": "serverkit-api" } ``` --- ## Applications ### List Applications ```http GET /apps Authorization: Bearer ``` **Response (200):** ```json { "apps": [ { "id": 1, "name": "my-app", "app_type": "php", "status": "running", "php_version": "8.2", "port": 8080, "root_path": "/var/www/my-app", "created_at": "2024-01-01T00:00:00Z" } ] } ``` --- ### Get Application ```http GET /apps/:id Authorization: Bearer ``` --- ### Create Application ```http POST /apps Authorization: Bearer ``` **Request Body:** ```json { "name": "my-app", "app_type": "php", "php_version": "8.2", "port": 8080, "root_path": "/var/www/my-app" } ``` **Valid app_type values:** `php`, `wordpress`, `flask`, `django`, `docker`, `static` --- ### Update Application ```http PUT /apps/:id Authorization: Bearer ``` --- ### Delete Application ```http DELETE /apps/:id Authorization: Bearer ``` --- ### Start Application ```http POST /apps/:id/start Authorization: Bearer ``` ### Stop Application ```http POST /apps/:id/stop Authorization: Bearer ``` ### Restart Application ```http POST /apps/:id/restart Authorization: Bearer ``` --- ## Environment Variables ### List Environment Variables ```http GET /apps/:app_id/env Authorization: Bearer ``` **Response (200):** ```json { "env_vars": [ { "id": 1, "key": "DATABASE_URL", "is_secret": true, "created_at": "2024-01-01T00:00:00Z" } ] } ``` --- ### Set Environment Variable ```http POST /apps/:app_id/env Authorization: Bearer ``` **Request Body:** ```json { "key": "DATABASE_URL", "value": "postgresql://user:pass@localhost/db", "is_secret": true } ``` --- ### Delete Environment Variable ```http DELETE /apps/:app_id/env/:key Authorization: Bearer ``` --- ## Domains ### List Domains ```http GET /domains Authorization: Bearer ``` ### Create Domain ```http POST /domains Authorization: Bearer ``` **Request Body:** ```json { "domain": "example.com", "app_id": 1 } ``` ### Delete Domain ```http DELETE /domains/:id Authorization: Bearer ``` --- ## SSL Certificates ### List Certificates ```http GET /ssl/certificates Authorization: Bearer ``` ### Issue Certificate Request a Let's Encrypt certificate. ```http POST /ssl/issue Authorization: Bearer ``` **Request Body:** ```json { "domain": "example.com", "email": "admin@example.com" } ``` ### Renew Certificate ```http POST /ssl/renew/:domain Authorization: Bearer ``` --- ## Databases ### List Databases ```http GET /databases Authorization: Bearer ``` ### Create Database ```http POST /databases Authorization: Bearer ``` **Request Body:** ```json { "name": "my_database", "type": "mysql", "charset": "utf8mb4" } ``` ### Delete Database ```http DELETE /databases/:id Authorization: Bearer ``` ### Create Database User ```http POST /databases/:id/users Authorization: Bearer ``` --- ## Docker ### List Containers ```http GET /docker/containers Authorization: Bearer ``` ### Get Container ```http GET /docker/containers/:id Authorization: Bearer ``` ### Start Container ```http POST /docker/containers/:id/start Authorization: Bearer ``` ### Stop Container ```http POST /docker/containers/:id/stop Authorization: Bearer ``` ### Container Logs ```http GET /docker/containers/:id/logs Authorization: Bearer ``` ### List Images ```http GET /docker/images Authorization: Bearer ``` --- ## Files ### List Directory ```http GET /files?path=/var/www Authorization: Bearer ``` **Response (200):** ```json { "files": [ { "name": "index.html", "path": "/var/www/index.html", "type": "file", "size": 1234, "modified": "2024-01-01T00:00:00Z" } ] } ``` ### Read File ```http GET /files/read?path=/var/www/index.html Authorization: Bearer ``` ### Write File ```http POST /files/write Authorization: Bearer ``` **Request Body:** ```json { "path": "/var/www/index.html", "content": "..." } ``` ### Delete File ```http DELETE /files?path=/var/www/old-file.txt Authorization: Bearer ``` --- ## Cron Jobs ### List Cron Jobs ```http GET /cron Authorization: Bearer ``` ### Create Cron Job ```http POST /cron Authorization: Bearer ``` **Request Body:** ```json { "name": "Daily Backup", "command": "/usr/local/bin/backup.sh", "schedule": "0 2 * * *", "enabled": true } ``` ### Update Cron Job ```http PUT /cron/:id Authorization: Bearer ``` ### Delete Cron Job ```http DELETE /cron/:id Authorization: Bearer ``` --- ## Firewall (UFW) ### Get Firewall Status ```http GET /firewall/status Authorization: Bearer ``` ### List Rules ```http GET /firewall/rules Authorization: Bearer ``` ### Add Rule ```http POST /firewall/rules Authorization: Bearer ``` **Request Body:** ```json { "port": 443, "protocol": "tcp", "action": "allow", "direction": "in" } ``` ### Delete Rule ```http DELETE /firewall/rules/:id Authorization: Bearer ``` ### Enable/Disable Firewall ```http POST /firewall/enable POST /firewall/disable Authorization: Bearer ``` --- ## Security ### Get Security Status ```http GET /security/status Authorization: Bearer ``` **Response (200):** ```json { "clamav_installed": true, "clamav_running": true, "last_scan": "2024-01-01T00:00:00Z", "threats_found": 0, "quarantined_files": 3, "integrity_initialized": true } ``` --- ### Get ClamAV Status ```http GET /security/clamav/status Authorization: Bearer ``` ### Install ClamAV ```http POST /security/clamav/install Authorization: Bearer ``` ### Update Virus Definitions ```http POST /security/clamav/update Authorization: Bearer ``` --- ### Scan File ```http POST /security/scan/file Authorization: Bearer ``` **Request Body:** ```json { "path": "/var/www/suspicious-file.php" } ``` ### Scan Directory ```http POST /security/scan/directory Authorization: Bearer ``` **Request Body:** ```json { "path": "/var/www/html", "recursive": true } ``` ### Quick Scan Scan common web directories. ```http POST /security/scan/quick Authorization: Bearer ``` ### Full Scan Scan entire system. ```http POST /security/scan/full Authorization: Bearer ``` --- ### Get Quarantine ```http GET /security/quarantine Authorization: Bearer ``` ### Quarantine File ```http POST /security/quarantine Authorization: Bearer ``` **Request Body:** ```json { "path": "/var/www/malware.php" } ``` ### Delete Quarantined File ```http DELETE /security/quarantine/:id Authorization: Bearer ``` --- ### Initialize Integrity Database ```http POST /security/integrity/initialize Authorization: Bearer ``` **Request Body:** ```json { "paths": ["/var/www", "/etc/nginx"] } ``` ### Check File Integrity ```http GET /security/integrity/check Authorization: Bearer ``` --- ### Get Failed Logins ```http GET /security/failed-logins Authorization: Bearer ``` ### Get Security Events ```http GET /security/events Authorization: Bearer ``` --- ## Notifications ### Get Notification Status ```http GET /notifications/status Authorization: Bearer ``` ### Get Notification Config ```http GET /notifications/config Authorization: Bearer ``` ### Update Channel Config ```http PUT /notifications/config/:channel Authorization: Bearer ``` **Channels:** `discord`, `slack`, `telegram`, `webhook` **Request Body (Discord):** ```json { "enabled": true, "webhook_url": "https://discord.com/api/webhooks/...", "severity_levels": ["warning", "critical"] } ``` ### Test Notification ```http POST /notifications/test/:channel Authorization: Bearer ``` ### Test All Channels ```http POST /notifications/test Authorization: Bearer ``` --- ## Monitoring ### Get Monitoring Status ```http GET /monitoring/status Authorization: Bearer ``` ### Get Alert Thresholds ```http GET /monitoring/thresholds Authorization: Bearer ``` ### Update Alert Thresholds ```http PUT /monitoring/thresholds Authorization: Bearer ``` **Request Body:** ```json { "cpu_warning": 70, "cpu_critical": 90, "memory_warning": 80, "memory_critical": 95, "disk_warning": 80, "disk_critical": 95 } ``` ### Get Alert History ```http GET /monitoring/alerts Authorization: Bearer ``` --- ## Uptime ### Get Uptime History ```http GET /uptime/history Authorization: Bearer ``` ### Get Uptime Stats ```http GET /uptime/stats Authorization: Bearer ``` --- ## Error Responses All endpoints return consistent error responses: ```json { "error": "Error message describing what went wrong" } ``` ### Common Status Codes | Code | Description | |------|-------------| | 200 | Success | | 201 | Created | | 400 | Bad Request - Invalid input | | 401 | Unauthorized - Invalid or missing token | | 403 | Forbidden - Insufficient permissions | | 404 | Not Found - Resource doesn't exist | | 409 | Conflict - Resource already exists | | 429 | Too Many Requests - Rate limit exceeded | | 500 | Internal Server Error | --- ## Rate Limiting Some endpoints have rate limits: | Endpoint | Limit | |----------|-------| | POST /auth/login | 5/minute | | POST /auth/register | 3/minute | When rate limited, you'll receive a 429 response with retry information. --- ## WebSocket Events ServerKit uses Socket.IO for real-time updates. **Connection:** ```javascript const socket = io('http://localhost:47927', { auth: { token: 'your-access-token' } }); ``` **Events:** | Event | Description | |-------|-------------| | `metrics` | Real-time system metrics | | `alert` | New alert triggered | | `scan_progress` | Malware scan progress | | `scan_complete` | Scan finished | --- ## Examples ### cURL ```bash # Login TOKEN=$(curl -s -X POST http://localhost:47927/api/v1/auth/login \ -H "Content-Type: application/json" \ -d '{"email":"admin@example.com","password":"password"}' \ | jq -r '.access_token') # Get system metrics curl -s http://localhost:47927/api/v1/system/metrics \ -H "Authorization: Bearer $TOKEN" | jq # Create application curl -s -X POST http://localhost:47927/api/v1/apps \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: application/json" \ -d '{"name":"my-app","app_type":"php","php_version":"8.2"}' ``` ### Python ```python import requests BASE_URL = "http://localhost:47927/api/v1" # Login response = requests.post(f"{BASE_URL}/auth/login", json={ "email": "admin@example.com", "password": "password" }) token = response.json()["access_token"] headers = {"Authorization": f"Bearer {token}"} # Get metrics metrics = requests.get(f"{BASE_URL}/system/metrics", headers=headers).json() print(f"CPU: {metrics['cpu']['percent']}%") ``` ### JavaScript ```javascript const BASE_URL = 'http://localhost:47927/api/v1'; // Login const loginRes = await fetch(`${BASE_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: 'admin@example.com', password: 'password' }) }); const { access_token } = await loginRes.json(); // Get metrics const metricsRes = await fetch(`${BASE_URL}/system/metrics`, { headers: { 'Authorization': `Bearer ${access_token}` } }); const metrics = await metricsRes.json(); console.log(`CPU: ${metrics.cpu.percent}%`); ``` ---

ServerKit API Reference
Version 1.6.7