Check Background Jobs

Monitor the status of asynchronous operations such as role assignments, cascade knowledge deletion, taxonomy publishing, and other workflow-backed operations. When an endpoint runs asynchronously, it returns a workflowId — pass it here to track progress.

Endpoint

POST /v1/workspaces/{workspaceId}/background-jobs

Request

Before you run the following snippets, see the API Key Authentication section on the Authentication page. From there, follow Step 1: Generate Access Token to get an access token. Run that code first, in the same shell session or script as the following snippets. The snippets also rely on the environment variables SHARELY_API_URL, SHARELY_API_KEY, SHARELY_WORKSPACE_ID, and SHARELY_ORGANIZATION_ID, which are described in the Complete Authentication Example.

curl -X POST \
  "$SHARELY_API_URL/v1/workspaces/$SHARELY_WORKSPACE_ID/background-jobs" \
  -H "Authorization: Bearer $SHARELY_ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  -H "organizationid: $SHARELY_ORGANIZATION_ID" \
  -d '{
    "jobIds": ["workflow-id-1", "workflow-id-2"]
  }'

Path Parameters

ParameterTypeRequiredDescription
workspaceIdstring (UUID)YesThe unique identifier of the workspace

Headers

HeaderTypeRequiredDescription
AuthorizationstringYesBearer token with access token
organizationidstring (UUID)YesYour organization ID
Content-TypestringYesMust be application/json

Body Parameters

ParameterTypeRequiredDescription
jobIdsarray of stringsNoWorkflow IDs to check (as returned by async endpoints). Omit to check all background jobs recorded for the workspace

Response

Success Response (200 OK)

Returns an array — one entry per matched job. Returns [] if no jobs match.

[
  {
    "workflowId": "workflow-id-1",
    "status": { "code": 2, "name": "COMPLETED" },
    "runId": "0190a1b2-c3d4-..."
  },
  {
    "workflowId": "workflow-id-2",
    "status": { "code": 1, "name": "RUNNING" },
    "runId": "0190a1b2-e5f6-..."
  }
]

Response Fields

FieldTypeDescription
workflowIdstringThe workflow ID you passed in (or that the async endpoint returned)
statusobjectWorkflow execution status
status.namestringOne of RUNNING, COMPLETED, FAILED, CANCELLED, TERMINATED, TIMED_OUT, CONTINUED_AS_NEW
status.codenumberNumeric status code corresponding to status.name
runIdstringThe underlying workflow run identifier (useful when contacting support)

Job Statuses

  • RUNNING — the job is currently being processed
  • COMPLETED — the job finished successfully; verify the resulting resource via its own GET endpoint
  • FAILED / TERMINATED / TIMED_OUT — the job did not complete; retry the originating operation or contact support with the workflowId and runId
  • CANCELLED — the job was cancelled before completion

Polling for Completion

The following snippets poll the endpoint with exponential backoff (up to 30 seconds between attempts) until every job reaches a terminal state. They use the same environment variables and access token as the previous snippets.

for attempt in $(seq 0 29); do
  jobs=$(curl -s -X POST \
    "$SHARELY_API_URL/v1/workspaces/$SHARELY_WORKSPACE_ID/background-jobs" \
    -H "Authorization: Bearer $SHARELY_ACCESS_TOKEN" \
    -H 'Content-Type: application/json' \
    -H "organizationid: $SHARELY_ORGANIZATION_ID" \
    -d '{"jobIds": ["workflow-id-1", "workflow-id-2"]}')
 
  # Count jobs that have not reached a terminal state
  pending=$(jq '[.[] | select(.status.name | IN("COMPLETED", "FAILED", "CANCELLED", "TERMINATED", "TIMED_OUT") | not)] | length' <<< "$jobs")
 
  if [ "$pending" -eq 0 ]; then
    echo "$jobs"
    break
  fi
 
  # Exponential backoff, capped at 30 seconds
  wait=$(( 2 ** attempt > 30 ? 30 : 2 ** attempt ))
  sleep "$wait"
done

Best Practices

  1. Poll Interval: Start with 1-2 second intervals, increase with exponential backoff
  2. Timeout: Set a reasonable timeout (30-60 seconds for most operations)
  3. Batch Checking: Check multiple workflow IDs in a single request
  4. Verify outcomes: The status tells you the workflow finished, not what it produced — confirm via the resource's own GET endpoint
  5. Keep IDs for support: Log workflowId and runId for failed jobs

Related Endpoints