# Turning on and off the Public Device URL from code on the device

**URL:** <https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082>\
**Category:** balenaOS\
**Tags:** api\
**Created:** [April 19, 2021, 6:29pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082 "2021-04-19T18:29:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tacLog](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tacLog](https://forums.balena.io/u/tacLog)\
**Post date:** [April 19, 2021, 6:29pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/1 "2021-04-19T18:29:53Z")

</div>

Hey team Balena,

I was poking around your api documentation trying to find a way to turn on and off the public device URL. The purpose of this would be to turn on an off access programmatically to add a layer of just-in-time security to a devices remote access.

I have set device tags in the past using this bit of python code:

```
def set_device_tag(self, name, value):
    logging.debug(f'set_device_tag: {name}, {value}')
    if type(value) == float:
        value = f'{value:.1f}'
    else:
        value = str(value)

    try:
        self.balena.models.tag.device.set(self.env['BALENA_DEVICE_UUID'], str(name), value)
    except Exception as e:
        logging.error("Tag set seems to have failed")
        handle_process_exception(e, debug=True)
        raise ConnectionError

def get_device_id(self):
    try:
        proc1 = subprocess.Popen("""curl --connect-timeout 10 --max-time 30 -sSL "$BALENA_API_URL/v3/device?\$select=id,uuid&\$filter=uuid%20eq%20'$BALENA_DEVICE_UUID'" -H "Authorization: Bearer $BALENA_API_KEY" | jq '.d[0].id'""",
                                 shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                                 close_fds=True, encoding='UTF-8')
        stdout, stderr = proc1.communicate()
        if proc1.returncode != 0:
            raise ConnectionError
    except subprocess.CalledProcessError as e:
        handle_process_exception(e)
        raise ConnectionError
    if stdout and not stdout == '':
        self.balena_device_id = stdout.strip('\n')
        return
    # """curl -sSL "$BALENA_API_URL/v3/device?\$select=id,uuid&\$filter=uuid%20eq%20'$BALENA_DEVICE_UUID'"
    # -H "Authorization: Bearer $BALENA_API_KEY" | jq '.d[0].id'"""

```

I see the in `is_web_accessible` var the [api docs](https://www.balena.io/docs/reference/api/resources/device/) for the deivce, but I can’t find anywhere I could possibly set that.

Thanks,  
-Thomas

---

<div class="post-metadata">

**Author:** ![tmigone](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/tmigone/32/2814_2.png) [@tmigone](https://forums.balena.io/u/tmigone)\
**Post date:** [April 19, 2021, 7:23pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/6 "2021-04-19T19:23:25Z")

</div>

Hi @taclog, as far as I’m aware we don’t have a special API endpoint/method to set or toggle the public URL.

There is a way around this by using [Pine queries](https://www.balena.io/docs/reference/sdk/node-sdk/#balena.pine) (pine is our backend engine, handling our backend resources), but this will only work for our NodeJS SDK.  
Here is a snippet:

```auto

  const { getSdk } = require('balena-sdk')
  const balena = getSdk({
    apiUrl: "https://api.balena-cloud.com/"
  })

  await balena.auth.loginWithToken('AUTH_TOKEN')
  await balena.pine.patch({
    resource: 'device',
    id: DEVICE_ID,
    body: {
      is_web_accessible: true
    }
  })

```

AFAIK you can’t run pine queries from our python SDK. This doesn’t seem right to me so let me get back to you once I talk to the python guys, as I might be misreading the docs.

---

<div class="post-metadata">

**Author:** ![tmigone](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/tmigone/32/2814_2.png) [@tmigone](https://forums.balena.io/u/tmigone)\
**Post date:** [April 19, 2021, 7:32pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/8 "2021-04-19T19:32:19Z")

</div>

Actually, for the NodeJS there are specific methods to enable/disable publicURL:

- [Balena Node.js SDK - Balena Documentation](https://www.balena.io/docs/reference/sdk/node-sdk/#balena.models.device.enableDeviceUrl)
- [Balena Node.js SDK - Balena Documentation](https://www.balena.io/docs/reference/sdk/node-sdk/#devicedisabledeviceurluuidorid)

---

<div class="post-metadata">

**Author:** ![tacLog](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tacLog](https://forums.balena.io/u/tacLog)\
**Post date:** [April 19, 2021, 8:07pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/12 "2021-04-19T20:07:10Z")

</div>

Thanks @tmigone that’s very helpful.

I am a bit new to node.js but I tired to throw something together.  
Could I run this from python by pathing together two simple node scripts?

Add this to my Dockerfile.template

> RUN apt-get update && install -y \  
> nodejs npm && \  
> apt-get clean && \  
> npm install --save balena-sdk  
> WORKDIR /usr/src/app  
> RUM mkdir /usr/src/app/balena

Then setting up a node script like this:

> const { getSdk } = require(‘balena-sdk’);
> 
> const balena = getSdk({  
> apiUrl: “[https://api.balena-cloud.com/](https://api.balena-cloud.com/)”,  
> dataDirectory: “/usr/src/app/balena”  
> });
> 
> (async () =\> {  
> await balena.auth.loginWithToken(process.env.BALENA\_API\_KEY);  
> await balena.models.device.disableDeviceUrl(process.env.BALENA\_DEVICE\_UUID);  
> console.log(“Url turned off”);  
> })();

And call it from python with: `os.system('node turnOffUrl.js')`

~~Does that seem like it would work? Can you log into the sdk with the `BALENA_API_KEY` env var from an application on a device?~~

This works, I just tested it. @tmigone Thanks for your help!

-Thomas

---

<div class="post-metadata">

**Author:** ![tacLog](https://avatars.discourse-cdn.com/v4/letter/t/ecae2f/32.png) [@tacLog](https://forums.balena.io/u/tacLog)\
**Post date:** [April 19, 2021, 9:33pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/13 "2021-04-19T21:33:52Z")

</div>

Also for other poeple that find this the python sdk also supports this:

> **[Balena Python SDK - Balena Documentation](https://www.balena.io/docs/reference/sdk/python-sdk/#function-disable_device_urluuid)**

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [April 20, 2021, 10:26am UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/14 "2021-04-20T10:26:34Z")

</div>

Thank you for sharing how it worked for you @taclog

---

<div class="post-metadata">

**Author:** ![rhampt](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/rhampt/32/7274_2.png) [@rhampt](https://forums.balena.io/u/rhampt)\
**Post date:** [June 13, 2024, 6:29pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/16 "2024-06-13T18:29:05Z")

</div>

Hi everyone, just jumping back in to explain that it _is_ in fact possible now to set Public Device URL via the API. Here is an example with curl:

```auto
curl -X PATCH \
"https://api.balena-cloud.com/v6/device(<ID>)" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTH_TOKEN>" \
--data '{
    "is_web_accessible": "true"
}'

```

---

<div class="post-metadata">

**Author:** ![rhampt](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/rhampt/32/7274_2.png) [@rhampt](https://forums.balena.io/u/rhampt)\
**Post date:** [June 13, 2024, 6:29pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/17 "2024-06-13T18:29:53Z")

</div>

And here is an example with the cli from [balena CLI Documentation - Balena Documentation](https://docs.balena.io/reference/balena-cli/#device-public-url-uuid)

```auto
$ balena device public-url 23c73a1
$ balena device public-url 23c73a1 --enable
$ balena device public-url 23c73a1 --disable
$ balena device public-url 23c73a1 --status

```

---

<div class="post-metadata">

**Author:** ![rhampt](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/rhampt/32/7274_2.png) [@rhampt](https://forums.balena.io/u/rhampt)\
**Post date:** [June 13, 2024, 6:31pm UTC](https://forums.balena.io/t/turning-on-and-off-the-public-device-url-from-code-on-the-device/297082/18 "2024-06-13T18:31:13Z")

</div>

And here is how you do it with PineJS:

```javascript
pine.patch({ resource: 'device', id: UUID, { is_web_accessible: BOOLEAN } })

```
