Skip to main content

Start Historical Sync

Creates a new historical sync job that will import past call data from the Ultravox API into the local database. The job is created in a pending state and will be picked up by the process-historical-sync cron function.

Endpoint

PropertyValue
Function Namestart-historical-sync
HTTP MethodPOST
AuthenticationSupabase JWT (Bearer token in Authorization header)
Required RoleAny authenticated agency user

Request

Headers

HeaderRequiredDescription
AuthorizationYesBearer <supabase_jwt>
Content-TypeYesapplication/json

Body

FieldTypeRequiredDescription
date_fromstring (ISO 8601)NoStart of the date range to sync. If omitted, no lower bound is applied.
date_tostring (ISO 8601)NoEnd of the date range to sync. Defaults to the current timestamp if omitted.
{
"date_from": "2025-01-01T00:00:00Z",
"date_to": "2025-03-01T00:00:00Z"
}

Response

Success (200)

{
"job_id": "uuid-of-the-created-job",
"status": "pending"
}

Error Responses

StatusCondition
401Missing or invalid Authorization header / JWT token
403Authenticated user is not associated with an agency (agency_id is null)
405HTTP method is not POST
409A sync job is already in progress for the agency (duplicate job conflict from create_historical_sync_job RPC)
500Unexpected server error or RPC failure

Behavior

  • Handles CORS preflight (OPTIONS) requests and rejects non-POST methods with a 405.
  • Validates the JWT token via supabase.auth.getUser() to identify the caller.
  • Looks up the user's agency_id and role from the users table.
  • Calls the create_historical_sync_job database RPC with the agency ID and requested date range.
  • If the RPC returns an error whose message includes "already exists", the function returns a 409 Conflict indicating a job is already running.
  • On success, returns the newly created job ID and a pending status.

Database Tables

Table / RPCOperationDescription
usersReadFetches agency_id and role for the authenticated user
create_historical_sync_job (RPC)WriteCreates a new sync job record in historical_sync_jobs

External APIs

None. This function only creates the job record; actual Ultravox API calls happen in process-historical-sync.