Build Different

Using TomTom Data via APIs to Improve Delivery ETA Accuracy

Developer Portal
Nov 9, 2021 • Last edit on Sep 20, 20229 min read
Laptop showing map alongside photo of a downtown urban city

When using delivery applications, it’s crucial to know roughly when a delivery will arrive, whether you’re expecting food, a device, documents, or a service person. The estimated time of arrival (ETA) helps determine which drivers can pick up a customer or an order quickly, the fastest route to the customer, and when the customer should expect their package to arrive.

One straightforward way to improve delivery ETA is to call the Routing API periodically to get ETA updates. This method is most suitable for real-time or nearly real-time applications when the driver is currently delivering the goods. Periodic calls to the Routing API can use bandwidth and incur additional costs, so it doesn’t make sense to use this method when it isn’t necessary.

In contrast, offline tools are best for planning and optimizing a delivery company’s routes. These tools can use historical traffic data to help the company decide when to send a delivery — without using excessive resources. The TomTom Routing API provides route information for a vast range of dates and times.

For example, we can send multiple GET requests to the Routing API with varying dates and times. We can, for instance, fix the day of the week then change the departure times. We then get the ETA for each route and select the one with a minimal ETA to optimize our course.

However, the more routes we must analyze, the longer it takes. A data science solution helps by automatically gathering large datasets (with variable route parameters) from the Routing API. We can build a machine learning (ML) model based on that information, and the model can then help us find the most optimal departure time to ensure the delivery arrives when expected. For instance, we can discover the route with the shortest travel time to ensure temperature-sensitive deliveries arrive as quickly as possible, with an accurate ETA to help customers know when to expect our driver.

Here, we’ll use the TomTom Routing API, Python, and scikit-learn to create such a model. The problem described above is an optimization (we need to find the route with the shortest travel time). To keep it simple, we’ll get route information for ten different departure times, then build the linear regression model to predict the ETA for a given departure time. We use scikit-learn for the machine learning portion of our solution. It will help us learn (fitting data to a model) and predict (using fitted parameters to predict ETAs).

For this tutorial, you’ll need some understanding of Python, a TomTom account, and a registered API key for the TomTom Maps SDK.


Improving Delivery ETA Accuracy

TomTom’s Maps APIs provide rich data collections that we can use to enhance accuracy for delivery applications. Developers can integrate this information with transportation management systems, truck tracking systems, mapping data, and predictive analytics. Data scientists can also use machine learning and data analytics to improve our applications.

TomTom Maps APIs provide a great developer experience because of their flexibility. Additionally, the variety and richness of data generated by TomTom Maps APIs make them the ideal choice for data scientists who aim to build accurate and handy prediction models based on mapping data. Significant benefits of using TomTom APIs as input for data science predictive models include:

• Increased ETA accuracy • Onboarding quickly • Suitable to use with AI and machine learning • Proactive service and customer satisfaction

Developers and data scientists first pull data from TomTom Maps APIs and put the information to work using our favorite tools and libraries, like Python, Pandas, scikit-learn, TensorFlow, and PyTorch. Then, we merge this data into our data science applications to get the desired results. The figure below presents an outline of using data science techniques to improve ETA based on the data collected from TomTom Maps APIs.

Using the Routing API

We start building our ETA optimization solution using the Routing API. The best way to familiarize yourself with this service is through the TomTom Developer portal. The portal’s user interface (UI) is similar to Swagger (that API developers frequently use) with a set of text boxes and dropdown lists to configure request parameters.

The image below shows an example of using this portal to determine the route between San Francisco (37.77493,-122.419415) and Los Angeles (34.052234,-118.243685). We specify these route endpoints in the locations parameter. For this tutorial, we’ll also use the departAt field and assume the delivery is by truck (travelMode):



Once we finish selecting the appropriate values, we scroll down and click Execute. The portal returns a sample response (here we use JSON format):



The results are:

