Skip to main content

Documentation Index

Fetch the complete documentation index at: https://samsara-showcase.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

This recipe calls the /gateways endpoint, filters for Vehicle Gateway models, paginates through the response, and writes each gateway’s installation status to data.csv.
import csv
import os

import requests

url = "https://api.samsara.com/gateways"

headers = {
    "Accept": "application/json",
    "Authorization": f"Bearer {os.environ['SAMSARA_API_TOKEN']}",
}

params = {
    "models": "VG34,VG54",
}

with open("data.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["assetId", "assetSerial", "installed"])

    while True:
        response = requests.request("GET", url, headers=headers, params=params).json()

        for asset in response["data"]:
            if asset["connectionStatus"]["healthStatus"] == "Not Installed":
                installed = "Not Installed"
            else:
                installed = "Installed"

            writer.writerow([
                asset["asset"]["id"],
                asset["serial"],
                installed,
            ])

        params["after"] = response["pagination"]["endCursor"]

        if response["pagination"]["hasNextPage"] is False:
            break

How it works

1

Use the requests and csv modules

The script uses requests to make HTTP requests to the Samsara REST API and csv to write response data in CSV format.
2

Use the /gateways endpoint

The /gateways endpoint lists all devices associated with the dashboard, including device health and status information.
3

Configure the Authorization header

Provide your API token in the Authorization HTTP header using the Bearer token scheme. See Authentication for more details.
4

Select Vehicle Gateway models

Samsara tracks the “Not Installed” status for Vehicle Gateway models. The models parameter limits the response to those gateway models.
5

Create the CSV file

The script opens data.csv and writes the assetId, assetSerial, and installed column headers.
6

Paginate through gateways

The response includes pagination.endCursor and pagination.hasNextPage. Continue passing the latest endCursor as after until hasNextPage is false.
7

Parse response data

Each entry in the data array represents a gateway. If connectionStatus.healthStatus is "Not Installed", the script writes "Not Installed"; otherwise, it writes "Installed".

Example response shape

{
  "data": [
    {
      "serial": "ABCD-EFG-HIJ",
      "model": "VG34",
      "asset": {
        "id": "1234",
        "externalIds": {
          "samsara.serial": "ABCDEFGHIJ"
        }
      },
      "connectionStatus": {
        "healthStatus": "Not Installed"
      },
      "dataUsageLast30Days": {
        "cellularDataUsageBytes": 0,
        "hotspotUsageBytes": 0
      }
    }
  ],
  "pagination": {
    "endCursor": "abc-123",
    "hasNextPage": true
  }
}