# Web API get device by id

**URL:** <https://community.homey.app/t/web-api-get-device-by-id/120488>\
**Category:** Developers\
**Tags:** app, homey-pro\
**Created:** [November 13, 2024, 8:05pm UTC](https://community.homey.app/t/web-api-get-device-by-id/120488 "2024-11-13T20:05:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Grewenig](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/christian_grewenig/32/49383_2.png) [@Christian\_Grewenig](https://community.homey.app/u/Christian_Grewenig)\
**Post date:** [November 13, 2024, 8:05pm UTC](https://community.homey.app/t/web-api-get-device-by-id/120488/1 "2024-11-13T20:05:59Z")

</div>

Hi,

I have an app with a device that has a measure\_battery capability. I created a Web API GET handler as described in [Web API | Homey Apps SDK](https://apps.developer.homey.app/advanced/web-api).

There is a method batteryStatus in the `/api.js` file as it is for the getSomething method from the documentation.

My hardware device will make a GET request to this web api with the query parameters `.../batteryStatus?deviceId=xxx&voltage=xxx`

So far everything works fine. But how can I get the device by the given deviceId to update the capability value?

kind regards,  
Christian

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 14, 2024, 6:15am UTC](https://community.homey.app/t/web-api-get-device-by-id/120488/2 "2024-11-14T06:15:18Z")

</div>

> [@Christian\_Grewenig](#):
>
> But how can I get the device by the given deviceId to update the capability value?

You need to retrieve the device instance and call `setCapabilityValue` on it. I’m not sure if you can access `ManagerDrivers` from the API, so it would be easiest to call an app method from your API endpoint:

```auto
// api.js
async setBatteryStatus({ homey, query }) {
  return homey.app.setBatteryStatus(query.deviceId, query.voltage);
}

// app.js
class MyApp extends Homey.App {
  ...
  async setBatteryStatus(deviceId, voltage) {
    for (const [driverId, driver] in Object.entries(this.homey.drivers.getDrivers())) {
      for (const device of await driver.getDevices()) {
        if (device.id === device.getId()) {
          await device.setCapabilityValue('measure_battery', voltage);
          return true;
        }
    }
  }
  return false;
}

```

(untested)

Sadly the way to retrieve a device by id in the SDK is hopelessly convoluted and inconsistent (`getDrivers()` returns an object, `getDevices()` returns an array 😑)

FWIW, semantically speaking you should use either `PUT` or `POST` instead of `GET`, but I assume there’s a reason you want to use the latter.

---

<div class="post-metadata">

**Author:** ![Christian\_Grewenig](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/christian_grewenig/32/49383_2.png) [@Christian\_Grewenig](https://community.homey.app/u/Christian_Grewenig)\
**Post date:** [November 24, 2024, 7:34pm UTC](https://community.homey.app/t/web-api-get-device-by-id/120488/3 "2024-11-24T19:34:55Z")

</div>

Thank you for your answer Robert.

I have now found a solution to access the device. I am not sure if it is the right solution, but it works.

```auto
async getDeviceById(homey, deviceId) {
        const driver = await homey.drivers.getDriver("<my.device.id>");
        let devices = await driver.getDevices();
        ...iterate over devices and retrieve the device...

```

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [November 25, 2024, 5:25am UTC](https://community.homey.app/t/web-api-get-device-by-id/120488/4 "2024-11-25T05:25:50Z")

</div>

Yeah, good solution 👍🏻
