# Web API local auth error

**URL:** <https://community.homey.app/t/web-api-local-auth-error/112701>\
**Category:** Developers\
**Created:** [July 10, 2024, 11:16pm UTC](https://community.homey.app/t/web-api-local-auth-error/112701 "2024-07-10T23:16:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KungWaz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kungwaz/32/56612_2.png) [@KungWaz](https://community.homey.app/u/KungWaz)\
**Post date:** [July 10, 2024, 11:16pm UTC](https://community.homey.app/t/web-api-local-auth-error/112701/1 "2024-07-10T23:16:16Z")

</div>

What am I missing to get my Shelly script to work?

I have created a API key with the “Devices” and “Flows” permissions using this guide, [https://support.homey.app/hc/en-us/articles/8178797067292-Getting-started-with-API-Keys](https://support.homey.app/hc/en-us/articles/8178797067292-Getting-started-with-API-Keys)

I have gotten the device ID of the device I want to fetch the status of ([Homey Developer Tools](https://tools.developer.homey.app/tools/devices)).

When I run my script using the API key I generated I get a 401, HomeyErrorUnauthorized

If I make a request towards http://MY\_HOMEY\_IP I get an object with the homeyId and homeyVersion back, so the IP is correct.  
But when I make a request towards http://MY\_HOMEY\_IP/api/manager/devices/device/DEVICE\_ID/capability/CAPABILITY\_ID and sending in the ‘Authorization’: 'Bearer ’ + HOMEY\_API\_KEY I get the auth error.

> **[ManagerDevices - Homey Web API](https://athombv.github.io/node-homey-api/HomeyAPIV3Local.ManagerDevices.html)**

```auto
const HOMEY = {
  apiKey: "xxx-xxx-xxx",
  ipAddress: "X.X.X.X"
}
const DEVICE_ID = "xxx-xxx-xxx"
const BASE_URL = 'http://' + HOMEY.ipAddress + '/api/manager/devices/device/';

function getHomeyData() {
    let url = 'http://' + HOMEY.ipAddress;

    Shelly.call(
        "http.get",
        {
            url: url
        },
        function (response, error_code, error_message) {
            if (response.code === 200 && response.body) {
                let homeyData = JSON.parse(response.body);
                print('Homey Data:', JSON.stringify(homeyData))
            } else {
                print("Error Code:", error_code);
                print("Message:", error_message);
                print("Body:", JSON.parse(response.body));
                if (response.code === 401) {
                    print('Unauthorized: Please check your API token and permissions.');
                }
            }
        }
    );
}

function getLockState(deviceId, capability) {
    let url = BASE_URL + deviceId + '/capability/' + capability
    print('URL:', url);
    Shelly.call(
        "http.get",
        {
            url: url,
            headers: {
                'Authorization': 'Bearer ' + HOMEY.apiKey
            }
        },
        function (response, error_code, error_message) {
            if (response.code === 200 && response.body) {
                let capabilityValue = JSON.parse(response.body);
                print('Capability Value:', capabilityValue);
            } else {
                print("Error Code:", error_code);
                print("Message:", error_message);
                print("Body:", JSON.parse(response.body));
                if (response.code === 401) {
                    print('Unauthorized: Please check your API token and permissions.');
                }
            }
        }
    );
}

getHomeyData();
getLockState(DEVICE_ID, 'locked');

```

---

<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:** [July 11, 2024, 4:30am UTC](https://community.homey.app/t/web-api-local-auth-error/112701/2 "2024-07-11T04:30:56Z")

</div>

I don’t have a Shelly to run this code on, but converted to a plain Node.js script it works just fine.

Are you sure that the API key was created with the correct scopes?

---

<div class="post-metadata">

**Author:** ![KungWaz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kungwaz/32/56612_2.png) [@KungWaz](https://community.homey.app/u/KungWaz)\
**Post date:** [July 11, 2024, 6:22am UTC](https://community.homey.app/t/web-api-local-auth-error/112701/3 "2024-07-11T06:22:21Z")

</div>

![homey-apikey-shelly](https://us1.discourse-cdn.com/flex025/uploads/athom/original/3X/1/d/1d04973d8b565e69d36abe36ac2c4483902fef5a.png)

I have also tried to create an API key with all boxes ticked, same result…  
Is there some feature I need to activate for this to work?

---

<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:** [July 11, 2024, 7:19am UTC](https://community.homey.app/t/web-api-local-auth-error/112701/4 "2024-07-11T07:19:22Z")

</div>

No, it should work.

---

<div class="post-metadata">

**Author:** ![KungWaz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kungwaz/32/56612_2.png) [@KungWaz](https://community.homey.app/u/KungWaz)\
**Post date:** [July 11, 2024, 1:59pm UTC](https://community.homey.app/t/web-api-local-auth-error/112701/5 "2024-07-11T13:59:38Z")

</div>

Any special encoding I need to use? Currently it just a string which I have copy/pasted from the Homey portal and feed directly to the Authentication header.

---

<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:** [July 11, 2024, 2:04pm UTC](https://community.homey.app/t/web-api-local-auth-error/112701/6 "2024-07-11T14:04:14Z")

</div>

This is the code I tested with:

```auto
const HOMEY = {
  apiKey: "...",
  ipAddress: "aa.bb.cc.dd"
}
const DEVICE_ID = "...";
const BASE_URL = 'http://' + HOMEY.ipAddress + '/api/manager/devices/device/';

async function getLockState(deviceId, capability) {
  let url = BASE_URL + deviceId + '/capability/' + capability
  console.log('URL:', url);

  const r = await fetch(url, {
    headers: {
      'Authorization': 'Bearer ' + HOMEY.apiKey
    }
  });
  console.log('R', r.status);
  const data = await r.json();
  console.log(data);
}

getLockState(DEVICE_ID, 'dim');

```

So looks very much like yours, but using `fetch()` instead of the Shelly-specific functions. You can run this code in HomeyScript to see if your issue is related to the API key somehow, or if it’s something specific to Shelly.

---

<div class="post-metadata">

**Author:** ![KungWaz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kungwaz/32/56612_2.png) [@KungWaz](https://community.homey.app/u/KungWaz)\
**Post date:** [July 11, 2024, 2:59pm UTC](https://community.homey.app/t/web-api-local-auth-error/112701/7 "2024-07-11T14:59:11Z")

</div>

Ok, thanks, will try that and see if my token somehow is the problem. Or if the Shelly script might have some issues with the auth header I send in.

---

<div class="post-metadata">

**Author:** ![KungWaz](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/kungwaz/32/56612_2.png) [@KungWaz](https://community.homey.app/u/KungWaz)\
**Post date:** [July 12, 2024, 7:00am UTC](https://community.homey.app/t/web-api-local-auth-error/112701/8 "2024-07-12T07:00:23Z")

</div>

It was the Shelly script itself I managed to mess up, if I changed to the `http.request` call it worked.

```auto
const BASE_URL = 'http://' + HOMEY.ipAddress + '/api/manager/devices/device/';
const HEADERS = {
  "Authorization": "Bearer " + HOMEY.apiKey
}

Shelly.call("http.request", {
   method: "GET",
   url: url,
   headers: HEADERS
}, ...

```

So it seems like the `http.get` does not handle the headers.

Thanks for the help, the verification from your end was helpful to point me in the right direction and try something else.
