Implement Web Thing API

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…?

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

1 Like

hi,
on the gateway…

  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] [documentation 2]
    • 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]

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 :pensive:

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.

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.

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

Hope this helps.

: )

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.

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>
1 Like

thanks a lot, :blush:
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
{
“on”: true
});
but if I write like this: xhr.open(‘PUT’, http://mythingserver.com/things/id/property/on); Console says : Cannot PUT

what’s my mistake?

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.

: )

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.

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.

1 Like

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

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.

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

hello again,
on a standalone web thing
:pray: