Cloud Fleet Routing service is under specific service terms. See Service specific terms.
Stay organized with collections Save and categorize content based on your preferences.

Using the Command Line

This page shows you how to get started to solve a vehicle routing problem with the Cloud Optimization API using command line.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  4. Enable the Cloud Optimization API.

    Enable the API

  5. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  6. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  7. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  8. Install and initialize the Google Cloud CLI.
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  10. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  11. Enable the Cloud Optimization API.

    Enable the API

  12. Create a service account:

    1. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    2. Select your project.
    3. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    4. Click Create and continue.
    5. Click Done to finish creating the service account.

      Do not close your browser window. You will use it in the next step.

  13. Create a service account key:

    1. In the Google Cloud console, click the email address for the service account that you created.
    2. Click Keys.
    3. Click Add key, and then click Create new key.
    4. Click Create. A JSON key file is downloaded to your computer.
    5. Click Close.
  14. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key. This variable only applies to your current shell session, so if you open a new session, set the variable again.

  15. Install and initialize the Google Cloud CLI.

Make an optimize tours request

The following script sends the request in a file request.json to the :optimizeTours method of the Cloud Fleet Routing service.

The following shows an example request body:

{
  "parent": "projects/${YOUR_GCP_PROJECT_ID}",
  "model": {
    "shipments": [
      {
        "deliveries": [
          {
            "arrivalLocation": {
              "latitude": 48.880942,
              "longitude": 2.323866
            },
            "duration": "250s",
            "timeWindows": [
              {
                "endTime": "1970-01-01T01:06:40Z",
                "startTime": "1970-01-01T00:50:00Z"
              }
            ]
          }
        ],
        "loadDemands": {
          "weight": {
            "amount": "10"
          }
        },
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": 48.874507,
              "longitude": 2.30361
            },
            "duration": "150s",
            "timeWindows": [
              {
                "endTime": "1970-01-01T00:33:20Z",
                "startTime": "1970-01-01T00:16:40Z"
              }
            ]
          }
        ]
      },
      {
        "deliveries": [
          {
            "arrivalLocation": {
              "latitude": 48.88094,
              "longitude": 2.323844
            },
            "duration": "251s",
            "timeWindows": [
              {
                "endTime": "1970-01-01T01:06:41Z",
                "startTime": "1970-01-01T00:50:01Z"
              }
            ]
          }
        ],
        "loadDemands": {
          "weight": {
            "amount": "20"
          }
        },
        "pickups": [
          {
            "arrivalLocation": {
              "latitude": 48.880943,
              "longitude": 2.323867
            },
            "duration": "151s",
            "timeWindows": [
              {
                "endTime": "1970-01-01T00:33:21Z",
                "startTime": "1970-01-01T00:16:41Z"
              }
            ]
          }
        ]
      }
    ],
    "vehicles": [
      {
        "loadLimits": {
          "weight": {
            "maxLoad": 50
          }
        },
        "endLocation": {
          "latitude": 48.86311,
          "longitude": 2.341205
        },
        "startLocation": {
          "latitude": 48.863102,
          "longitude": 2.341204
        }
      },
      {
        "loadLimits": {
          "weight": {
            "maxLoad": 60
          }
        },
        "endLocation": {
          "latitude": 48.86312,
          "longitude": 2.341215
        },
        "startLocation": {
          "latitude": 48.863112,
          "longitude": 2.341214
        }
      }
    ]
  }
}

Command-line

Use curl to make a POST request to the optimizeTours method and provide the appropriate request body as shown in the following example.

Get your access token by entering gcloud auth print-access-token in your terminal. Save the JSON request file as request.json in the same directory.

Replace ${YOUR_GCP_PROJECT_ID} with your Google Cloud project ID in both the code and the request.

In your terminal, run the Curl script. Note that the command includes the name of the request file as an argument.

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(YOUR_ACCESS_TOKEN)" \
  -H "x-goog-user-project: ${YOUR_GCP_PROJECT_ID}" \
  -d @request.json \
  https://cloudoptimization.googleapis.com/v1/projects/${YOUR_GCP_PROJECT_ID}:optimizeTours

You should see a response similar to the following:

{
  "routes": [
    {
      "vehicleStartTime": "1970-01-01T00:02:11Z",
      "vehicleEndTime": "1970-01-01T01:15:34Z",
      "visits": [
        {
          "isPickup": true,
          "startTime": "1970-01-01T00:16:40Z",
          "detour": "0s",
          "arrivalLoads": [
            {
              "type": "weight"
            }
          ]
        },
        {
          "shipmentIndex": 1,
          "isPickup": true,
          "startTime": "1970-01-01T00:27:35Z",
          "detour": "725s",
          "arrivalLoads": [
            {
              "type": "weight",
              "value": "10"
            }
          ]
        },
        {
          "startTime": "1970-01-01T00:50:00Z",
          "detour": "1345s",
          "arrivalLoads": [
            {
              "type": "weight",
              "value": "30"
            }
          ]
        },
        {
          "shipmentIndex": 1,
          "startTime": "1970-01-01T00:58:43Z",
          "detour": "1444s",
          "arrivalLoads": [
            {
              "type": "weight",
              "value": "20"
            }
          ]
        }
      ],
      "travelSteps": [
        {
          "duration": "869s",
          "distanceMeters": 4243
        },
        {
          "duration": "505s",
          "distanceMeters": 2480
        },
        {
          "duration": "0s"
        },
        {
          "duration": "273s",
          "distanceMeters": 986
        },
        {
          "duration": "760s",
          "distanceMeters": 3099
        }
      ],
      "vehicleDetour": "4403s",
      "endLoads": [
        {
          "type": "weight"
        }
      ],
      "transitions": [
        {
          "travelDuration": "869s",
          "travelDistanceMeters": 4243,
          "loads": [
            {
              "type": "weight"
            }
          ]
        },
        {
          "travelDuration": "505s",
          "travelDistanceMeters": 2480,
          "loads": [
            {
              "type": "weight",
              "value": "10"
            }
          ]
        },
        {
          "travelDuration": "0s",
          "loads": [
            {
              "type": "weight",
              "value": "30"
            }
          ]
        },
        {
          "travelDuration": "273s",
          "travelDistanceMeters": 986,
          "loads": [
            {
              "type": "weight",
              "value": "20"
            }
          ]
        },
        {
          "travelDuration": "760s",
          "travelDistanceMeters": 3099,
          "loads": [
            {
              "type": "weight"
            }
          ]
        }
      ]
    },
    {
      "vehicleIndex": 1,
      "vehicleStartTime": "1970-01-01T00:00:00Z",
      "vehicleEndTime": "1970-01-01T00:00:00Z",
      "vehicleDetour": "0s"
    }
  ]
}

Clean up

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next