---
title: "Re-optimize a plan"
method: POST
path: "/plans/{planId}:reoptimize"
tags: ["Live Plans"]
---

# Re-optimize a plan

`POST /plans/{planId}:reoptimize`

## Request

- Base URL: `https://api.spoke.com/public/v1`
- URL: `https://api.spoke.com/public/v1/plans/{planId}:reoptimize`
- Auth: HTTP basic
- TypeScript: <https://skmtc.dev/spoke/apis/spoke-api/docs/plans/:planId:reoptimize/post.ts> (a function with typed inputs; its response is checked against the schema)

Re-optimize a plan. This endpoint should be used only after updating a live plan. Returns the created operation, which can be polled for the result. Use the returned `id` with the [operations](#tag/Operations) endpoints to poll for the result.

## Path parameters

- `planId` string, required

## Request body

- object: The request body for reoptimize a plan.
  - `optimizationType` 'reorder_changed_stops' | 'reorder_all_stops' | 'redistribute_stops_between_drivers': The type of optimization to use

## Response `200`

The created plan_optimization operation.

- OperationSchema
  - `id` string, required
  - `type` 'plan_optimization', required
  - `done` boolean, required: Whether the operation is done.
  - `metadata` object, required: Metadata related to a plan optimization operation.
    - `canceled` boolean, required: Whether the operation was canceled.
    - `startedAt` number, required: The time the operation started at, in seconds since epoch.
    - `finishedAt` number, nullable, required: The time the operation finished at, in seconds since epoch.
    - `startedBy` union, required: The entity that started the operation.
      - 'dispatcher'
      - 'api'
      - string
    - `targetPlanId` string, required
  - `result` union, required: The result of the plan optimization operation, or an error.
    - object
      - `numOptimizedStops` number, required: The number of stops that were considered for optimization.
      - `skippedStops` object[], required: The stops that were skipped, if any.
        - `id` string, required
        - `reason` union, required: The reason the stop was skipped.
          - 'impossible_time_window'
          - 'impossible_navigation'
          - 'impossible_number_of_stops'
          - 'impossible_order_of_stops'
          - string
    - object
      - `code` string, required: A code that identifies the error
      - `message` string, required: A human-readable message that describes the error. This message is not intended to be parsed by machines.

## Other responses

- `400`: ID format is invalid
- `401`: Unauthorized
- `403`: Plan is no longer accessible due to data access restrictions. Upgrade to a plan that supports a longer delivery history period to access it.
- `404`: The provided plan id does not exist
- `409`: The plan is not being edited, an optimization is already in progress, there are no drivers, or there is a problem with the depot.
- `500`: An internal server error occurred
- `default`: The default error model

---

[API](https://skmtc.dev/spoke/apis/spoke-api.md) - [All operations](https://skmtc.dev/spoke/apis/spoke-api/llms.txt) - [OpenAPI document](https://skmtc.dev/spoke/apis/spoke-api/revisions/a09d9194ae1c?raw)
