# Implement Web Thing API

**URL:** <https://discourse.mozilla.org/t/implement-web-thing-api/66280>\
**Category:** WebThings\
**Created:** [August 24, 2020, 8:50am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280 "2020-08-24T08:50:17Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 24, 2020, 8:50am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/1 "2020-08-24T08:50:17Z")

</div>

Hi  
I don’t know how can I have access to the URL of Web Thing API to call it .  
for example I want to send the request of turning the lamp on, but I can’t figure out what to call for this action.  
or how to implement this API on our devices…?

---

<div class="post-metadata">

**Author:** ![mrstegeman](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@mrstegeman](https://discourse.mozilla.org/u/mrstegeman)\
**Post date:** [August 24, 2020, 5:58pm UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/2 "2020-08-24T17:58:33Z")

</div>

Are you wanting to use the API on the gateway or on a standalone web thing?

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 25, 2020, 6:00am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/3 "2020-08-25T06:00:27Z")

</div>

hi,  
on the gateway…

---

<div class="post-metadata">

**Author:** ![mrstegeman](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@mrstegeman](https://discourse.mozilla.org/u/mrstegeman)\
**Post date:** [August 25, 2020, 6:41am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/4 "2020-08-25T06:41:07Z")

</div>

1. Get an OAuth token via \_Settings -\> Developer -\> Create local authorization`
  - That screen will tell you how to use the token.

2. Get the list of thing descriptions via `GET /things`, or an individual thing description via `GET /things/<thing-id>`. [[documentation 1]](https://iot.mozilla.org/wot/#things-resource) [[documentation 2]](https://iot.mozilla.org/wot/#thing-resource)
  - The thing description will have the URLs of the various properties, actions, etc.

3. To perform an action, use `POST /things/<thing-id>/actions` with the JSON content of the action. [[documentation]](https://iot.mozilla.org/wot/#actions-resource)

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 25, 2020, 9:24am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/5 "2020-08-25T09:24:00Z")

</div>

where should I write this URL "POST /things//actions "for each thing??  
when I write this URL with real values in the address bar nothing happens!  
I don’t know how and where to use these URLs and local token 😔

---

<div class="post-metadata">

**Author:** ![PeskyPotato](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/peskypotato/32/38610_2.png) [@PeskyPotato](https://discourse.mozilla.org/u/PeskyPotato)\
**Post date:** [August 25, 2020, 9:34am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/6 "2020-08-25T09:34:14Z")

</div>

> [@mahsa.mohammadpourghaeni](#):
>
> where should I write this URL "POST /things//actions "for each thing??

Just so I know how your setup currently is, do you have the Mozilla Gateway fully setup? If so do you have registered devices on the Gateway that you can interact with from the browser? Here’s an example of how that would look.

 ![Things on Gateway](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/f/0/f022048dd1b53748e3df3fab5d9ecd2e3698d7e0.png)

Once you have that setup and working you can create an OAuth token as mrstegemam said through the Developer Settings. Once you create a token you will see a page with the token and various ways you can use it to see all the registered Things, including Python and Curl. Here’s what that page will look like.

 ![token](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/6/7/67f53ebb1ded591a413c6ee147e8e6705e84b25d.png)

If this works you can use WoT API to interact with the devices which has been linked by mrstegeman above.

Hope this helps.

: )

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 25, 2020, 9:38am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/7 "2020-08-25T09:38:37Z")

</div>

thanks,but I did all these things you said.  
my problem is the codes in the last picture that you send. where should I write one of those codes?for example the javascript code.

---

<div class="post-metadata">

**Author:** ![PeskyPotato](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/peskypotato/32/38610_2.png) [@PeskyPotato](https://discourse.mozilla.org/u/PeskyPotato)\
**Post date:** [August 25, 2020, 9:45am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/8 "2020-08-25T09:45:40Z")

</div>

The JavaScript code can be run in the browser, for example, I just created an HTML file and was able to log out the results and view it from the console on my browser.

The code is below, if you copy it into a file, say `index.html`, open it with your browser and go to the console you will see an array of your devices and their Thing Description and schema. Replace `<TOKEN_HERE>` with your generated token.

```
<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script>
        fetch('http://localhost:8080/things', {
            headers: {
            Accept: 'application/json',
            Authorization: 'Bearer <TOKEN_HERE>'
        }
        }).then(res => {
            return res.json();
        }).then(things => {
            console.log(things);
        });
    </script>
</body>
</html>
```

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 25, 2020, 11:41am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/9 "2020-08-25T11:41:56Z")

</div>

thanks a lot, 😊  
I’m sorry but I have another question. I Got my properties with GET command and are like below:  
{“on”:false,“brightness”:50}  
but when I write the PUT method to make the on property true like the format below,It doesn’t work and Console shows nothing.  
:  
xhr.open(‘PUT’, [http://mythingserver.com/things/id/property/on](http://mythingserver.com/things/id/property/on)  
{  
“on”: true  
});  
but if I write like this: xhr.open(‘PUT’, [http://mythingserver.com/things/id/property/on](http://mythingserver.com/things/id/property/on)); Console says : Cannot PUT

what’s my mistake?

---

<div class="post-metadata">

**Author:** ![PeskyPotato](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/peskypotato/32/38610_2.png) [@PeskyPotato](https://discourse.mozilla.org/u/PeskyPotato)\
**Post date:** [August 25, 2020, 8:43pm UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/10 "2020-08-25T20:43:11Z")

</div>

I’ve tested the code below and it works for setting the “on” property. Just make sure to replace `<thing-id>` and `<token>`.

```
var xhr = new XMLHttpRequest();
xhr.open("PUT", 'http://localhost:8080/things/<thing-id>/properties/on', true);
xhr.setRequestHeader('Content-type','application/json);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Authorization', 'Bearer <token>');
xhr.addEventListener('load', function() {
    console.log(this.responseText);
});
xhr.send('{"on":true}');

```

Hopefully this helps.

: )

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 26, 2020, 6:57am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/11 "2020-08-26T06:57:58Z")

</div>

Thank you so much,It works.I appreciate your guidance.

another thing that is ambiguous to me is the command that gateway sends to devices.  
actually we send these POST , GET and PUT methods to the gateway , but I really want to know when the gateway gets our commands, what does it send to devices in order to interpret our command for devices.

for example when we send put method to turn the on property false,what does the gateway send to that device to make it false.  
I want to know the model or format of it’s code.

---

<div class="post-metadata">

**Author:** ![PeskyPotato](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/peskypotato/32/38610_2.png) [@PeskyPotato](https://discourse.mozilla.org/u/PeskyPotato)\
**Post date:** [August 26, 2020, 8:33am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/12 "2020-08-26T08:33:16Z")

</div>

Glad I was able to help.

If it is a native WoT device then the Gateway communicates through the WebThing API or WebSockets just like you do with the Gateway. The API is the same as the Gateway with the exception of the URL being different since you are now targeting the device.

If it’s not a WoT device the communication goes through the installed adapter and the adapter communicates in whatever propriety protocol is used for the device.

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [August 26, 2020, 10:29am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/13 "2020-08-26T10:29:15Z")

</div>

thank you,  
how can I understand that how tokens are produced?  
Are they produced from information of devices that are selected?

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [September 28, 2020, 6:34am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/14 "2020-09-28T06:34:37Z")

</div>

hello again,  
what did you do that it recognized your (localhost:8080)?  
why when I put the address of my project localhost ,it doesn’t work?  
instead of localhost ,it just understand ip of mozilla getway but i’m writing a web server on windows and want to put my web server url like you instead of mozilla gateway.  
it returns cors error.

---

<div class="post-metadata">

**Author:** ![PeskyPotato](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/peskypotato/32/38610_2.png) [@PeskyPotato](https://discourse.mozilla.org/u/PeskyPotato)\
**Post date:** [October 3, 2020, 4:49pm UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/15 "2020-10-03T16:49:32Z")

</div>

Not sure about that, I just set up the development environment locally never registered a domain with Mozilla.

---

<div class="post-metadata">

**Author:** ![mahsa.mohammadpourghaeni](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@mahsa.mohammadpourghaeni](https://discourse.mozilla.org/u/mahsa.mohammadpourghaeni)\
**Post date:** [October 5, 2020, 6:02am UTC](https://discourse.mozilla.org/t/implement-web-thing-api/66280/16 "2020-10-05T06:02:52Z")

</div>

hello again,  
on a standalone web thing  
🙏
