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

# Plan a route

> POST /dn/routes/plan creates a route or updates an existing one by name and date, then returns the optimized plan with sequenced jobs.

Use this endpoint to build a new route or overwrite an existing route that shares the same `name` and `date`. Detrack runs an optimization engine to sequence the supplied `job_ids`, compute ETAs, and return totals such as distance and duration. If the route already exists, it is replaced with the newly computed plan.

<Warning>
  **Known issues:**

  * If you re-plan with the same `name` and `date` and the request fails validation, the existing plan has already been deleted and isn't replaced. Check your request before re-planning ([DTR-6207](https://detrack.atlassian.net/browse/DTR-6207)).
  * Each name in `vehicles` matches any vehicle whose name contains it, ignoring case. For example, `Driver 1` also selects `Driver 10`. Use vehicle names that aren't contained in other names ([DTR-6206](https://detrack.atlassian.net/browse/DTR-6206)).
</Warning>

<Note>
  Vehicle working hours (`from` and `to`) are configured per driver inside the Detrack app; they are not part of the API request payload.
</Note>

Each route has two levels:

* **The plan** (the top-level object) holds the plan's `status`, depot, and totals across all vehicles.
* **`planned_routes`** has one entry per vehicle, with that vehicle's `status`, `start_time`, `completion_time`, time window, and its `planned_jobs` in delivery order (`job_sequence`, `eta_time`).