2 "formatVersion": "0.0.12",
3 "routes": [
4 {
5 "summary": {
6 "lengthInMeters": 624544,
7 "travelTimeInSeconds": 24670,
8 "trafficDelayInSeconds": 0,
9 "trafficLengthInMeters": 0,
10 "departureTime": "2021-10-20T10:00:00-07:00",
11 "arrivalTime": "2021-10-20T16:51:10-07:00"
12 },
13 "legs": [
14 {
15 "summary": {
16 "lengthInMeters": 624544,
17 "travelTimeInSeconds": 24670,
18 "trafficDelayInSeconds": 0,
19 "trafficLengthInMeters": 0,
20 "departureTime": "2021-10-20T10:00:00-07:00",
21 "arrivalTime": "2021-10-20T16:51:10-07:00"
22 },
23 "points": [
24 {
25 "latitude": 37.77499,
26 "longitude": -122.41948
27 },
28 // Note that most of the points were omitted
29 {
30 "latitude": 34.05238,
31 "longitude": -118.24359
32 }
33 ]
34 }
35 ],
36 "sections": [
37 {
38 "startPointIndex": 0,
39 "endPointIndex": 4779,
40 "sectionType": "TRAVEL_MODE",
41 "travelMode": "truck"
42 }
43 ]

As the results above show, we can read ETA from the arrivalTime value (2021-10-22T17:00:10-07:00). We can also get the estimated travel time, expressed in seconds, from the travelTimeInSeconds field. Here, the travel time is approximately seven hours.

Fetching Data Using Python

We now have all the required information to write the Python script for fetching ETAs. We just need to prepare the request link based on the previous section then use a dedicated tool for sending the GET request to the TomTom API. Here, we use the Requests pip package, a standard Python tool.

Of course, we also need to encode the request URL correctly, so we use urllib.parse.

This code constructs the request URL to get the route information between San Francisco and Los Angeles:

1import requests
2import urllib.parse as urlparse
4# Route parameters
5start = "37.77493,-122.419415" # San Francisco
6end = "34.052234,-118.243685" # Los Angeles
7routeType = "fastest" # Fastest route
8traffic = "true" # To include Traffic information
9travelMode = "truck" # Travel by truck
10avoid = "unpavedRoads" # Avoid unpaved roads
11departAt = "2021-10-20T10:00:00" # Departure date and time
12vehicleCommercial = "true" # Commercial vehicle
13key = "key=<TYPE_YOUR_API_KEY_HERE>" # API Key
15# Building the request URL
16baseUrl = "";
18requestParams = (
19 urlparse.quote(start) + ":" + urlparse.quote(end)
20 + "/json?routeType=" + routeType
21 + "&traffic=" + traffic
22 + "&travelMode=" + travelMode
23 + "&avoid=" + avoid
24 + "&vehicleCommercial=" + vehicleCommercial
25 + "&departAt=" + urlparse.quote(departAt))
27requestUrl = baseUrl + requestParams + "&key=" + key

Once we have the request, we send it like this:

response = requests.get(requestUrl)

The resulting response object contains the HTTP status code as well as the route information. Here is how to parse the data to get the first route’s ETA and travel time:

1if(response.status_code == 200):
2 # Get response's JSON
3 jsonResult = response.json()
5 # Read summary of the first route
6 routeSummary = jsonResult['routes'][0]['summary'];
8 # Read ETA
9 eta = routeSummary['arrivalTime']
11 # Read travel time and convert it to hours
12 travelTime = routeSummary['travelTimeInSeconds'] / 3600
14 # Print results
15 print(f"{departAt}, ETA: {eta}, Travel time: {travelTime:.2f}h")

You can find the complete script on GitHub.

First, edit the script to replace the API key placeholder (line 12) with your API key. Then, run the script (python3 You should get results similar to the following screenshot:


Getting Training Data

Next, we need to send several requests for various departure times to get data for building our model. You can find the complete script on GitHub, built on the previous code. Specifically, there is a getEta function, which accepts one parameter, departureTime. Based on this, the getEta function returns a tuple comprising three items: departure time, ETA, and travel time. To determine ETAs for various departure times, we need to call getEta multiple times, like so:

1# Departure times
2departureTimeStart = datetime.datetime(2021, 10, 20, 0, 0, 0)
4for i in range(0,23):
5 # Update an hour
6 departureTime = departureTimeStart.replace(hour=departureTimeStart.hour + i)
8 # Format datetime string
9 departureTime = departureTime.strftime('%Y-%m-%dT%H:%M:%S')
11 # Get and print ETA
12 getEta(departureTime)

This code prints the arrival times and travel times for each departure time as follows:


We now see that by leaving San Francisco at 10 PM, the route will take about 6.5 hours. Conversely, if we leave San Francisco at 4 PM, the travel time will be about 7.3 hours. Hence, we can save nearly an hour by choosing one departure time versus another.

Training the Model to Predict ETA

We can save effort and analyze more departure times using an ML model to determine the optimal departure time. To prepare the model, we first copy the data printed by the previous script to the CSV file: train_data.csv. Also, we add one more column, departure hour, containing only the hour part of the departure datetime. We do this to simplify linear regression.

Now, we can use Pandas to read the CSV (see

1import pandas as pd
2from sklearn.linear_model import LinearRegression
4# Read train data
5dataFrame = pd.read_csv('train_data.csv',sep=';')
7# Use DepartureHour column as X
8X = dataFrame.DepartureHour
10# Use TravelTimeInHours column as Y
11Y = dataFrame.TravelTimeInHours

As shown above, we use DepartureHour and TravelTimeInHours as our X (predictor) and Y (criterion variable).

Then, we use scikit-learn to train the Linear Regression model:

1# Train the model
2from sklearn.linear_model import LinearRegression
3import numpy as np
5model = LinearRegression().fit(np.array(X).reshape(-1,1), Y)

Finally, we predict travel times for the departure hours that the model has not already seen, like so:

1# Predict travel time for different departure hours
2departureHours = [1.5, 3.12, 5.17, 16.05, 10.15, 20.29]
4for departureHour in departureHours:
5 predictedTravelTime = model.predict(np.array(departureHour).reshape(-1,1))
7 print(f"Departure hour: {departureHour}, Predicted travel time:
8{predictedTravelTime[0]:.2f} h")

The above code produces the following result:


With this solution in hand, route planners can now find the best departure time. Our estimator (model) is not perfect as it uses the simple linear model. However, we can always replace it with a more comprehensive ML model. Also, we can use more features from the TomTom Routing API (like routeType, avoid, hilliness, windingness, and vehicleMaxSpeed) during training to further optimize our route planning.


The problem of predicting estimated travel time accuracy isn’t straightforward. It’s a complicated process that must consider all types of traffic modes without affecting application performance. Once we know the speed limits of roads and have an up-to-date road routing plot and traffic information, our application can analyze the expected arrival time of different routes to predict the optimal route needed for customer satisfaction.

The data science module uses TomTom’s historical traffic data to predict the optimal time to leave one city to reduce travel time. Choosing this route helps ensure the customer’s delivery arrives with minimal travel time (ideal for temperature-sensitive items, for instance) with a more accurate ETA based on actual historical data for that time of day. Pulling in additional details for each route can further improve ETA accuracy.

Now that you’ve seen the capabilities of TomTom Maps APIs and how you can improve the accuracy of your ETAs, sign up and start using TomTom Maps!

Get the developer

No marketing fuff. Tech content only.
Thanks for contacting us

We will reach out to you soon.
Blog cards
tomtom tech news