---
title: Optimize a plan
description: Optimize a plan. Returns the created operation, which can be polled
  for the result. Use the returned `id` with the [operations](/api/operations)
  endpoints to poll for the result.
url: /api/plans/optimizePlan
---

# Optimize a plan

`POST /plans/{planId}:optimize`

````yaml
openapi: 3.1.0
info:
  title: Spoke API
  version: v1
servers:
  - url: https://api.spoke.com/public/v1
security:
  - BasicAuth: []
paths:
  /plans/{planId}:optimize:
    post:
      operationId: optimizePlan
      summary: Optimize a plan
      tags:
        - Plans
      description: Optimize a plan. Returns the created operation, which can be polled for the result. Use the returned `id` with the [operations](/api/operations) endpoints to poll for the result.
      parameters:
        - schema:
            type: string
            pattern: ^[a-zA-Z0-9---_]{1,50}$
          in: path
          name: planId
          required: true
          description: The plan id
      responses:
        "200":
          description: The created plan_optimization operation.
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/operationSchema"
                description: The created plan_optimization operation.
        "400":
          description: ID format is invalid
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: The error message.
                  code:
                    type: string
                    description: The error code.
                  param:
                    type: string
                    description: The parameter that caused the error.
                  url:
                    type: string
                    description: The URL with more information about the error.
                required:
                  - message
                description: ID format is invalid
                title: The request is invalid
        "401":
          description: Unauthorized
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: The error message.
                  url:
                    type: string
                    description: The URL with more information about the error.
                required:
                  - message
                description: Unauthorized
        "403":
          description: Plan is no longer accessible due to data access restrictions. Upgrade to a plan that supports a longer delivery history period to access it.
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    enum:
                      - Plan is no longer accessible due to data access restrictions. Upgrade to a plan that supports a longer delivery history period to access it.
                  code:
                    type: string
                    enum:
                      - plan_inaccessible
                  url:
                    type: string
                    enum:
                      - https://dispatch.spoke.com/paywall
                required:
                  - message
                  - code
                  - url
                description: 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":
          description: The provided plan id does not exist
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    enum:
                      - Plan not found
                required:
                  - message
                description: The provided plan id does not exist
                title: Plan not found
        "409":
          description: The plan is not writable, an optimization is already in progress, there are no drivers, or there is a problem with the depot.
          content:
            application/json:
              schema:
                oneOf:
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Plan already optimized
                      code:
                        type: string
                        enum:
                          - plan_already_optimized
                    required:
                      - message
                      - code
                    title: Plan already optimized
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Plan optimization in progress
                      code:
                        type: string
                        enum:
                          - plan_optimization_in_progress
                    required:
                      - message
                      - code
                    description: The plan's optimization is in progress.
                    title: Plan optimization in progress
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Plan has no drivers. Either the plan's depot has no drivers, if the optimization is for minimizing drivers, or the plan has no assigned drivers.
                      code:
                        type: string
                        enum:
                          - no_drivers_available
                    required:
                      - message
                      - code
                    description: The plan has no available drivers.
                    title: Plan has no drivers
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Please set an end time or max stops per driver for the plan's depot when optimizing to minimize drivers.
                      code:
                        type: string
                        enum:
                          - no_end_time_and_max_stops_per_driver
                    required:
                      - message
                      - code
                    description: The plan's depot has no end time and max stops per driver.
                    title: Plan's depot has no end time and max stops per driver
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Depot not found
                      code:
                        type: string
                        enum:
                          - depot_not_found
                    required:
                      - message
                      - code
                    description: Depot not found
                    title: Depot not found
                    x-response-description: Depot not found
                  - type: object
                    properties:
                      message:
                        type: string
                        enum:
                          - Please set a start location for the plan's depot.
                      code:
                        type: string
                        enum:
                          - depot_missing_start_address
                    required:
                      - message
                      - code
                    description: Depot not found
                    title: Plan's depot missing start address
                description: The plan is not writable, an optimization is already in progress, there are no drivers, or there is a problem with the depot.
        "500":
          description: An internal server error occurred
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: The error message.
                  code:
                    type: string
                    description: The error code.
                  param:
                    type: string
                    description: The parameter that caused the error.
                  url:
                    type: string
                    description: The URL with more information about the error.
                required:
                  - message
                description: An internal server error occurred
        default:
          description: The default error model
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                    description: The error message.
                  code:
                    type: string
                    description: The error code.
                  param:
                    type: string
                    description: The parameter that caused the error.
                  url:
                    type: string
                    description: The URL with more information about the error.
                required:
                  - message
                description: The default error model
components:
  schemas:
    operationIdSchema:
      type: string
      pattern: ^operations\/[a-zA-Z0-9---_]{1,50}$
    operationSchema:
      type: object
      properties:
        id:
          allOf:
            - $ref: "#/components/schemas/operationIdSchema"
          description: The id of the operation, in the format `operations/<id>`.
        type:
          type: string
          enum:
            - plan_optimization
        done:
          type: boolean
          description: Whether the operation is done.
        metadata:
          type: object
          properties:
            canceled:
              type: boolean
              description: Whether the operation was canceled.
            startedAt:
              type: number
              description: The time the operation started at, in seconds since epoch.
            finishedAt:
              description: The time the operation finished at, in seconds since epoch.
              type:
                - "null"
                - number
            startedBy:
              anyOf:
                - type: string
                  enum:
                    - dispatcher
                - type: string
                  enum:
                    - api
                - type: string
              description: The entity that started the operation.
            targetPlanId:
              allOf:
                - $ref: "#/components/schemas/planIdSchema"
              description: The id of the plan, in the format `plans/<id>`.
          required:
            - canceled
            - startedAt
            - finishedAt
            - startedBy
            - targetPlanId
          description: Metadata related to a plan optimization operation.
        result:
          anyOf:
            - anyOf:
                - type: object
                  properties:
                    numOptimizedStops:
                      type: number
                      description: The number of stops that were considered for optimization.
                    skippedStops:
                      type: array
                      items:
                        type: object
                        properties:
                          id:
                            allOf:
                              - $ref: "#/components/schemas/stopIdSchema"
                            description: The id of the stop, in the format `plans/<id>/stops/<id>`.
                          reason:
                            anyOf:
                              - type: string
                                enum:
                                  - impossible_time_window
                              - type: string
                                enum:
                                  - impossible_navigation
                              - type: string
                                enum:
                                  - impossible_number_of_stops
                              - type: string
                                enum:
                                  - impossible_order_of_stops
                              - type: string
                            description: The reason the stop was skipped.
                        required:
                          - id
                          - reason
                      description: The stops that were skipped, if any.
                  required:
                    - numOptimizedStops
                    - skippedStops
                - type: object
                  properties:
                    code:
                      type: string
                      description: A code that identifies the error
                    message:
                      type: string
                      description: A human-readable message that describes the error. This message is not intended to be parsed by machines.
                  required:
                    - code
                    - message
            - type: "null"
          description: The result of the plan optimization operation, or an error.
      required:
        - id
        - type
        - done
        - metadata
        - result
    planIdSchema:
      type: string
      pattern: ^plans\/[a-zA-Z0-9---_]{1,50}$
    stopIdSchema:
      type: string
      pattern: ^plans\/[a-zA-Z0-9---_]{1,50}\/stops\/[a-zA-Z0-9---_]{1,50}$
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic
      description: Use the API key as the username and leave the password empty.
````