Jobs that couldn't be placed on any vehicle are in the plan-level `unplanned_jobs`. The plan-level `vehicle` shows one of the plan's vehicles; use `planned_routes[].vehicle` to see which vehicle drives which route. See [Route status](/concepts/routes#route-status) for what each `status` value means.

## Errors

Validation errors return `422` with a `code` and `message`. The API checks the request in the order below and stops at the first failure.

| `code` | When |
| - | - |
| `invalid_data` | The request body has no `data` object. |
| `missing_name_or_date` | `data.name` or `data.date` is missing. |
| `missing_jobs` | `data.job_ids` is missing or empty. |
| `missing_vehicles` | `data.vehicles` is missing or empty. |
| `error_vehicles` | `data.vehicles` isn't an array. |
| `no_vehicles_found` | None of the names in `data.vehicles` match a vehicle in your account. |
| `validation_failed` | The plan couldn't be saved. The `errors` array lists each failing `field` and its `codes`. |

```json theme={null}
{
  "code": "no_vehicles_found",
  "message": "No Vehicles Found"
}
```

See [Errors](/errors) for other status codes.


## OpenAPI

````yaml POST /dn/routes/plan
openapi: 3.0.3
info:
  title: Detrack REST API v2
  version: '2.0'
  description: >-
    Detrack REST API v2. Checked against detrack-webapp origin/main (production)
    on 2026-09-29: dn_jobs.rb @ d246ad006, dn_depots.rb @ 2a5f387, dn_routes.rb
    @ 50a73a9, app_helper.rb @ d745ad2, dn_jobs_helper.rb @ d296e30,
    job_decorator.rb @ 3041da7, run_location_decorator.rb @ 0f0274d,
    vehicle_route_plan_decorator.rb and tsp_route_decorator.rb @ 7d1f3ef,
    data_export.rb @ ed4e135.
servers:
  - url: https://app.detrack.com/api/v2
security:
  - apiKey: []
tags:
  - name: Jobs
    description: Deliveries and collections.
  - name: Depots
    description: Saved warehouse and pickup locations.
  - name: Routes
    description: Optimized multi-stop plans across vehicles.
paths:
  /dn/routes/plan:
    post:
      tags:
        - Routes
      summary: Plan a route
      description: >-
        Creates a route plan, or replaces the plan with the same `name` and
        `date`, then optimizes it. The existing plan is currently deleted before
        the request is validated, so a `422` still removes it (DTR-6207).
        Returns `200` with the plan in `data` when optimization has finished, or
        `202` with the plan unwrapped while it's still running.
      operationId: planRoute
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - data
              properties:
                data:
                  type: object
                  required:
                    - name
                    - date
                    - vehicles
                    - job_ids
                  properties:
                    name:
                      type: string
                      description: Route name. Must be unique per date.
                      example: sample_run
                    date:
                      type: string
                      format: date
                      example: '2026-09-15'
                    vehicles:
                      type: array
                      minItems: 1
                      items:
                        type: string
                      description: >-
                        Names of the vehicles to plan across. Each name
                        currently matches any vehicle whose name contains it,
                        ignoring case (DTR-6206).
                      example:
                        - Driver 1
                        - Driver 2
                    job_ids:
                      type: array
                      minItems: 1
                      items:
                        type: string
                      description: Detrack job IDs to plan.
                    depot_location:
                      description: Start depot. Required when `manual_depot` is `true`.
                      allOf:
                        - $ref: '#/components/schemas/DepotLocationRef'
                    end_depot_location:
                      description: End depot, in the same format as `depot_location`.
                      allOf:
                        - $ref: '#/components/schemas/DepotLocationRef'
                    mode:
                      type: string
                      enum:
                        - drive
                        - light_truck
                        - medium_truck
                        - truck
                        - heavy_truck
                      default: drive
                    manual_time_windows:
                      type: boolean
                      default: false
                      description: >-
                        Set to `true` to use `time_window_from` and
                        `time_window_to` for the whole plan. When `false`, each
                        vehicle's own working hours are used, and every vehicle
                        must have them set.
                    time_window_from:
                      type: string
                      description: >-
                        `HH:MM`, 24-hour. Required and used only when
                        `manual_time_windows` is `true`.
                      example: '07:00'
                    time_window_to:
                      type: string
                      description: >-
                        `HH:MM`, 24-hour. Required and used only when
                        `manual_time_windows` is `true`.
                      example: '23:50'
                    traffic:
                      type: string
                      default: free_flow
                      description: >-
                        Traffic model for the routing engine: `free_flow`
                        (default) or `approximated`.
                    max_speed:
                      type: number
                      description: >-
                        Maximum vehicle speed, in your account's distance unit:
                        km/h for accounts that use kilometres, mph for accounts
                        that use miles (converted to km/h for the routing
                        engine).
                    service_time:
                      type: integer
                      description: Minutes per stop.
                    manual_service_time:
                      type: boolean
                      default: false
                    avoid_toll:
                      type: boolean
                      default: false
                    manual_depot:
                      type: boolean
                      default: false
                    manual_end_depot:
                      type: boolean
                      default: false
                    retain_assignments:
                      type: boolean
                      default: false
                    assign_route:
                      type: boolean
                      default: false
                      description: >-
                        Apply the plan to the jobs: each job gets its sequence,
                        ETA, run number, and driver, and its `status` becomes
                        `dispatched` (`out_for_delivery` in courier mode).
                        Completed, partially completed, and failed jobs aren't
                        changed.
                    vrp_exclude_send_driver:
                      type: boolean
                      default: false
                      description: >-
                        With `assign_route`, jobs get their sequence, ETA, and
                        run number, but no driver, and their `status` doesn't
                        change.
      responses:
        '200':
          description: Optimization finished.
          content:
            application/json:
              schema:
                type: object
                properties:
                  data:
                    $ref: '#/components/schemas/RoutePlan'
        '202':
          description: >-
            Saved, but still optimizing. The plan is **not** wrapped in `data`,
            and has `jobs` instead of `planned_routes`.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RoutePlan'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '403':
          $ref: '#/components/responses/Forbidden'
        '422':
          description: >-
            Validation failed. `code` is one of `invalid_data`,
            `missing_name_or_date`, `missing_jobs`, `missing_vehicles`,
            `error_vehicles`, `no_vehicles_found`, or `validation_failed`. Only
            `validation_failed` includes an `errors` array.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidationError'
              example:
                code: no_vehicles_found
                message: No Vehicles Found
        default:
          $ref: '#/components/responses/ServerError'
components:
  schemas:
    DepotLocationRef:
      description: >-
        A saved depot by `id`, or a depot by `name` and `address`. **By name,
        Detrack saves it as a depot: it updates the depot with that name, or
        creates one.**
      oneOf:
        - type: object
          title: Saved depot
          required:
            - id
          properties:
            id:
              type: string
              example: 674ecabbe8ec54052f7ead41
        - allOf:
            - $ref: '#/components/schemas/DepotInput'
          title: Depot by name
    RoutePlan:
      type: object
      properties:
        id:
          type: string
          example: 69001c6de8ec5410e2570bb3
        name:
          type: string
          example: sample_run
        date:
          type: string
          format: date
          example: '2026-09-15'
        mode:
          type: string
          example: drive
        status:
          type: string
          description: >-
            Plan status: `new` or `routing_in_progress` while optimizing, then
            `to_be_confirm`, `routing_completed`, or `routing_failed`. See
            [Route status](/concepts/routes#route-status).
          example: to_be_confirm
        message:
          type: string
          nullable: true
        vehicle:
          type: object
          additionalProperties: true
          description: >-
            One of the plan's vehicles. Use `planned_routes[].vehicle` to see
            which vehicle drives which route.
        job_ids:
          type: array
          items:
            type: string
        depot_location:
          $ref: '#/components/schemas/Depot'
        end_depot_location:
          type: object
          allOf:
            - $ref: '#/components/schemas/Depot'
          nullable: true
        total_distance:
          type: number
          description: Meters, across all vehicles.
          example: 15107
        total_time:
          type: number
          description: Seconds, across all vehicles.
          example: 4373.497
        total_planned:
          type: integer
          example: 3
        total_unplanned:
          type: integer
          example: 0
        type:
          type: string
          example: balanced
        jobs:
          type: array
          description: >-
            Only while the plan is still optimizing (`new` or
            `routing_in_progress`): the jobs being planned, in place of
            `planned_routes`.
          items:
            $ref: '#/components/schemas/PlannedJob'
        planned_routes:
          type: array
          description: One entry per vehicle, once optimization has finished.
          items:
            $ref: '#/components/schemas/VehicleRoute'
        unplanned_jobs:
          type: array
          description: Jobs that couldn't be placed on any vehicle.
          items:
            $ref: '#/components/schemas/PlannedJob'
    ValidationError:
      type: object
      properties:
        code:
          type: string
          example: validation_failed
        message:
          type: string
          example: Validation Failed
        errors:
          type: array
          items:
            $ref: '#/components/schemas/FieldError'
    DepotInput:
      type: object
      required:
        - name
        - address
      properties:
        name:
          type: string
          description: Depot name. An existing depot with the same name is updated.
          example: Main Depot
        address:
          type: string
          description: Full address that can be geocoded.
          example: SM Mall of Asia, Seaside Blvd, Pasay, Metro Manila
        addr_1:
          type: string
        addr_2:
          type: string
        addr_3:
          type: string
        zip_code:
          type: string
        city:
          type: string
        state:
          type: string
    Depot:
      type: object
      properties:
        id:
          type: string
          readOnly: true
          example: 68f0d416e8ec54cbf4a04dba
        name:
          type: string
          example: Main Depot
        address:
          type: string
          example: >-
            SM Megamall, EDSA corner Doña Julia Vargas Ave, Mandaluyong, 1550
            Metro Manila
        addr_1:
          type: string
          nullable: true
        addr_2:
          type: string
          nullable: true
        addr_3:
          type: string
          nullable: true
        zip_code:
          type: string
          nullable: true
        country:
          type: string
          nullable: true
        city:
          type: string
          nullable: true
        state:
          type: string
          nullable: true
        country_id:
          type: string
          nullable: true
          readOnly: true
        location:
          type: array
          readOnly: true
          description: Coordinates as `[longitude, latitude]`.
          items:
            type: number
          example:
            - 121.055626
            - 14.583941
        address_lat:
          type: number
          readOnly: true
          example: 14.583941
        address_lng:
          type: number
          readOnly: true
          example: 121.055626
    PlannedJob:
      type: object
      properties:
        id:
          type: string
          example: 68e0bac7e8ec545357500616
        type:
          type: string
          example: Delivery
        status:
          type: string
          example: dispatched
        do_number:
          type: string
          example: DO-10001
        address:
          type: string
          example: Vicente Sotto St, CCP Complex, Pasay 1307
        deliver_to_collect_from:
          type: string
          nullable: true
        phone_number:
          type: string
          nullable: true
        time_window_from:
          type: string
          nullable: true
          example: '09:00'
        time_window_to:
          type: string
          nullable: true
          example: '12:00'
        job_sequence:
          type: integer
          example: 1
        eta_time:
          type: string
          example: '07:05'
        run_number:
          type: string
          example: '1'
        service_time:
          type: integer
          nullable: true
          example: 10
        geocoded_lat:
          type: number
          example: 14.5542
        geocoded_lng:
          type: number
          example: 120.9839
        has_latlng:
          type: boolean
          example: true
    VehicleRoute:
      type: object
      description: One vehicle's route within a plan.
      properties:
        id:
          type: string
          example: 69001c6fe8ec5410e2570bc0
        vrp_id:
          type: string
          example: 69001c6de8ec5410e2570bb3
        name:
          type: string
          example: '1'
        date:
          type: string
          format: date
          example: '2026-09-15'
        status:
          type: string
          description: >-
            Vehicle route status: `to_be_confirm`, `route_assigned`,
            `route_assigned_without_driver`, or `unplanned_jobs`. See [Route
            status](/concepts/routes#route-status).
          example: to_be_confirm
        vehicle:
          type: object
          additionalProperties: true
          description: The vehicle and its driver details.
        time_window_from:
          type: string
          example: '07:00'
        time_window_to:
          type: string
          example: '23:50'
        start_time:
          type: string
          example: '07:00'
        completion_time:
          type: string
          example: '08:12'
        total_distance:
          type: number
          description: Meters.
          example: 15107
        total_time:
          type: number
          description: Seconds.
          example: 4373.497
        total_planned:
          type: integer
          example: 3
        total_unplanned:
          type: integer
          example: 0
        planned_jobs:
          type: array
          description: Jobs in delivery order.
          items:
            $ref: '#/components/schemas/PlannedJob'
        unplanned_jobs:
          type: array
          items:
            $ref: '#/components/schemas/PlannedJob'
    Error:
      type: object
      properties:
        code:
          type: string
          example: not_found
        message:
          type: string
          example: Could not find job with this DO number
    FieldError:
      type: object
      properties:
        field:
          type: string
          example: do_number
        codes:
          type: array
          items:
            type: string
          example:
            - can't be blank
  responses:
    BadRequest:
      description: The request body isn't valid JSON.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: invalid_json_format
    Unauthorized:
      description: >-
        The API key is missing or invalid. A missing `X-API-KEY` returns `{
        "error": "Missing Authorization header" }`; an invalid key returns `{
        "code": "invalid_key", "message": "Invalid key" }`.
      content:
        application/json:
          schema:
            oneOf:
              - type: object
                title: Missing key
                required:
                  - error
                properties:
                  error:
                    type: string
                    example: Missing Authorization header
              - type: object
                title: Invalid key
                required:
                  - code
                properties:
                  code:
                    type: string
                    example: invalid_key
                  message:
                    type: string
                    example: Invalid key
          example:
            code: invalid_key
            message: Invalid key
    Forbidden:
      description: Your API key's user doesn't have permission for this action.
      content:
        application/json:
          schema:
            type: object
            properties:
              code:
                type: string
                example: forbidden
          example:
            code: forbidden
    ServerError:
      description: >-
        Unexpected server error. Retry with exponential backoff, and contact
        Detrack support if it persists.
  securitySchemes:
    apiKey:
      type: apiKey
      in: header
      name: X-API-KEY
      description: Your Detrack API key, from **Integrations > API Key** in the dashboard.

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.