# Retrieve Offline Devices

import { Badge } from "zudoku/ui/Badge";

<div className="flex flex-wrap gap-2 not-prose mb-4">
  <Badge variant="outline">Monitoring</Badge>
  <Badge variant="secondary" className="font-mono text-xs">
    GET /v1/devices
  </Badge>
</div>

Retrieve every device whose `status` is `offline`, paging through the full result set.

:::tip

You can adjust the script to your needs. Change the conditions, variables, and values to
produce the output you want.

:::

<Recipe
  language={["python", "javascript"]}
  steps={[
    {
      title: "Set up the request",
      body: "Use the access token returned by the [Authenticate](/recipes/authentication) recipe.",
      code: {
        python:
          'import json\nimport time\n\nimport requests\n\nBASE = "https://api.securithings.com"\nheaders = {"Authorization": f"Bearer {access_token}"}',
        javascript:
          'const BASE = "https://api.securithings.com";\nconst headers = {\n  Authorization: `Bearer ${accessToken}`,\n};\nconst sleep = (ms) =>\n  new Promise((resolve) => setTimeout(resolve, ms));',
      },
    },
    {
      title: "Request the first page",
      body: "Filter on `status` and request the maximum page size.",
      code: {
        python:
          'params = {\n    "status": "offline",\n    "limit": 100,\n    "offset": 0,\n}\npage = requests.get(\n    f"{BASE}/v1/devices",\n    params=params,\n    headers=headers,\n).json()\ndevices = page["devices"]',
        javascript:
          'const params = {\n  status: "offline",\n  limit: 100,\n  offset: 0,\n};\n\nconst fetchPage = async () => {\n  const query = new URLSearchParams(params);\n  const response = await fetch(\n    `${BASE}/v1/devices?${query}`,\n    { headers },\n  );\n  return response.json();\n};\n\nlet page = await fetchPage();\nconst devices = [...page.devices];',
      },
    },
    {
      title: "Page through the results",
      body: "Increase `offset` by `limit` until `total` devices have been retrieved.",
      code: {
        python:
          'while len(devices) < page["total"]:\n    params["offset"] += params["limit"]\n    time.sleep(1)  # 1 request per second\n    page = requests.get(\n        f"{BASE}/v1/devices",\n        params=params,\n        headers=headers,\n    ).json()\n    devices += page["devices"]',
        javascript:
          "while (devices.length < page.total) {\n  params.offset += params.limit;\n  await sleep(1000); // 1 request per second\n  page = await fetchPage();\n  devices.push(...page.devices);\n}",
      },
    },
    {
      title: "Build the result",
      body: "`lastSeen` reports the last time each device was not offline.",
      code: {
        python:
          'result = {\n    "total": page["total"],\n    "devices": [\n        {\n            "id": device["id"],\n            "name": device["name"],\n            "lastSeen": device["lastSeen"],\n        }\n        for device in devices\n    ],\n}\n\nprint(json.dumps(result, indent=2))',
        javascript:
          "const result = {\n  total: page.total,\n  devices: devices.map((device) => ({\n    id: device.id,\n    name: device.name,\n    lastSeen: device.lastSeen,\n  })),\n};\n\nconsole.log(JSON.stringify(result, null, 2));",
      },
    },
  ]}
  response={`{
  "total": 1,
  "devices": [
    {
      "id": "9ff559ee-38b6-11f1-81cb-465864a35d0a",
      "name": "Headquarters camera1",
      "lastSeen": "2026-07-09T11:20:00Z"
    }
  ]
}`}
/>
