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

# Optimize Reroute

> Same as `reroute/generate`. The optional `operations_parameters` in the body is not used yet.

**Failure Modes:**
- **401**: Token is missing or invalid
- **400**: Rerouting is not enabled on the route, or it has no open operations
- **403**: Insufficient permissions, or you cannot manage the route's company
- **404**: No route with this ID, or its session was deleted

**Required Auth Scopes:**
- `routing-operations:manage`



## OpenAPI

````yaml /api-reference/openapi.json post /operations/routes/{route_id}/reroute/optimize
openapi: 3.1.0
info:
  title: SWITCH API
  version: '1.0'
  description: >-
    The SWITCH API gives mobility and logistics teams programmatic access to
    SWITCH forecasts, fleet operations, routing and analytics. Every request is
    authenticated with an API key sent as a bearer token.
servers:
  - url: https://stage-api.getswitch.io
    description: Stage
security: []
tags:
  - name: Forecast
    description: Demand forecasts per zone and time window.
  - name: Operations
    description: Fleet imbalance, rebalancing suggestions and generated tasks.
  - name: Routing
    description: Route optimisation for single and multiple fleets.
  - name: Routing Sessions
    description: Saved routing sessions, their routes and drivers.
  - name: Routing Operations
    description: Complete, undo and re-route the stops of a route.
  - name: Fleet Management
    description: Your depots, stations, drivers and cargo vehicles.
  - name: Integrations
    description: Vehicles, statuses and commands on your fleet platform.
  - name: Station Based Analytics
    description: Analytics for station-based (docked) services.
  - name: Zone Based Analytics
    description: Analytics for free-floating services, by zone.
  - name: Free Float Analytics
    description: Trip history for free-floating services.
  - name: Transit Analytics
    description: Punctuality and service delivery for public transit.
  - name: Live
    description: Real-time fleet, delivery and transit status.
  - name: Grids
    description: Spatial grids used by forecasts and analytics.
  - name: API Keys
    description: Create and manage the API keys you call this API with.
paths:
  /operations/routes/{route_id}/reroute/optimize:
    post:
      tags:
        - Routing Operations
      summary: Optimize Reroute
      description: >-
        Same as `reroute/generate`. The optional `operations_parameters` in the
        body is not used yet.


        **Failure Modes:**

        - **401**: Token is missing or invalid

        - **400**: Rerouting is not enabled on the route, or it has no open
        operations

        - **403**: Insufficient permissions, or you cannot manage the route's
        company

        - **404**: No route with this ID, or its session was deleted


        **Required Auth Scopes:**

        - `routing-operations:manage`
      operationId: optimize_reroute
      parameters:
        - name: route_id
          in: path
          required: true
          schema:
            type: integer
            description: Route ID, from the session detail or the route list.
            title: Route Id
          description: Route ID, from the session detail or the route list.
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RerouteRequest'
              title: Payload
      responses:
        '200':
          description: Optimized reroute preview. Returns proposed operations.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ReroutePreview'
        '400':
          description: Bad Request. Rerouting disabled or no pending ops.
        '401':
          description: Unauthorized. Token is missing or invalid.
        '403':
          description: Forbidden. Insufficient permissions.
        '404':
          description: Not Found. Route not found.
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
        '500':
          description: Internal Server Error.
      security:
        - HTTPBearer: []
        - HTTPBearer: []
components:
  schemas:
    RerouteRequest:
      properties:
        operations_parameters:
          additionalProperties: true
          type:
            - object
            - 'null'
          title: Operations Parameters
          description: >-
            Accepted for compatibility but not used yet: the plan is always
            built from the route's stored `routing_parameters`.
      type: object
      title: RerouteRequest
      description: Optional body for reroute generation and optimization.
      examples:
        - operations_parameters:
            strategies:
              - slug: battery-threshold
                strat_params:
                  threshold: 25
    ReroutePreview:
      properties:
        route_id:
          type: integer
          title: Route Id
          description: Route ID.
        kept_operations:
          type: integer
          title: Kept Operations
          description: Number of completed/cancelled operations kept in place.
        new_operations:
          items:
            $ref: '#/components/schemas/RerouteSuggestion'
          type: array
          title: New Operations
          description: Proposed operations for the remaining route.
        total_distance:
          type:
            - number
            - 'null'
          title: Total Distance
          description: Estimated total distance for the new plan (km).
        total_duration:
          type:
            - number
            - 'null'
          title: Total Duration
          description: Estimated total duration for the new plan (minutes).
      type: object
      required:
        - route_id
        - kept_operations
        - new_operations
      title: ReroutePreview
      description: >-
        Preview of a reroute result — proposed operations to replace pending
        ones.
      examples:
        - kept_operations: 1
          new_operations:
            - additional_notes: 'Zone surplus 1.0 vehicles (threshold: 1.5)'
              address: Via Mazzini 9, 20123 Milano MI
              estimated_arrive: '2026-11-30T08:21:00Z'
              latitude: 45.4671
              longitude: 9.1883
              provider: acme-mobility
              routing_parameters:
                capacity_delta:
                  vehicles: 1
                profit: 2.1
              step: 2
              task_type: pickup
          route_id: 412
          total_distance: 3.1
          total_duration: 21.5
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    RerouteSuggestion:
      properties:
        step:
          type: integer
          title: Step
          description: >-
            Position in the new plan, from 1; applied after the route's
            completed and cancelled operations.
        task_type:
          type: string
          title: Task Type
          description: Operation type, e.g. `pickup`.
        latitude:
          type: number
          title: Latitude
          description: Latitude of the stop (WGS84).
        longitude:
          type: number
          title: Longitude
          description: Longitude of the stop (WGS84).
        provider:
          type: string
          title: Provider
          description: Fleet the vehicle belongs to.
        address:
          type:
            - string
            - 'null'
          title: Address
          description: Street address, if known.
        vehicle_properties:
          additionalProperties: true
          type:
            - object
            - 'null'
          title: Vehicle Properties
          description: Snapshot of the vehicle, carried over from the route.
        estimated_arrive:
          type: string
          format: date-time
          title: Estimated Arrive
          description: Estimated arrival time in the new plan.
        routing_parameters:
          additionalProperties: true
          type: object
          title: Routing Parameters
          description: Free-form data of the stop (`capacity_delta`, `profit`).
        additional_notes:
          type:
            - string
            - 'null'
          title: Additional Notes
          description: Free-text notes.
      type: object
      required:
        - step
        - task_type
        - latitude
        - longitude
        - provider
        - estimated_arrive
      title: RerouteSuggestion
      description: A single operation in the proposed rerouted plan.
      examples:
        - additional_notes: 'Zone surplus 1.0 vehicles (threshold: 1.5)'
          address: Via Mazzini 9, 20123 Milano MI
          estimated_arrive: '2026-11-30T08:21:00Z'
          latitude: 45.4671
          longitude: 9.1883
          provider: acme-mobility
          routing_parameters:
            capacity_delta:
              vehicles: 1
            profit: 2.1
          step: 2
          task_type: pickup
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
  securitySchemes:
    HTTPBearer:
      type: http
      scheme: bearer
      description: >-
        An API key, sent as `Authorization: Bearer <key>`. Create one in the
        SWITCH Copilot under **Settings → API keys** (needs the API keys
        permission in your company), or with `POST /auth/api-keys`. The key is
        shown once, at creation.

````

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