Using the Client Libraries
This page shows you how to get started with the Cloud Optimization API in your favorite programming language.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Optimization API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Optimization API.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
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
. - Click Create and continue.
-
Grant the Project > Owner role to the service account.
To grant the role, find the Select a role list, then select Project > Owner.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
Install the client library
C++
To authenticate to Cloud Optimization, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
See Setting up a C++ development environment for details about this client library's requirements and install dependencies.
Java
For more on setting up your Java development environment, refer to the Java Development Environment Setup Guide.
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
Python
For more on setting up your Python development environment, refer to the Python Development Environment Setup Guide.
pip install --upgrade google-cloud-optimization
Solving a vehicle routing problem
Now you can use the Cloud Optimization API to solve a vehicle routing problem and get optimized routes.
Prepare your OptimizeToursRequest
in a separate file or construct it in the code. The example below stores the request as a JSON file.
{ "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 }, "costPerTraveledHour": 3600 }, { "loadLimits": { "weight": { "maxLoad": 60 } }, "endLocation": { "latitude": 48.86312, "longitude": 2.341215 }, "startLocation": { "latitude": 48.863112, "longitude": 2.341214 }, "costPerTraveledHour": 3600 } ] } }
Now, run the following code to perform your first call to the Cloud Fleet Routing service.
C++
To authenticate to Cloud Optimization, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
To authenticate to Cloud Optimization, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
To authenticate to Cloud Optimization, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
How did it go?
Clean up
To avoid incurring charges to your Google Account for the resources used in this quickstart:
- Use the Google Cloud console to delete your project if you do not need it.
What's next
Find out more about our Cloud Optimization API Client Libraries.