Synchronizes emails from Gmail into the DelightBridge database. Supports both full sync (all emails) and incremental sync (only new emails since last sync).
Authentication
Requires admin session. Only users with admin permission can trigger email synchronization.
This endpoint requires admin privileges and the service must be connected to Gmail via OAuth.
Path parameters
The unique identifier of the service to sync
Query parameters
mode
string
default: "incremental"
Sync mode: full or incremental
incremental - Only fetch new emails since last sync
full - Re-sync all emails from Gmail
Sync modes
Incremental sync (default)
Fetches only new emails since the last sync by using Gmail’s history API:
Uses stored lastHistoryId to determine starting point
More efficient for regular updates
Recommended for scheduled sync jobs
Faster execution time
Full sync
Re-downloads all emails from Gmail:
Ignores lastHistoryId
Fetches complete email history
Useful after connection issues or data corruption
Slower but ensures data consistency
Response
Whether the sync completed successfully
The sync mode that was used: full or incremental
Number of email threads processed (optional, implementation-dependent)
Number of individual messages processed (optional, implementation-dependent)
Response examples
200 OK - Incremental
200 OK - Full sync
401 Unauthorized
403 Forbidden
500 Sync error
{
"ok" : true ,
"mode" : "incremental" ,
"threadsProcessed" : 12 ,
"messagesProcessed" : 18
}
Code examples
cURL - Incremental
cURL - Full sync
TypeScript
JavaScript
curl --request POST \
--url 'https://your-domain.com/api/services/service-123/sync' \
--cookie 'authjs.session-token=YOUR_ADMIN_SESSION_TOKEN'
Implementation details
The sync process performs the following operations:
Authentication check : Validates admin session
Mode determination : Reads mode query parameter
Function routing : Calls syncAccountFull() or syncAccountIncremental()
Gmail API calls : Fetches emails using service’s OAuth tokens
Data transformation : Converts Gmail messages to database schema
Database updates : Inserts/updates threads and messages
History tracking : Updates lastHistoryId for incremental syncs
Gmail API integration
The sync functions use the Gmail API with the service’s stored OAuth tokens:
// Pseudocode of sync implementation
async function syncAccountIncremental ( accountId : string ) {
const account = await db . query . gmailAccounts . findFirst ({
where: eq ( gmailAccounts . id , accountId )
});
const gmail = google . gmail ({
version: 'v1' ,
auth: oauthClient . setCredentials ({
access_token: account . accessToken ,
refresh_token: account . refreshToken ,
}),
});
const response = await gmail . users . history . list ({
userId: 'me' ,
startHistoryId: account . lastHistoryId ,
});
// Process and store new messages
// ...
}
Error handling
Common errors:
Service not connected : No refresh token in database
Token expired : Automatic refresh attempted
Token refresh failed : OAuth connection needs to be re-established
Gmail API quota exceeded : Rate limiting in effect
Network errors : Temporary Gmail API unavailability
If sync fails due to invalid OAuth tokens, reconnect the service using the Connect endpoint .
Incremental sync : Typically completes in 1-5 seconds
Full sync : Can take several minutes for accounts with large email volumes
Rate limits : Gmail API has quota limits; avoid frequent full syncs
Concurrency : Do not sync the same service simultaneously
Schedule incremental syncs every 5-15 minutes for near real-time email updates. Use full sync only when necessary.
Next steps
After syncing emails:
View email threads using GET /api/threads
Generate AI drafts with POST /api/drafts/generate
Send responses through POST /api/threads/send