Remove the old Go/Mongo API (api/main.go, go.mod, go.sum, .env.example) and switch telemetry backend to PocketBase (http://db.community-scripts.org). Update documentation and flowcharts to reflect the PocketBase collection (_dev_telemetry_data), new REST endpoints (POST/PATCH/GET), field schema, and revised api.func integration (LXC/VM reporting and status updates). Misc scripts and helpers were adjusted (misc/api.func, misc/build.func, misc/error_handler.func) and a new misc/ingest.go was added. This consolidates telemetry to a hosted PocketBase instance and updates docs and integration points accordingly.
api.func Documentation
Overview
The api.func file provides PocketBase API integration and diagnostic reporting for the Community Scripts project. It handles telemetry communication, error reporting, and status updates to the PocketBase backend at db.community-scripts.org.
Purpose and Use Cases
- API Communication: Send installation and status data to PocketBase
- Diagnostic Reporting: Report installation progress and errors for analytics
- Error Description: Provide detailed error code explanations (canonical source of truth)
- Status Updates: Track installation success/failure status
- Analytics: Contribute anonymous usage data for project improvement
Quick Reference
Key Function Groups
- Error Handling:
explain_exit_code()- Convert exit codes to human-readable messages - API Communication:
post_to_api(),post_to_api_vm()- Send installation data to PocketBase - Status Updates:
post_update_to_api()- Report installation completion status via PATCH
PocketBase Configuration
- URL:
http://db.community-scripts.org - Collection:
_dev_telemetry_data - API Endpoint:
/api/collections/_dev_telemetry_data/records
Dependencies
- External:
curlcommand for HTTP requests - Internal: Uses environment variables from other scripts
Integration Points
- Used by: All installation scripts for diagnostic reporting
- Uses: Environment variables from build.func and other scripts
- Provides: API communication, error reporting, and exit code descriptions
Documentation Files
📊 API_FLOWCHART.md
Visual execution flows showing API communication processes and error handling.
📚 API_FUNCTIONS_REFERENCE.md
Complete alphabetical reference of all functions with parameters, dependencies, and usage details.
💡 API_USAGE_EXAMPLES.md
Practical examples showing how to use API functions and common patterns.
🔗 API_INTEGRATION.md
How api.func integrates with other components and provides API services.
Key Features
Exit Code Descriptions
- Canonical source: Single authoritative
explain_exit_code()for the entire project - Non-overlapping ranges: Clean separation between error categories
- Comprehensive Coverage: 60+ error codes with detailed explanations
- System Errors: General system, curl, and network errors
- Signal Errors: Process termination and signal errors
PocketBase Integration
- Record Creation: POST to create telemetry records with status
installing - Record Updates: PATCH to update with final status, exit code, and error
- ID Tracking: Stores
PB_RECORD_IDfor efficient updates - Fallback Lookup: Searches by
random_idfilter if record ID is lost
Diagnostic Integration
- Optional Reporting: Only sends data when diagnostics enabled
- Privacy Respect: Respects user privacy settings
- Error Tracking: Tracks installation errors for improvement
- Usage Analytics: Contributes to project statistics
Common Usage Patterns
Basic API Setup
#!/usr/bin/env bash
source api.func
# Set up diagnostic reporting
export DIAGNOSTICS="yes"
export RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
# Report installation start (creates PocketBase record)
post_to_api
Error Reporting
#!/usr/bin/env bash
source api.func
# Get error description
error_msg=$(explain_exit_code 137)
echo "Error 137: $error_msg"
# Output: Error 137: Killed (SIGKILL / Out of memory?)
Status Updates
#!/usr/bin/env bash
source api.func
# Report successful installation
post_update_to_api "done" 0
# Report failed installation with exit code
post_update_to_api "failed" 127
Environment Variables
Required Variables
DIAGNOSTICS: Enable/disable diagnostic reporting ("yes"/"no")RANDOM_UUID: Unique identifier for session tracking
Optional Variables
CT_TYPE: Container type (1 for LXC, 2 for VM)DISK_SIZE: Disk size in GBCORE_COUNT: Number of CPU coresRAM_SIZE: RAM size in MBvar_os: Operating system typevar_version: OS versionNSAPP: Application nameMETHOD: Installation method
Internal Variables
POST_UPDATE_DONE: Prevents duplicate status updatesPB_URL: PocketBase base URLPB_API_URL: Full API endpoint URLPB_RECORD_ID: Stored PocketBase record ID for updates
Error Code Categories (Non-Overlapping Ranges)
| Range | Category |
|---|---|
| 1-2 | Generic shell errors |
| 6-35 | curl/wget network errors |
| 100-102 | APT/DPKG package errors |
| 124-143 | Command execution & signal errors |
| 150-154 | Systemd/service errors |
| 160-162 | Python/pip/uv errors |
| 170-173 | PostgreSQL errors |
| 180-183 | MySQL/MariaDB errors |
| 190-193 | MongoDB errors |
| 200-231 | Proxmox custom codes |
| 243-249 | Node.js/npm errors |
| 255 | DPKG fatal error |
Best Practices
Diagnostic Reporting
- Always check if diagnostics are enabled
- Respect user privacy settings
- Use unique identifiers for tracking
- Report both success and failure cases
Error Handling
- Use the correct non-overlapping exit code ranges
- Use
explain_exit_code()from api.func (canonical source) - Handle API communication failures gracefully
- Don't block installation on API failures
API Usage
- Check for curl availability before API calls
- Handle network failures gracefully (all calls use
|| true) - Store and reuse PB_RECORD_ID for updates
- Use proper PocketBase REST methods (POST for create, PATCH for update)
Troubleshooting
Common Issues
- API Communication Fails: Check network connectivity and curl availability
- Diagnostics Not Working: Verify
DIAGNOSTICS=yesin/usr/local/community-scripts/diagnostics - Status Update Fails: Check that
PB_RECORD_IDwas captured orrandom_idfilter works - Duplicate Updates:
POST_UPDATE_DONEflag prevents duplicates
Debug Mode
Enable diagnostic reporting for debugging:
export DIAGNOSTICS="yes"
export RANDOM_UUID="$(cat /proc/sys/kernel/random/uuid)"
API Testing
Test PocketBase connectivity:
curl -s http://db.community-scripts.org/api/health
Test record creation:
source api.func
export DIAGNOSTICS="yes"
export RANDOM_UUID="test-$(date +%s)"
export NSAPP="test"
export CT_TYPE=1
post_to_api
echo "Record ID: $PB_RECORD_ID"
Related Documentation
- core.func - Core utilities
- error_handler.func - Error handling (fallback
explain_exit_code) - build.func - Container creation with API integration
- tools.func - Extended utilities
This documentation covers the api.func file which provides PocketBase communication and diagnostic reporting for all Proxmox Community Scripts.