| path | docs/api/Settings_API.mdx | |||||
|---|---|---|---|---|---|---|
| title | System Settings API | |||||
| description | API reference for managing system-wide and tenant-specific configuration settings in SveltyCMS. | |||||
| order | 4 | |||||
| icon | mdi:cog | |||||
| author | admin | |||||
| created | 2025-10-05 | |||||
| updated | 2025-10-05 | |||||
| tags |
|
The Settings API provides endpoints for managing system-wide configuration in SveltyCMS. All settings are stored in the database using a database-agnostic adapter, supporting multi-tenancy when enabled.
Base Path: /api/settings
All settings endpoints require authentication:
Cookie: session=your-session-idMost operations also require admin permissions.
Retrieves settings for a specific configuration group.
GET /api/settings/[group]Path Parameters:
group(string) - Configuration group name (e.g.,'system','email','security')
Headers:
Cookie: session=your-session-idPermissions Required: Authenticated user
Success (200):
{
"success": true,
"data": {
"SITE_NAME": "My CMS",
"SITE_URL": "https://example.com",
"MULTI_TENANT": false
}
}Error Responses:
// 401 Unauthorized
{
"success": false,
"message": "Unauthorized"
}
// 404 Not Found - Group doesn't exist
{
"success": false,
"message": "Settings group not found"
}
// 500 Internal Server Error
{
"success": false,
"message": "Failed to retrieve settings"
}Updates one or more system settings in the database.
POST /api/settings/updateHeaders:
Cookie: session=your-session-id
Content-Type: application/jsonBody:
{
"SITE_NAME": "Updated Site Name",
"SITE_URL": "https://newdomain.com",
"MULTI_TENANT": true
}Permissions Required: Admin user (recommended)
Success (200):
{
"success": true,
"message": "Settings saved successfully."
}Error Responses:
// 401 Unauthorized
{
"success": false,
"message": "Unauthorized"
}
// 400 Bad Request - No settings provided
{
"success": false,
"message": "No settings provided to update."
}
// 500 Internal Server Error
{
"success": false,
"message": "Failed to save settings."
}- Settings are validated before being saved
- Cache is automatically invalidated after successful update
- Changes take effect immediately across the system
- Multi-tenant mode: Some settings may be tenant-specific
{
"SITE_NAME": "string",
"SITE_URL": "string",
"MULTI_TENANT": "boolean",
"DEFAULT_LANGUAGE": "string"
}{
"DB_TYPE": "mongodb | postgres | mysql",
"DB_HOST": "string",
"DB_PORT": "number",
"DB_NAME": "string"
}{
"SMTP_HOST": "string",
"SMTP_PORT": "number",
"SMTP_USER": "string",
"SMTP_FROM": "string"
}{
"SESSION_TIMEOUT": "number (minutes)",
"PASSWORD_MIN_LENGTH": "number",
"ENABLE_2FA": "boolean",
"RATE_LIMIT_ENABLED": "boolean"
}The Settings API uses a database-agnostic adapter:
// Settings are stored and retrieved via the adapter interface
const result = await db.settings.getSettings(group);
const updateResult = await db.settings.updateSettings(settingsData);This means you can swap databases (MongoDB, PostgreSQL, MySQL) without changing API code.
When MULTI_TENANT mode is enabled:
- Settings can be scoped to specific tenants
- Tenant ID is derived from subdomain or user context
- System-wide settings override tenant-specific ones
Tenant-Specific Settings:
GET /api/settings/system
# Automatically scoped to the current tenantSome settings contain sensitive data:
- Database credentials
- API keys
- SMTP passwords
- Encryption keys
Best Practices:
- Never expose sensitive settings in client-side code
- Use environment variables for critical secrets
- Implement proper role-based access control
- Audit all settings changes
- Encrypt sensitive values at rest
All settings are validated before being saved:
- Type checking (string, number, boolean)
- Format validation (URLs, emails)
- Range validation (min/max values)
- Dependency validation (some settings require others)
Settings are cached for performance:
- Cache Duration: Configurable (default: until invalidated)
- Invalidation: Automatic after updates
- Manual Invalidation: Available via internal API
Cache Behavior:
- First request: Load from database, cache result
- Subsequent requests: Serve from cache
- After update: Cache invalidated, next request re-loads
Get Settings:
curl -X GET https://your-domain.com/api/settings/system \
-H "Cookie: session=your-session-cookie"Update Settings:
curl -X POST https://your-domain.com/api/settings/update \
-H "Cookie: session=your-session-cookie" \
-H "Content-Type: application/json" \
-d '{
"SITE_NAME": "My New CMS",
"SITE_URL": "https://newdomain.com"
}'// Get settings
const response = await fetch('/api/settings/system', {
credentials: 'include'
});
const { data } = await response.json();
// Update settings
const updateResponse = await fetch('/api/settings/update', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify({
SITE_NAME: 'Updated Name',
MULTI_TENANT: true
})
});For implementation details, see:
src/routes/api/settings/update/+server.ts- Update endpointsrc/routes/api/settings/[group]/+server.ts- Get endpointsrc/stores/globalSettings.ts- Settings store and cachesrc/databases/dbInterface.ts- Database adapter interface