GPS asset tracking
Monitor asset locations and telematics for vehicles and trailers
Fleets depend on accurate, real-time location data to manage daily operations, plan dispatches, and provide visibility to customers. This guide shows how to integrate Samsara's APIs to track vehicles and trailers in your TMS.
Overview
-
Vehicle tracking
- Monitor live vehicle locations.
- Plan dispatches based on vehicle proximity and availability.
-
Trailer tracking
- Track trailer locations independently of tractors.
- Understand asset utilization and deploy trailers more effectively.
-
Historical data
- Reconstruct routes, investigate exceptions, or fill gaps if your real-time polling is interrupted.
- Generate analytical insights like utilization or dwell times.
Key endpoints
Samsara offers multiple endpoints to retrieve location data. Which one you use depends on your use case: a simple point-in-time snapshot, near real-time feed, or historical queries.
1. Vehicles
-
Latest snapshot
GET /fleet/vehicles/stats
- Retrieve the current (most recent) telematics data for each vehicle in your fleet.
-
Continuous feed
GET /fleet/vehicles/stats/feed
- Return GPS and other telematics events since your last request. Ideal for near real-time location updates (e.g., polling every 5–30 seconds).
-
Historical backfill
GET /fleet/vehicles/stats/history
- Retrieve historical events in a specified time range to fill gaps or run reports. Not recommended for continuous streaming.
2. Trailers
-
Snapshot
GET /beta/fleet/trailers/stats
- Fetch a point-in-time location and telematics snapshot for trailers.
-
Streaming location updates
GET /assets/location-and-speed/stream
- Continuously retrieve new locations for assets (including trailers). This endpoint supports useful query parameters such as
includeReverseGeo
(address lookup) andincludeGeofenceLookup
(geofence ID lookup).
Example request:
curl -X GET 'https://api.samsara.com/assets/location-and-speed/stream?ids=123&startTime=2025-03-01T00:00:00Z&includeReverseGeo=true&includeGeofenceLookup=true' \
--header "Authorization: Bearer <TOKEN>"
Example response:
{
"data": [
{
"happenedAtTime": "2025-03-11T10:21:19Z",
"asset": {
"id": "281474994182986"
},
"location": {
"latitude": 42.93845637,
"longitude": -71.53398559,
"headingDegrees": 0,
"accuracyMeters": 2.868,
"address": {
"streetNumber": "74",
"pointOfInterest": "",
"neighborhood": "",
"street": "Briar Road",
"city": "Bedford",
"state": "NH",
"postalCode": "03110",
"country": "US"
},
"geofence": {
"id": "232149051"
}
}
}
],
"pagination": {
"endCursor": "...",
"hasNextPage": true
}
}
Example usage
Choose the approach that fits your needs:
-
Occasional snapshots
- Use
GET /fleet/vehicles/stats
orGET /beta/fleet/trailers/stats
for simple on-demand lookups when you only need to show the current location.
- Use
-
Near real-time tracking
- Poll
GET /fleet/vehicles/stats/feed
orGET /assets/location-and-speed/stream
at a fixed interval (e.g., every 5–30 seconds) to retrieve incremental updates. - This approach ensures that you can quickly display changes in location or heading within your TMS.
- Poll
-
Historical backfill
- If your feed-based integration temporarily goes down, you can use
GET /fleet/vehicles/stats/history
(with a time range) to retrieve any missed data points.
- If your feed-based integration temporarily goes down, you can use
-
Augmenting location data with addresses or geofences
- Pass
includeReverseGeo=true
to include street-level address details. - Pass
includeGeofenceLookup=true
to return any Samsara geofences that match the asset’s current position.
- Pass
Sample workflow: near real-time vehicle and trailer tracking
-
Initialize
- Call
GET /fleet/vehicles/stats?types=gps
andGET /beta/fleet/trailers/stats?types=gps
once at startup to populate your TMS with current locations for all vehicles and trailers.
- Call
-
Continuous sync
- Poll
GET /fleet/vehicles/stats/feed?types=gps
and/orGET /assets/location-and-speed/stream?reverseGeo=true&geofenceLookup=true
every few seconds to retrieve any new GPS events. - Match each incoming event to the corresponding vehicle or trailer in your TMS (e.g., using
externalIds
).
- Poll
-
UI updates
- As soon as new data arrives, update your TMS map view or other tracking interfaces.
- Show relevant details such as last known address, speed, and geofence if available.
-
Historical recovery
- If a polling outage or network error occurs, backfill missing points using
GET /fleet/vehicles/stats/history
with a time range. - This ensures no gaps in trip history or reporting.
- If a polling outage or network error occurs, backfill missing points using
Example requests and responses
Get the latest vehicle snapshot
Example request:
curl 'https://api.samsara.com/fleet/vehicles/stats?types=gps' \
--header "Authorization: Bearer <TOKEN>"
Example response:
{
"data": [
{
"id": "123",
"name": "Truck",
"externalIds": {
"samsara.serial": "G522SBGJT9",
"samsara.vin": "123"
},
"gps": {
"time": "2025-03-25T18:23:17Z",
"latitude": 43.34141535,
"longitude": -74.74746674,
"headingDegrees": 0,
"speedMilesPerHour": 0,
"reverseGeo": {
"formattedLocation": "Fuller Drive, Boylston, MA, 01505"
},
"isEcuSpeed": false
}
}
],
"pagination": {
"endCursor": "",
"hasNextPage": false
}
}
Get incremental trailer location updates
Example request:
curl -X GET 'https://api.samsara.com/assets/location-and-speed/stream?includeReverseGeo=true&includeGeofenceLookup=true&ids=123&startTime=2025-03-10T00:00:00Z' \
--header "Authorization: Bearer <TOKEN>"
Example response:
{
"data": [
{
"happenedAtTime": "2025-03-10T14:15:06Z",
"asset": {
"id": "123"
},
"location": {
"latitude": 43.94845637,
"longitude": -72.53393359,
"headingDegrees": 0,
"accuracyMeters": 2.868,
"address": {
"streetNumber": "1",
"pointOfInterest": "",
"neighborhood": "",
"street": "Main Road",
"city": "Somewhere",
"state": "FL",
"postalCode": "03330",
"country": "US"
},
"geofence": {
"id": "232149051"
}
}
}
],
"pagination": {
"endCursor": "...",
"hasNextPage": true
}
}
Use Kafka for near real-time streaming
For large fleets or advanced analytics use cases, Samsara's Kafka Connector provides a high-throughput, near real-time stream of GPS and telematics data. Instead of periodically polling an API, the connector continuously pushes location events, speed updates, and more to your Kafka environment.
Key benefits
- Frequent push updates: Suitable for TMS solutions that require very high-frequency location data.
- Scalability: Easily handle large fleets and massive data volumes by leveraging Kafka’s horizontal scaling.
- Real-time analytics: Integrate with stream-processing frameworks (e.g., Apache Spark, Flink) for advanced analytics, real-time dashboards, or machine-learning pipelines.
How it works
-
Setup the Samsara Kafka Connector:
- Configure the connector in the Samsara Dashboard and provide your Kafka cluster details.
- Select the event types (e.g., GPS and Speed, Diagnostics) you want streamed.
-
Consume events in your TMS:
- Implement a Kafka consumer in your TMS or data pipeline to read location messages as they arrive.
- Samsara publishes each event containing asset and location details, such as latitude, longitude, speed, and timestamps.
-
Process and store data:
- Parse each message to associate the event with the correct vehicle, trailer, or other asset.
- Optionally enrich location data by reversing geocodes or matching geofences before persisting to your TMS database.
Example message format
Below is an illustrative JSON payload you might receive for a GPS and speed event via Kafka:
{
"asset": {
"id": "123"
},
"happenedAtTime": "2023-12-19T17:57:21Z",
"location": {
"accuracyMeters": 0,
"geofence": {},
"headingDegrees": 102,
"latitude": 38.253265915,
"longitude": -80.018465004
},
"speed": {
"gpsSpeedMetersPerSecond": 43.8,
"ecuSpeedMetersPerSecond": 51.2
}
}
When to choose Kafka
- High-frequency updates: You need sub-minute location data without constantly polling an HTTP endpoint.
- Event-driven architecture: Your TMS or analytics platform is already built on Kafka, or you want to leverage Kafka’s decoupled, scalable design.
- Complex data pipelines: You plan to enrich or correlate location events with other real-time sources (e.g., weather, traffic) before serving them to end users.
For more details, see the Samsara Kafka Connector documentation.
Updated about 1 hour ago