> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mileapp.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Create Activity

> Create a new Activity from a Workflow template. The system creates one parent task (`taskType = activityMaster`) plus one child task (`taskType = activityChild`) per Workflow step, in the order defined by the Workflow.

**Process:**
1. Validates that the referenced Workflow exists and belongs to your organization.
2. Creates the master task with the embedded Workflow reference.
3. Creates N child tasks sequentially based on `workflow.steps`. Each child task uses the `flow` and `flowId` of its corresponding Workflow step.
4. Links every child task to the master via `parentId`; the master tracks every child via its `subId` array, in Workflow-step order.
5. Aggregates the assignees from all child tasks onto the master.
6. Sets the master's `startTime` to the earliest child start time and `endTime` to the latest child end time.

**Notes:**
- The number of objects in `tasks` must equal the Workflow's number of steps.
- The master's status / sub-status updates happen automatically as child tasks progress — not via this endpoint. To update fields after creation, use the standard Task endpoints with the master task's `_id`.



## OpenAPI

````yaml /openapi/public/openapi-task.json post /activity
openapi: 3.0.0
info:
  title: MileApp API - Task
  version: 3.0.0
  description: MileApp API Documentation - RESTful API for field operations management.
servers:
  - url: https://apiweb.mile.app/api/v3
security:
  - bearerAuth: []
