Integrating ETA Service via API
This article explains how to receive and fetch the estimated arrival times (ETAs) of your bookings' stops through the MotionTools API.
The ETAs come from the ETA service and are ready to be shown in your customer app, on external tracking pages, or on a custom dashboard.
Prerequisites
A valid API key has been created by an Admin via the MotionTools Dashboard.
At least one booking has been created via the Dashboard or API.
How ETA notifications work
When the ETAs of a tour's stops have been recalculated, MotionTools sends two webhooks:
| Webhook | Sent for | Carries |
|---|---|---|
booking.etas_recalculated | Each booking on the tour | unfinished_stops_info: the ETAs of the booking's next ten unfinished stops |
tour.etas_recalculated | The tour | route_estimates: the estimates for every stop still expecting an arrival, in the same shape as the #EstimateBookingStopsArrivals endpoint returns them |
On very large tours, and when the estimation isn't available, route_estimates is null. In that case, fetch the ETAs from the estimates endpoints instead.
ETAs are recalculated whenever something changes on the tour, and additionally at regular intervals while the tour is En route. Whether a recalculation sends the webhooks depends on what caused it:
- A change to the tour, such as the tour starting, a stop being completed, or stops being added, removed or reordered, always sends them.
- A regular recalculation only sends them when the expected arrival time of at least one stop moved, compared to the minute, or the stops still expecting an arrival changed.
Don't treat ETA notifications as a heartbeat
The webhooks don't arrive at a fixed rate. A quiet period means the ETAs haven't changed, not that they're out of date. The estimates endpoints always return the latest ETAs.
Setting a stop's arrival times (earliest_arrival_at, latest_arrival_at) doesn't trigger a recalculation. The estimates endpoints take the new time window into account right away, while the webhooks only include it with the next recalculation that sends them.
INFO
For the payload details, see the API announcements The tour ETA notification now carries the estimates and ETA notifications now only arrive when the estimates change.
Best practices for fetching ETAs for a single booking
Subscribe to the
booking.etas_recalculatedwebhook. Itsunfinished_stops_infocarries the ETAs of the booking's next ten unfinished stops, so you can show them without fetching anything.Use the #EstimateBookingStopsArrivals endpoint to fetch the ETAs for the booking's stops, for example when you first show the booking. Besides the ETAs, it returns each stop's waiting time, which the webhook doesn't carry. The ETAs are returned as fixed timestamps in UTC indicating when a driver is expected to arrive at each stop. Note that the ETAs aren't recalculated each time the endpoint is called.
Best practices for fetching ETAs for multiple bookings
If you track whole tours, subscribe to the
tour.etas_recalculatedwebhook. A single event carries the estimates for all stops of the tour that still expect an arrival, across all of its bookings. Otherwise, subscribe tobooking.etas_recalculatedand use each booking'sunfinished_stops_infoas described for a single booking.Use the #ListBookings endpoint to fetch bookings for which you need to get ETAs.
Use the #EstimateBookingsStopsArrivals endpoint to fetch ETAs in bulk for the stops of each booking, for example when you first load them. The ETAs are returned as fixed timestamps in UTC indicating when a driver is expected to arrive at each stop. Note that the ETAs aren't recalculated each time the endpoint is called.
When a webhook event arrives, apply the ETAs it carries. Only fetch them from the estimates endpoints when
route_estimatesisnull.