POST /api/v1/sync
Synchronize offline changes with the server. Supports batch operations for form fills, inspections, photo deletions, and signature deletions.Authentication
Requires user authentication via Devise. Include authentication token in request headers.Request body
Array of sync items to process. Each item represents a change made offline.
Type of sync operation:
form_fill- Create or update form fill dataform_fill_update- Update form fill data (alias)inspection- Update inspection status/notesphoto_delete- Delete photo from form fillsignature_delete- Delete signature from form fill
Client-side identifier for tracking sync results
Payload data specific to the sync type
Form fill sync payload
Form fill ID (required for updates)
Client’s last updated timestamp for conflict detection
Complete form data hash (for full sync)
Partial changes to merge (for patch sync)
Conflict resolution strategy:
use_local to force local versionResponse
Overall sync success status
Sync results categorized by outcome
Server timestamp of sync completion
Summary message with counts
Example request
Example response
Conflict handling
When the server detects a version conflict (server’supdated_at is newer than client’s), it returns conflict data:
resolve_strategy: "use_local" to force the local version.
POST /api/v1/sync/upload_photo
Upload a photo from offline storage to a specific form fill field.Request parameters
ID of the form fill to attach the photo to
Name of the form field for the photo
Image file (JPEG, PNG). Maximum 10MB.
Response
Upload success status
Unique attachment ID for the uploaded photo
Success or error message
Example (multipart form data)
Error responses
Error message for validation failures:
"Required parameters: form_fill_id, field_name, photo""Not authorized to update this form""FormFill not found""File must be an image""File too large (max 10MB)"
GET /api/v1/sync/status
Check synchronization status and user statistics.Response
Request success status
Sync status data
Example response
Implementation details
Conflict resolution
The sync endpoint implements optimistic locking usingupdated_at timestamps:
- Client sends last known
updated_atwith changes - Server compares with current
updated_at - If server version is newer, return conflict data
- Client can resolve by:
- Showing user a merge UI
- Sending
resolve_strategy: "use_local"to force local version - Implementing automatic merge logic
Data merging strategy
Fromsync_controller.rb:182-211:
- Local changes override server values
- Server-only keys (e.g., photo attachment IDs) are preserved
- No data loss during sync
Authorization
All endpoints use Pundit policies to verify user access:- Users can only sync their own inspections
- Technicians can access assigned inspections
- Admins have full access
The sync endpoints are designed for offline-first mobile applications. They handle network interruptions gracefully and support batch operations for efficiency.
Best practices
Batch sync
Group multiple changes into a single sync request to minimize network requests
Conflict handling
Always implement conflict resolution UI for better user experience
Photo uploads
Upload photos separately after syncing form data to avoid timeout issues
Status checks
Poll sync status periodically to track pending changes
Error handling
Common error scenarios:| Error | Cause | Resolution |
|---|---|---|
| Version conflict | Server data changed | Implement conflict resolution |
| FormFill not found | ID mismatch or deleted | Re-download inspection data |
| Unauthorized | Missing/invalid auth | Re-authenticate user |
| File too large | Photo > 10MB | Compress before upload |
| Invalid sync type | Unknown type | Check API version |
Related
- Offline support - Full offline functionality guide
- FormFill model - Data storage structure
- Filling forms - User guide for form completion