tags:
  - name: Task
    description: >-
      A task is an item of work intended for completion by an assignee. It can
      embody activities like pickups, deliveries, planning, inspections, sales
      visits, or other work-related actions within your organization. Tasks
      encompass essential attributes, including IDs, flow, time, status
      indicators, assignee information, order index, and other pertinent data.
      The content of a task is configurable based on your specified flow
      settings. For instance, tasks can serve as visits during the route
      optimization process, functioning as pivotal elements. The task can also
      represent a visit in route optimization, please follow [this
      link](#tag/Routing) to read more detail.


      **To see the details of task object, please follow [this
      link](#tag/task_model)**
  - name: Location History
    description: >-
      Location history is data that stores the user's location points that are
      sent periodically from the MileApp mobile application if the user
      activates the tracking feature. However, this endpoint can also be useful
      for integration into the user's IoT devices such as GPS or other mobile
      trackers. All location data sent can be visualized in MileApp in real time
      and makes it easier for users to monitor field activities more accurately.


      **Related Guide:** [Start Finish
      Feature](/configuration/start-trip-end-trip-feature)
  - name: Task Schedule
    description: >-
      Task Schedule is a mechanism to generate tasks according to the schedule
      that is set by you. The task schedule attributes consist of IDs,
      repetition period data (daily, weekly, etc.), flow structure, task
      content, and time of the schedule's start & end. This schedule could help
      you to set a task that will be repeated at a certain period.


      **To see the details of task schedule object, please follow [this
      link](#tag/task_schedule_model)**
  - name: Activity
    description: >-
      An Activity is a multi-step task group created from a Workflow template.
      When you create an Activity, MileApp automatically creates one parent task
      (with `taskType = activityMaster`) plus one child task (with `taskType =
      activityChild`) per Workflow step, and copies field values forward between
      steps according to the Workflow's field mappings. Activities are stored
      alongside regular tasks — the parent's `_id` is also the Activity ID, and
      its `subId` array lists every child task in Workflow-step order.


      After creation, retrieve, update, or delete an Activity through the
      standard Task endpoints using the Activity master task's ID.


      **To see the details of Activity object, please follow [this
      link](#tag/activity_model)**
paths:
  /activity:
    post:
      tags:
        - Activity
      summary: Create Activity
      description: >-
        Create a new Activity from a Workflow template. The system creates one
        parent task (`taskType = activityMaster`) plus one child task (`taskType
        = activityChild`) per Workflow step, in the order defined by the
        Workflow.


        **Process:**

        1. Validates that the referenced Workflow exists and belongs to your
        organization.

        2. Creates the master task with the embedded Workflow reference.

        3. Creates N child tasks sequentially based on `workflow.steps`. Each
        child task uses the `flow` and `flowId` of its corresponding Workflow
        step.

        4. Links every child task to the master via `parentId`; the master
        tracks every child via its `subId` array, in Workflow-step order.

        5. Aggregates the assignees from all child tasks onto the master.

        6. Sets the master's `startTime` to the earliest child start time and
        `endTime` to the latest child end time.


        **Notes:**

        - The number of objects in `tasks` must equal the Workflow's number of
        steps.

        - The master's status / sub-status updates happen automatically as child
        tasks progress — not via this endpoint. To update fields after creation,
        use the standard Task endpoints with the master task's `_id`.
      operationId: createActivity
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - workflowId
                - tasks
              properties:
                workflowId:
                  type: string
                  description: >-
                    ID of the Workflow that defines the steps for this Activity.
                    Must reference an existing Workflow in your organization.
                  example: 68f2198aef526585c30e16d2
                hubId:
                  type: string
                  description: >-
                    Optional hub ID where this Activity is executed. Inherited
                    by all child tasks.
                  example: 68ff42815ed967adac0f7192
                tasks:
                  type: array
                  description: >-
                    Array of step task objects. The array length must equal the
                    Workflow's number of steps; the order of objects matches the
                    order of `workflow.steps`. Each object carries the schedule,
                    assignee, and Flow-specific data fields for one Workflow
                    step.
                  items:
                    type: object
                    additionalProperties: true
                    properties:
                      assignee:
                        type: array
                        description: >-
                          Email(s) of the user(s) assigned to this step task.
                          Pass an empty array to leave the step unassigned.
                        items:
                          type: string
                        example:
                          - driver@example.com
                      startTime:
                        type: string
                        format: date-time
                        description: Start time of the step task, in ISO 8601 format.
                        example: '2025-10-31T08:55:00.000+07:00'
                      endTime:
                        type: string
                        format: date-time
                        description: End time of the step task, in ISO 8601 format.
                        example: '2025-11-01T08:55:00.000+07:00'
                      hubId:
                        type: string
                        description: >-
                          Hub ID for this step task. Usually the same as the
                          parent Activity's `hubId`.
                        example: 68ff42815ed967adac0f7192
                      createdFrom:
                        type: string
                        description: Source of creation, e.g. `WEB`, `API`, `MOBILE`.
                        example: WEB
                    description: >-
                      Each step task carries the schedule, assignee, and
                      Flow-specific data fields for one Workflow step. The
                      Flow-specific field names depend on the Flow referenced by
                      that step in the Workflow (for example `customerName`,
                      `customerAddress`, `recipientName`, `pickupAddress`,
                      etc.).
              example:
                workflowId: 68f2198aef526585c30e16d2
                hubId: 68ff42815ed967adac0f7192
                tasks:
                  - customerName: John Doe
                    customerAddress: Jl. Sudirman No. 1, Jakarta
                    pickupCoordinate: '-6.2088,106.8456'
                    assignee:
                      - driver@example.com
                    startTime: '2025-10-31T08:55:00.000+07:00'
                    endTime: '2025-11-01T08:55:00.000+07:00'
                    hubId: 68ff42815ed967adac0f7192
                    createdFrom: WEB
                  - recipientName: Jane Smith
                    recipientAddress: Jl. Pajajaran No. 10, Bogor
                    deliveryCoordinate: '-6.5950,106.8167'
                    assignee: []
                    startTime: '2025-10-31T08:55:00.000+07:00'
                    endTime: '2025-11-01T08:55:00.000+07:00'
                    hubId: 68ff42815ed967adac0f7192
                    createdFrom: WEB
      responses:
        '201':
          description: >-
            Activity created successfully. The response contains the master task
            object. `workflow.steps` is omitted from the response to reduce
            payload size — read the Workflow directly if you need the step
            definitions.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                    example: true
                  code:
                    type: integer
                    example: 201
                  message:
                    type: string
                    example: Activity created successfully
                  data:
                    type: object
                    description: >-
                      The newly-created Activity master task. The shape follows
                      the standard [Task
                      object](/api-reference/objects/task-object) with `taskType
                      = activityMaster`.
                    properties:
                      _id:
                        type: string
                        example: 634e98098ce07d29474a7e22
                      taskType:
                        type: string
                        example: activityMaster
                      title:
                        type: string
                        example: Pickup to Delivery
                      workflowId:
                        type: string
                        example: 68f2198aef526585c30e16d2
                      subId:
                        type: array
                        items:
                          type: string
                        example:
                          - 634e98098ce07d29474a7e23
                          - 634e98098ce07d29474a7e24
                      status:
                        type: string
                        example: ONGOING
                      assignee:
                        type: array
                        items:
                          type: string
                        example:
                          - driver@example.com
                      startTime:
                        type: string
                        format: date-time
                        example: '2025-10-31T08:55:00.000+07:00'
                      endTime:
                        type: string
                        format: date-time
                        example: '2025-11-01T08:55:00.000+07:00'
                      hubId:
                        type: string
                        example: 68ff42815ed967adac0f7192
                      organizationId:
                        type: string
                        example: 634e98098ce07d29474a7e20
                      createdBy:
                        type: string
                        example: admin@example.com
                      createdTime:
                        type: string
                        format: date-time
                        example: '2025-10-31T08:50:00.000+07:00'
        '400':
          description: >-
            Bad Request — validation failed (missing required fields, or `tasks`
            length does not match `workflow.steps.length`).
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                    example: false
                  code:
                    type: integer
                    example: 400
                  message:
                    type: string
                    example: Validation failed
        '404':
          description: >-
            Not Found — the referenced Workflow does not exist in your
            organization.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                    example: false
                  code:
                    type: integer
                    example: 404
                  message:
                    type: string
                    example: Workflow not found
        '500':
          description: Server Error — unexpected error while creating the Activity.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: boolean
                    example: false
                  code:
                    type: integer
                    example: 500
                  message:
                    type: string
                    example: Internal server error
      deprecated: false
      security:
        - bearerAuth: []
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
      description: Use a valid Bearer token to authenticate.

````