Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Route Optimization API Google: Setup Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide walks you through using Google's Route Optimization API (formerly part of the Cloud Fleet Routing and now integrated into the Google Maps Platform) to plan efficient multi-stop routes. You'll learn how to set up the API, send a basic optimization request, and read the response to get turn-by-turn directions or a sequenced list of stops. This is useful for delivery fleets, field service teams, or anyone needing to visit many locations in the best order. Before you start, you'll need a Google Cloud project with billing enabled, because the API is not free—though it does offer a monthly credit for testing.

What You'll Need

  • A Google Cloud account (sign up at cloud.google.com).
  • A billing-enabled project—the Route Optimization API requires a credit card on file, even for the free tier.
  • The Google Maps Platform API key, created in the Cloud Console, with the Route Optimization API enabled.
  • Basic familiarity with JSON and making HTTP POST requests (using curl, Postman, or a programming language like Python).
  • Your stop data: at least two locations (origin and destination) with latitude/longitude coordinates or place IDs.

Step-by-Step

  1. Enable the API: Go to the Google Cloud Console → APIs & Services → Library. Search for "Route Optimization API" and click Enable. If you don't see it, you may need to enable the "Maps Platform" first.
  2. Create an API key: In the Console, go to APIs & Services → Credentials → Create Credentials → API Key. Copy the key—you'll use it in every request. For security, restrict the key to only the Route Optimization API (under "API restrictions").
  3. Understand the request format: The API uses a POST request to https://routeoptimization.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/optimizeTravel. Replace YOUR_PROJECT_ID with your actual Cloud project ID (found on the Console home page).
  4. Build your JSON payload: The core object is model , which contains vehicles and shipments . A minimal example: { "model": { "vehicles": [ { "vehicleIndex": 0, "startLocation": {"latitude": 37.7749, "longitude": -122.4194}, "endLocation": {"latitude": 37.7749, "longitude": -122.4194} } ], "shipments": [ { }, { } ] } } Each shipment needs both a pickup and delivery (they can be the same location if it's a drop-off only).
  5. Send the request: Using curl, run:
    curl -X POST \
      -H "Content-Type: application/json" \
      -H "X-Goog-Api-Key: YOUR_API_KEY" \
      -d @request.json \
      "https://routeoptimization.googleapis.com/v1/projects/YOUR_PROJECT_ID/locations/global/optimizeTravel"
    Save your JSON payload as request.json first.
  6. Read the response: The API returns a routes array. Each route has a visits list, where each visit references a shipment index and includes the arrival time and location. For example:
    {
      "routes": [
        {
          "vehicleIndex": 0,
          "visits": [
            {"shipmentIndex": 1, "arrivalTime": "2023-01-01T08:00:00Z"},
            {"shipmentIndex": 0, "arrivalTime": "2023-01-01T09:15:00Z"}
          ]
        }
      ]
    }
    The order of visits is the optimized sequence.
  7. Add time windows (optional): To respect business hours, add timeWindows to each pickup/delivery, e.g., "timeWindows": [{"startTime": "2023-01-01T09:00:00Z", "endTime": "2023-01-01T17:00:00Z"}]. The API will schedule within these windows.
  8. Handle multiple vehicles: Add more objects to the vehicles array, each with its own start/end location. The API will assign shipments to vehicles automatically.
  9. Get driving directions (optional): The Route Optimization API returns only the stop sequence, not turn-by-turn navigation. To get actual driving directions between stops, take the optimized order and feed it into the Directions API (with optimize:true if you want it to re-optimize, though that may differ from the route optimization result).

Common Mistakes

  • Using the wrong project ID: The URL requires your project ID, not your project name or number. Double-check from the Console dashboard.
  • Forgetting the API key header: The key goes in the X-Goog-Api-Key header, not in the URL query string (though the latter also works for some endpoints, it's not recommended).
  • Omitting deliveries: Every shipment must have at least one delivery. If you only have pickups, you'll get a validation error.
  • Not setting vehicle start/end: If a vehicle lacks a start location, the API assumes it can start anywhere, which may produce unexpected routes.
  • Ignoring time zone: All times in the request must be in UTC (with a Z suffix). Using local time without offset will cause errors or wrong scheduling.

Tips

  • Use place IDs instead of coordinates: The API accepts placeId in the location object, which can be more accurate for addresses.
  • Set a global duration limit: Add "globalEndTime" in the model to cap the total route time, preventing overly long solutions.
  • Test with the free monthly credit: Google gives $200/month free usage for Maps Platform APIs. Use that for development before scaling.
  • Batch requests: If you have many stops (e.g., 100+), split them into multiple requests to avoid hitting the 500-shipment limit per request.
  • Use the populatePolylines option: Set "populatePolylines": true in the request to get a polyline for each route, which you can draw on a map.

Troubleshooting

  • Error 403 (Forbidden): Your API key is either not enabled for Route Optimization, or the API is not enabled in your project. Go to the Library and re-check both.
  • Error 400 (Invalid JSON): Validate your payload with a JSON validator. Common issues: missing commas, extra braces, or using single quotes instead of double.
  • "Shipment has no delivery" error: Add at least one delivery object to every shipment, even if it's the same location as the pickup.
  • Response is empty or times out: For large models (many shipments/vehicles), the API can take 10-30 seconds. Increase your HTTP timeout to at least 60 seconds.
  • Unexpected route order: If the API returns a route that doesn't match your intuition, check if you've set time windows or vehicle capacities—these constraints can force a different order.
  • Billing not active: If you get a "billing required" error, go to your Cloud project's Billing section and link a valid credit card, then wait a few minutes for propagation.

Frequently Asked Questions

Q: Is the Route Optimization API the same as the Directions API? A: No. The Directions API gives turn-by-turn routes for a fixed order of stops. The Route Optimization API finds the best order to visit stops, considering time windows, vehicle capacity, and multiple vehicles.

Q: How many stops can I optimize in one request? A: The API supports up to 500 shipments per request, and up to 100 vehicles. For larger problems, you'd need to split or use a different service.

Q: Does it support real-time traffic? A: The Route Optimization API uses historical traffic patterns by default. For live traffic, you'd need to combine it with the Routes API or Directions API for the actual travel times.

Q: Can I use it for same-day delivery scheduling? A: Yes, but you must set time windows for each delivery. The API will assign arrival times within those windows, but it doesn't handle dynamic re-optimization as new orders come in—that requires your own logic.

Q: What's the cost? A: Pricing is per optimization request—around $0.10 per request for up to 100 shipments. There's a free monthly credit of $200 that covers roughly 2,000 requests. Check the official pricing page for current rates.

Q: Can I get driving directions in the response? A: Not directly. The response gives the stop order and times. To get road-level directions, feed the ordered stops into the Directions API separately.