# How we can execute shell command from custom widget using code

**URL:** <https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109>\
**Category:** Forum\
**Tags:** device-connectivity, cumulocity\
**Created:** [April 12, 2023, 10:41am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109 "2023-04-12T10:41:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumit.singh](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sumit.singh](https://community.cumulocity.com/u/sumit.singh)\
**Post date:** [April 12, 2023, 10:41am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/1 "2023-04-12T10:41:59Z")

</div>

#### _Product/components used and version/fix level are you on:_

Backend: **1015.0.278**  
UI: **1011.0.38**

#### _Detailed explanation of the problem:_

I have custom widget in angular on top of Application Builder

 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/2/5/2572e4cf92c6d5e3c1b3b77fd14a9648f840b63a.png)  
Widget Look like above image and below is the widget code in my IDE  
 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/3/3/332dcedc49f1dd566e61099663242a4c058795b3.png)

now i want to Execute shell command from this widget code to cumulocity shell till now i have not done any Cumulocity connection and all so how i can execute shell command from custom widget code and what steps I have to do for Executing shell command from custom widget code then from widget code I will call a Alarm Rest Api from widget code once shell command executed from custom widget code then we will use values of rest api in custom widget.

how can I achieve this any docs or info will be helpful.

I want :-

1. Execute Shell command from custom widget code
2. once done with shell command execution in custom widget code I want to call my alarm rest API and use these values in custom widget

is there any approach for achieve this ?

#### _Error messages / **full error** message screenshot / log fileL_

#### _Is your question related to the free trial, or to a production (customer) instance?_

#### _Have you installed all the latest fixes for the products and systems you are using?_

---

<div class="post-metadata">

**Author:** ![Darpankumar\_Lalani](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darpankumar\_Lalani](https://community.cumulocity.com/u/Darpankumar_Lalani)\
**Post date:** [April 13, 2023, 5:19am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/2 "2023-04-13T05:19:27Z")

</div>

Hi Sumit,

All Communication from UI(angular) app to backend is via REST APIs.  
I am not sure what are you referring here as “Shell Command”.  
Can you please share more details.

---

<div class="post-metadata">

**Author:** ![sumit.singh](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sumit.singh](https://community.cumulocity.com/u/sumit.singh)\
**Post date:** [April 13, 2023, 7:09am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/3 "2023-04-13T07:09:42Z")

</div>

Thank You @Darpankumar_Lalani for your valuable response  
From my main application I am able to call my REST API over MQTT  
my device is connected to Cumulocity and my main UI

 ![cloud connect](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/7/e/7e9a07c5cae480c5bd871611d7b40ec499713440.png)  
refer the above image here my Cumulocity cloud is connected to Cumulocity now when i am sending from device shell this payload from shell  
 ![cumulocity shell payload](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/3/d/3dbea434a1ae97874e58a935389b3148759814be.png)  
refer the above image here i am executing this payload from connected device and I am able to call my REST API in same manner I want to call my REST API in widget code  
 ![image](https://europe1.discourse-cdn.com/flex005/uploads/cumulocity/original/3X/d/6/d6414c1a3c3f985e7e93b5b601ae372bfddd36ff.png)  
here i want to call my REST API how can I achieve this ?  
doing all the because the REST API needed AUTH token that’s why I am doing all this so please help me with this scenario how can I call my REST API from this widget code

all these I am doing over MQTT.

a little help can resolve my problem my end goal is to call REST API how can I achieve this in my custom widget code if I don’t have auth token, what kind of things I need to do REST API call from my custom run time widget code .

---

<div class="post-metadata">

**Author:** ![sumit.singh](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sumit.singh](https://community.cumulocity.com/u/sumit.singh)\
**Post date:** [April 13, 2023, 10:00am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/4 "2023-04-13T10:00:11Z")

</div>

I am stuck to call rest api from my widget code , I am connected to my device and cumulocity thought my product application there i am easily able to call my rest api over mqtt here how can i call rest api over mqtt

is there any way or approach to achieve this ?

---

<div class="post-metadata">

**Author:** ![Darpankumar\_Lalani](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darpankumar\_Lalani](https://community.cumulocity.com/u/Darpankumar_Lalani)\
**Post date:** [April 13, 2023, 10:59am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/5 "2023-04-13T10:59:00Z")

</div>

If I understood correctly, you are looking for Operation API.  
Please check below URL about how to call Operation Rest APIs from UI. [http://resources.cumulocity.com/documentation/websdk/client/classes/operationservice.html](http://resources.cumulocity.com/documentation/websdk/client/classes/operationservice.html)

For More details about operations, you can refer [Device management - Cumulocity IoT Guides](https://cumulocity.com/guides/users-guide/device-management/#working-with-operations)

---

<div class="post-metadata">

**Author:** ![sumit.singh](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sumit.singh](https://community.cumulocity.com/u/sumit.singh)\
**Post date:** [April 13, 2023, 11:08am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/6 "2023-04-13T11:08:19Z")

</div>

Thank You @Darpankumar_Lalani  
But You didn’t understand it i just want to call my rest api over mqtt  
how i can call my rest api over mqtt in my custom widget code ?

some example or info will be enough, if you share with me related to call rest api over MQTT in my custom widget code .

---

<div class="post-metadata">

**Author:** ![Darpankumar\_Lalani](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darpankumar\_Lalani](https://community.cumulocity.com/u/Darpankumar_Lalani)\
**Post date:** [April 14, 2023, 4:55am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/7 "2023-04-14T04:55:00Z")

</div>

When you execute Shell command from device management (in your screenshot), it will call Operation API from cumulocity.  
You can verify that in browser developer tool by right-click in browser-\> inspect → network tab.  
In network tab you will find operation API rest call with your json payload.

As per my understanding, you want to call same API from your widget. In that case you need to use Operation REST APIs.

---

<div class="post-metadata">

**Author:** ![sumit.singh](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@sumit.singh](https://community.cumulocity.com/u/sumit.singh)\
**Post date:** [April 14, 2023, 5:45am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/8 "2023-04-14T05:45:22Z")

</div>

Thank You @Darpankumar_Lalani can you provide some code references or example to integrate this Operation API in my Angular based custom widget ?

---

<div class="post-metadata">

**Author:** ![Darpankumar\_Lalani](https://avatars.discourse-cdn.com/v4/letter/d/e9bcb4/32.png) [@Darpankumar\_Lalani](https://community.cumulocity.com/u/Darpankumar_Lalani)\
**Post date:** [April 14, 2023, 9:01am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/9 "2023-04-14T09:01:49Z")

</div>

You can refer Device Control Widget.

> **[GitHub - Cumulocity-IoT/cumulocity-device-control-widget: Widget to allow display and control of devices and...](https://github.com/Cumulocity-IoT/cumulocity-device-control-widget)**
>
> Widget to allow display and control of devices and groups of devices. Developed by Global Presales.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [October 11, 2023, 9:01am UTC](https://community.cumulocity.com/t/how-we-can-execute-shell-command-from-custom-widget-using-code/2109/10 "2023-10-11T09:01:55Z")

</div>

Closed on October 11, 2023, 9:01am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
