Migrate the Directions API and DirectionsService to the Routes API
DirectionsService, DistanceMatrixService and the Directions and Distance Matrix web services are legacy products. They still work on projects that already use them, but new projects can't enable them. Their replacement is the Routes API, which the Maps JavaScript API exposes through the Route and RouteMatrix classes.
Where the legacy routing APIs stand
Google moved the Directions API, the Distance Matrix API and their JavaScript services to Legacy status on 1 March 2025. Google's documentation, as of November 2026, says:
- As of October 2026, Directions API (Legacy) and Distance Matrix API (Legacy) can no longer be enabled for new Google Cloud projects.
- Existing projects remain fully supported.
- Legacy services are feature frozen. New routing features go to the Routes API.
- There is no decommission date yet, and Google has committed to at least 12 months' notice before one.
A working site won't break today. The problem appears when you launch the same code on a new Cloud project, for example a new client site or a rebuilt account. There, the legacy services can't be enabled and every request is refused.
How to tell you are on the legacy APIs
| Legacy | Replacement |
|---|---|
| new google.maps.DirectionsService() and .route() | Route.computeRoutes() |
| DirectionsRenderer.setDirections() | route.createPolylines() and route.createWaypointAdvancedMarkers() |
| new google.maps.DistanceMatrixService() and .getDistanceMatrix() | RouteMatrix.computeRouteMatrix() |
| Server calls to maps.googleapis.com/maps/api/directions/json | Routes API computeRoutes on routes.googleapis.com |
| Server calls to maps.googleapis.com/maps/api/distancematrix/json | Routes API computeRouteMatrix |
Legacy services fail quietly. A refused request returns a REQUEST_DENIED status inside a normal response, so there may be no red …MapError line in the console. The map loads, and the route simply never appears.
Our scanner records the page's Google Maps network requests and labels them as legacy Directions, legacy Distance Matrix or Routes API, along with the status Google returned. That only covers routing requests the page makes while it loads. A route that is only requested after a visitor clicks a button won't show up in a scan.
Before you change code: enable the Routes API
- 01In Google Cloud Console, select the project that owns the key your page uses.
- 02Open the API Library, find Routes API and enable it. Google's setup guide for the JavaScript routes library requires both the Maps JavaScript API and the Routes API.
- 03Open the key under Credentials. If its API restrictions limit which APIs it can call, add Routes API to the list.
- 04Keep the legacy Directions API enabled until the new code is live.
A key that is restricted to a list without the Routes API will be refused even though the API is enabled. See API restriction errors.
Migrating DirectionsService
// Before: DirectionsService + DirectionsRenderer
const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer({ map });
directionsService.route(
{ origin: "King's Cross, London", destination: "Greenwich, London", travelMode: "DRIVING" },
(result, status) => {
if (status === "OK") directionsRenderer.setDirections(result);
}
);
// After: Route.computeRoutes
const { Route } = await google.maps.importLibrary("routes");
const { routes } = await Route.computeRoutes({
origin: "King's Cross, London",
destination: "Greenwich, London",
travelMode: "DRIVING",
fields: ["path", "distanceMeters", "durationMillis"], // required field mask
});
const route = routes[0];
route.createPolylines().forEach((polyline) => polyline.setMap(map));
// Start and end markers are not drawn automatically.
// Advanced markers need a map created with a map ID.
const markers = await route.createWaypointAdvancedMarkers();
markers.forEach((marker) => (marker.map = map));
summary.textContent = `${(route.distanceMeters / 1000).toFixed(1)} km`;
What changes:
- The routes library is loaded on demand with importLibrary("routes"), after the Maps JavaScript API itself has loaded.
- A field mask is required. computeRoutes only returns the fields you list in fields. Google advises against requesting everything with ["*"]. Request path if you want to draw the route.
- There is no renderer object. You draw the route with createPolylines() and add markers with createWaypointAdvancedMarkers(). Advanced markers need a map ID. Google's docs mention DEMO_MAP_ID for testing.
- Promises replace callbacks. Errors are thrown, not returned as a status. Wrap the call in try/catch and show a message when it fails.
- Distance and duration come as numbers. The result has distanceMeters and durationMillis, so format them yourself.
Migrating DistanceMatrixService
// Before: DistanceMatrixService
const service = new google.maps.DistanceMatrixService();
const response = await service.getDistanceMatrix({
origins: ["Manchester, UK"],
destinations: ["Leeds, UK", "Liverpool, UK"],
travelMode: "DRIVING",
drivingOptions: { departureTime: new Date(), trafficModel: "optimistic" },
});
// After: RouteMatrix.computeRouteMatrix
const { RouteMatrix } = await google.maps.importLibrary("routes");
const response = await RouteMatrix.computeRouteMatrix({
origins: ["Manchester, UK"],
destinations: ["Leeds, UK", "Liverpool, UK"],
travelMode: "DRIVING",
departureTime: new Date(),
trafficModel: "optimistic",
});
Google's migration guide maps the old options like this: drivingOptions becomes top-level departureTime, arrivalTime and trafficModel. transitOptions becomes transitPreference. unitSystem becomes units. avoidHighways and avoidTolls move into route modifiers.
Server-side code
If your server calls the Directions web service, move it to the Routes API's REST endpoint. Requests are POST with a JSON body, the key goes in a header, and the field mask is mandatory:
curl -X POST "https://routes.googleapis.com/directions/v2:computeRoutes" \
-H "Content-Type: application/json" \
-H "X-Goog-Api-Key: $SERVER_KEY" \
-H "X-Goog-FieldMask: routes.duration,routes.distanceMeters,routes.polyline.encodedPolyline" \
-d '{
"origin": { "address": "Manchester, UK" },
"destination": { "address": "Leeds, UK" },
"travelMode": "DRIVE"
}'
Note that the REST API's travel mode values are not the same strings as the JavaScript ones. Check the Routes API reference for each value you use. Keep server keys separate from the browser key, restricted by IP address rather than website.
Plugins and store locators
If the directions come from a WordPress plugin or a store locator script, you can't change its calls yourself. Check whether the current version supports the Routes API and update. Until it does, keep the legacy Directions API enabled on the existing project and don't move the site to a new Cloud project.
Confirm it works
- 01Load the page with the console open and request a route. The line should draw and the distance and time should display.
- 02In the network tab, check that routing requests now go to routes.googleapis.com.
- 03Try a route that can't be computed, such as an address on another continent, and check that your error message appears.
- 04Once stable, remove the legacy Directions and Distance Matrix APIs from the key's API restrictions.
Common questions
Is the Google Directions API being shut down?
Not yet. It has been in Legacy status since 1 March 2025, and Google says existing projects remain fully supported. Google has committed to at least 12 months' notice before decommissioning legacy services. As of October 2026, new projects can't enable it.
Why does DirectionsService return REQUEST_DENIED on my new site?
The most likely reason is that the new site uses a new Cloud project where Directions API (Legacy) isn't enabled and can no longer be. Migrate to Route.computeRoutes and enable the Routes API.
Is there a DirectionsRenderer for the Routes API?
No. With the Route class you draw the route yourself using createPolylines(), and add start, end and waypoint markers with createWaypointAdvancedMarkers().
Do I need a field mask in the JavaScript Route class?
Yes. Route.computeRoutes requires the fields property. Request only what you use, such as path, distanceMeters and durationMillis.
A scan tells you in about twelve seconds, from a clean session.