This document describes a common data model and API for the Web of Things. The Web Thing Description provides a vocabulary for describing physical devices connected to the World Wide Web in a machine readable format with a default JSON encoding. Common device capabilities can be specified using optional semantic annotations. The Web Thing REST API and Web Thing WebSocket API allow a web client to access the properties of devices, request the execution of actions and subscribe to events representing a change in state.
Note that the Web Thing Description section is currently being standardised by the W3C Web of Things Working Group and the Web Thing REST API and Web Thing WebSocket API sections are being standardised by the W3C Web Thing Protocol Community Group.
This specification started as a draft W3C member submission by Mozilla, but continues to be maintained to document the API currently implemented by the WebThings IoT platform while it converges with the W3C standards as they mature.
The goal of the Web of Things is to extend the web of pages into a web of things by giving connected devices URLs on the World Wide Web. This will allow the web to be used as a unifying application layer for a decentralized Internet of Things.
Whilst web technologies are already in widespread use on the Internet of Things, this is currently done with mostly proprietary data formats and APIs which require per-vendor integrations to make devices interoperable. In order to promote ad-hoc interoperability on the Internet of Things a shared vocabulary and common API is needed.
In this document we propose a common data model and API for the Web of Things.
The Thing Description provides a vocabulary for describing physical devices connected to the World Wide Web in a machine readable format with a default JSON encoding.
{ "id": "https://mywebthingserver.com/things/switch", "title": "On/Off Switch", "description": "A web connected switch", "properties": { "on": { "title": "On/Off", "type": "boolean", "description": "Whether the lamp is turned on", "links": [{"href": "/things/switch/properties/on"}] } } }
{ "@context": "https://webthings.io/schemas/", "@type": ["Light", "OnOffSwitch"], "id": "https://mywebthingserver.com/things/lamp", "title": "My Lamp", "description": "A web connected lamp", "properties": { "on": { "@type": "OnOffProperty", "type": "boolean", "title": "On/Off", "description": "Whether the lamp is turned on", "links": [{"href": "/things/lamp/properties/on"}] }, "brightness" : { "@type": "BrightnessProperty", "type": "integer", "title": "Brightness", "description": "The level of light from 0-100", "minimum" : 0, "maximum" : 100, "links": [{"href": "/things/lamp/properties/brightness"}] } }, "actions": { "fade": { "@type": "FadeAction", "title": "Fade", "description": "Fade the lamp to a given level", "input": { "type": "object", "properties": { "level": { "type": "integer", "minimum": 0, "maximum": 100 }, "duration": { "type": "integer", "minimum": 0, "unit": "milliseconds" } } }, "links": [{"href": "/things/lamp/actions/fade"}] } }, "events": { "overheated": { "title": "Overheated", "@type": "OverheatedEvent", "type": "number", "unit": "degree celsius", "description": "The lamp has exceeded its safe operating temperature", "links": [{"href": "/things/lamp/events/overheated"}] } }, "links": [ { "rel": "properties", "href": "/things/lamp/properties" }, { "rel": "actions", "href": "/things/lamp/actions" }, { "rel": "events", "href": "/things/lamp/events" }, { "rel": "alternate", "href": "wss://mywebthingserver.com/things/lamp" }, { "rel": "alternate", "mediaType": "text/html", "href": "/things/lamp" } ] }
@context
member
The @context
member is an optional annotation which can be used to provide a URI for a schema repository which defines standard schemas for common "types" of device capabilities. These types can then be referred to
using @type
annotations elsewhere in the Thing Description.
"@context": "https://webthings.io/schemas"
@type
member
The @type
member is an optional annotation which can be used to provide the names of schemas for types of capabilities a device supports, from a schema repository referred to in the @context
member. The
value of the @type
member is a string or an array of strings which correspond to schema names from a schema repository.
Providing a @type
annotation tells a client that it can expect the device to conform to the constraints of that type's schema. This may include certain types of property, action and event the device can be expected to
support. A client may make use of this information to aid in automation of or to generate a user interface for known standard device capabilities.
"@type": ["Light", "OnOffSwitch"]
id
memberThe id member provides an identifier of the device in the form of a URI [[RFC3986]] (e.g. a URL or a URN). This may be the URL of the Thing Description itself, but can be any URI.
"id": "https://mywebthingserver.com/things/lamp"
title
memberThe title member is a human friendly string which describes the device. This can be set to any value by the device creator and may include a brand name or model number.
description
memberThe description member is a human friendly string which describes the device and its functions. This can be set to any value by the device creator.
properties
memberThe properties member is a map of Property objects which describe the attributes of the device.
actions
memberThe actions member is a map of Action objects which describe functions that can be carried out on a device. Actions are used when the setting of a property alone is not sufficient to affect a required change in state. This may be used to describe a function which takes a period of time to complete, manipulates multiple properties, or has a different outcome based on provided arguments or current state. An example might include a "fade" action which has a specified "duration", a "sequence" action which triggers a sequence of flashing lights or a "toggle" action on a switch.
events
memberThe events member is a map of Event objects which define the types of events which may be emitted by a device. Events can be used where subscribing to a change in property state is not sufficient. This may be used to monitor changes in multiple properties or events which describe something other than a property change. An example might be an event which is emitted when a device is about to reboot.
links
memberThe links member provides an array of Link Objects which link to other resources of a thing.
Examples of links include a link to a Properties resource ("rel":"properties"
), an Actions resource ("rel":"actions"
) or an
Events resource ("rel":"events"
). It can also include links to alternate representations of a thing such as a WebSocket API endpoint or an HTML user interface
("rel":"alternate"
).
"links": [ { "rel": "properties", "href": "/things/lamp/properties" }, { "rel": "actions", "href": "/things/lamp/actions" }, { "rel": "events", "href": "/things/lamp/events" }, { "rel": "alternate", "href": "wss://mywebthingserver.com/things/lamp" }, { "rel": "alternate", "mediaType": "text/html", "href": "/things/lamp" } ]
Property
objectA property object describes an attribute of a Thing and is indexed by a property id. A property description may include:
type
(one of null, boolean, object, array, number, integer or string as per [[!json-schema]])@type
(a string identifying a type from the linked @context
)
unit
([[!SI]] unit)title
(A string providing a human friendly name)description
(A string providing a human friendly description)links
array (An array of Link objects linking to one or more representations of a Property resource, each with an implied default
rel=property
.)
enum
(an enumeration of possible values for the property)readOnly
(A boolean indicating whether or not the property is read-only, defaulting to false)minimum
and maximum
(numeric values)multipleOf
(A number indicating what the value must be a multiple of)"level" : { "title": "Level", "description": "The level of light from 0-100", "@type": "LevelProperty", "type": "integer", "unit": "percent", "minimum": 0, "maximum": 100, "readOnly": false, "links": [{"href": "/things/lamp/properties/level"}] }
Action
objectAn action object describes a function which can be carried out on a device. An action definition may include:
title
(A string providing a human friendly name)description
(A string providing a human friendly description)links
array (An array of Link objects linking to one or more representations of an Action resource, each with an implied default rel=action
.)
input
object with a:
type
(one of null, boolean, object, array, number, integer or string as per [[!json-schema]])@type
(a string identifying a type from the linked @context
)
unit
([[!SI]] unit)minimum
and maximum
(numeric values)multipleOf
(A number indicating what the value must be a multiple of)"fade": { "@type": "FadeAction", "title": "Fade", "description": "Fade the lamp to a given level", "input": { "type": "object", "properties": { "level": { "type": "integer", "minimum": 0, "maximum": 100 }, "duration": { "type": "integer", "minimum": 0, "unit": "milliseconds" } } }, "links": [{"href": "/things/lamp/actions/fade"}] }
Event
objectAn event object describes a kind of event which may be emitted by a device. This may include:
type
(one of null, boolean, object, array, number, integer or string as per [[!json-schema]])@type
(a string identifying a type from the linked @context
)
unit
([[!SI]] unit)title
(A string providing a human friendly name)description
(A string providing a human friendly description)links
array (An array of Link objects linking to one or more representations of an Event resource, each with an implied default rel=event
).
minimum
and maximum
(numeric values)multipleOf
(A number indicating what the value must be a multiple of)"overheated": { "title": "Overheated", "description": "The lamp has exceeded its safe operating temperature", "@type": "OverheatedEvent", "type": "number", "unit": "degree celsius", "links": [{"href": "/things/lamp/events/overheated"}] }
Link
objectA link object represents a link relation ([[!web-linking]]) and may have a:
href
(a string representation of a URL)rel
(a string describing a relationship)mediaType
(a string identifying a media type){ "href": "/things/lamp/properties", "rel": "properties", "mediaType": "application/json" }
The JSON serialization of the Web Thing Description is identified by the Media Type application/td+json
.
The default encoding for the Thing description is JSON, but other encodings such as XML, JSON-LD or CBOR may be used. Alternative encodings are beyond the scope of this specification but may be defined separately.
References to alternative available encodings may be provided using Link HTTP response headers with the alternate link relation. A client can express a preference for an alternative encoding using HTTP content negotiation.
The Web Thing API provides a web services based programming interface with a RESTful design for applications to access the properties of devices, request the execution of actions and access a list of events representing a change in state. A default HTTP [[!HTTP11]] protocol binding is defined here.
The Web Thing REST API consists of a number of different types of resources which represent different aspects of a device and can be independently referenced by a URL. The specific URLs and URL structure of resources are defined by the Thing Description. This specification does not define a fixed URL structure.
Thing
resourceA thing Resource provides a Thing Description for a device. A Thing Resource is considered the root resource of a Thing.
The URL of a Thing Resource may be enumerated by the href
member of Thing object in a Things Resource provided by a gateway or directory, or may be discovered by some other means.
A Thing description is usually read only. An HTTP GET request can be used to retrieve a Thing Description for a Thing.
Example: Get a description of a Thing
GET http://mythingserver.com/things/pi Accept: application/json
200 OK { "id": "https://mywebthingserver.com/things/pi", "title": "WoT Pi", "description": "A WoT-connected Raspberry Pi", "properties": { "temperature": { "title": "Temperature", "type": "number", "unit": "degree celsius", "readOnly": true, "description": "An ambient temperature sensor", "links": [{"href": "/things/pi/properties/temperature"}] }, "humidity": { "title": "Humidity", "type": "number", "unit": "percent", "readOnly": true, "links": [{"href": "/things/pi/properties/humidity"}] }, "led": { "title": "LED", "type": "boolean", "description": "A red LED", "links": [{"href": "/things/pi/properties/led"}] } }, "actions": { "reboot": { "title": "Reboot", "description": "Reboot the device" } }, "events": { "reboot": { "description": "Going down for reboot" } }, "links": [ { "rel": "properties", "href": "/things/pi/properties" }, { "rel": "actions", "href": "/things/pi/actions" }, { "rel": "events", "href": "/things/pi/events" }, { "rel": "alternate", "href": "wss://mywebthingserver.com/things/pi" }, { "rel": "alternate", "mediaType": "text/html", "href": "/things/pi" } ] }
Properties
resourceA properties resource represents all the properties of a device. The value of all properties can be retrieved with an HTTP GET request.
The URL of a Properties resource can be defined by a properties
link relation in the links
member of a Thing Description.
Example: Get all properties
GET http://mythingserver.com/things/pi/properties Accept: application/json
200 OK { "temperature": 21, "humidity": 50, "led": true }
Property
resourceA property resource represents a single property of a device. Some property resources may be read only and some may be writable. The value of a property can be retrieved with an HTTP GET request and updated with an HTTP PUT request.
The URL of a Property resource can be defined by the links
section of a Property object in a Thing Description.
If a Property object does not define a primitive type
it may not have a JSON serialisation and may instead return a binary file or stream in response to an HTTP GET request (e.g. an image file or video stream).
Example: Get a property
GET http://mythingserver.com/things/pi/properties/temperature Accept: application/json
200 OK { "temperature": 21 }
Example: Set a property
PUT http://mythingserver.com/things/pi/properties/led { "led": true } Accept: application/json
200 OK { "led": true }
Actions
resourceAn actions resource represents a queue of actions to be executed on a device. A new action is created in the queue with an HTTP POST request and a list of action requests in the queue can be requested with an HTTP GET request.
The URL of an actions resource can be defined by an actions
link relation in the links
member of a Thing Description.
Any action supported by the thing can be requested via this top level action queue. If an unsupported action type is requested, the server should respond with a 400 Bad Request
response.
Action Request
POST https://mythingserver.com/things/lamp/actions/ Accept: application/json { "fade": { "input": { "level": 50, "duration": 2000 } } }
201 Created { "fade": { "input": { "level": 50, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655", "status": "pending" } }
Actions Queue
GET /things/lamp/actions Accept: application/json
200 OK [ { "fade": { "input": { "level": 50, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655", "timeRequested": "2017-01-25T15:01:35+00:00", "status": "pending" } }, { "reboot": { "href": "/things/lamp/actions/reboot/124e4568-f89b-22d3-a356-427656", "timeRequested": "2017-01-24T13:13:33+00:00", "timeCompleted": "2017-01-24T13:15:01+00:00", "status": "completed" } } ]
Action
resourceAn action resource represents a queue of actions of a single action type. A new action is created in the queue with an HTTP POST request and a list of action requests in the queue can be requested with an HTTP GET request.
The URL of an Action resource can be defined by the links
section of an Action object in a Thing Description.
If a client tries to request an action of another type via this resource, the server should respond with a 400 Bad Request
response.
Action Request
POST https://mythingserver.com/things/lamp/actions/fade Accept: application/json { "fade": { "input": { "level": 50, "duration": 2000 } } }
201 Created { "fade": { "input": { "level": 50, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655" "status": "pending" } }
Action Queue
GET /things/lamp/actions/fade Accept: application/json
200 OK [ { "fade": { "input": { "level": 50, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655", "timeRequested": "2017-01-25T15:01:35+00:00", "status": "pending" } }, { "fade": { "input": { "level": 100, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655", "timeRequested": "2017-01-24T11:02:45+00:00", "timeCompleted": "2017-01-24T11:02:46+00:00", "status": "completed" } } ]
ActionRequest
resourceAn action request resource represents an individual action request for a given action. The current status of an action request can be retrieved with an HTTP GET request, updated with an HTTP PUT request and deleted with an HTTP DELETE request.
The URL of an Action request resource can be defined by the href
member of an action request object in an action queue.
Action Request Status
GET /things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655 Accept: application/json
200 OK { "fade": { "input": { "level": 50, "duration": 2000 }, "href": "/things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655", "timeRequested": "2017-01-25T15:01:35+00:00", "status": "pending" } }
Cancel an Action Request
DELETE /things/lamp/actions/fade/123e4567-e89b-12d3-a456-426655
204 No Content
Events
resourceAn events resource provides a log of all events recently emitted by a device. An events resource is usually read only.
The URL of an Events resource can be defined by an events
link relation in the links
member of a Thing Description.
Event Log
GET /things/lamp/events Accept: application/json
200 OK [ { "overheated": { "data": 102, "timestamp": "2017-01-25T15:01:35+00:00" } }, { "reboot": { "timestamp": "2017-01-24T13:02:45+00:00" } } ]
Event
resourceAn event resource provides a log of events recently emitted by a device for a particular event type. An event resource is usually read only.
The URL of an Event resource can be defined by the links
section of an Event object in a Thing Description.
Event Log
GET /things/lamp/events/overheated Accept: application/json
200 OK [ { "overheated": { "data": 102, "timestamp": "2017-01-25T15:01:35+00:00" } }, { "overheated": { "data": 101, "timestamp": "2017-01-24T13:02:45+00:00" } } ]
Things
resourceA things resource represents a collection of web things exposed by a particular Web of Things server. This resource is not needed for individual web things, for which the Thing Description is the entry point. It is used only by Web of Things gateways and services which expose a directory of web things.
Example: Get a list of things
GET /things Accept: application/json
200 OK [ { "id": "https://mywebthingserver.com/things/pi", "title": "WoT Pi", "description": "A WoT-connected Raspberry Pi", "properties": { "temperature": { "title": "Temperature", "type": "number", "unit": "degree celsius", "readOnly": true, "description": "An ambient temperature sensor", "links": [{"href": "/things/pi/properties/temperature"}] }, "humidity": { "title": "Humidity", "type": "number", "unit": "percent", "readOnly": true, "links": [{"href": "/things/pi/properties/humidity"}] }, "led": { "title": "LED", "type": "boolean", "description": "A red LED", "links": [{"href": "/things/pi/properties/led"}] } }, "actions": { "reboot": { "title": "Reboot", "description": "Reboot the device" } }, "events": { "reboot": { "description": "Going down for reboot" } }, "href": "/things/pi" } ]
The default protocol binding for the Web Thing REST API is HTTP(S). Bindings to alternative application protocols (e.g. CoAP) may be used, but these bindings are beyond the scope of this specification. A Web Thing API protocol binding may also be layered on top of a non-Internet application protocol by use of a gateway.
The Web Thing WebSocket API complements the REST API to provide a realtime mechanism to make multiple requests and be notified of events as soon as they happen, by keeping a WebSocket [[!WEBSOCKETS-PROTOCOL]] open on the Web Thing. The "webthing" WebSocket subprotocol defined here has a simple set of message types and a JSON response format consistent with the Web Thing REST API.
To open a WebSocket on a Thing, an HTTP GET request is upgraded to a WebSocket using a standard WebSocket protocol handshake [[!WEBSOCKETS-PROTOCOL]] and the "webthing" subprotocol. The WebSocket URL for a Web Thing is specified in the links member of the Web Thing Description.
GET wss://mythingserver.com/things/robot Host: mythingserver.com Origin: https://mythingserver.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: webthing Sec-WebSocket-Version: 13
HTTP 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: webthing
A WebSocket can be opened from a web page using the JavaScript WebSocket API which will take care of the handshake detailed above and allow messages to be sent and received.
const socket = new WebSocket('wss://mywebthingserver/things/robot', 'webthing');
setProperty
message
The setProperty
message type is sent from a web client to a Web Thing in order to set the value of one or more of its properties. This is equivalent to a PUT
request on a Property resource URL using the
REST API, but with the WebSocket API a property value can be changed multiple times in quick succession over an open socket and multiple properties can be set at the same time.
{ "messageType": "setProperty", "data": { "leftMotor": 100 } }
requestAction
message
The requestAction
message type is sent from a web client to a Web Thing to request an action be carried out on a Web Thing. This is equivalent to a POST
request on an Actions resource URL using the REST
API, but multiple actions can be requested at the same time or in quick succession over an open socket.
{ "messageType": "requestAction", "data": { "goForward": {}, } }
addEventSubscription
message
The addEventSubscription
message type is sent from a web client to a Web Thing to allow a client to subscribe to a particular event type, defined by the events
member of a Web Thing description. This is
similar to adding an event listener in JavaScript, but events are received as an event
message over the Web Thing WebSocket API.
{ "messageType": "addEventSubscription", "data": { "motion": {} } }
propertyStatus
message
The propertyStatus
message type is sent from a Web Thing to a web client whenever a property of a Web Thing changes. The payload data of this message is in the same format as a response to a GET
request
on Property resource using the REST API, but the message is pushed to the client whenever a property changes and can include multiple properties at the same time.
{ "messageType": "propertyStatus", "data": { "led": true } }
actionStatus
message
The actionStatus
message type is sent from a Web Thing to a web client when the status of a requested action changes. The payload data is consistent with the format of an Action resource in the REST API, but messages
are pushed to the client as soon as the status of an action changes.
{ "messageType": "actionStatus", "data": { "grab": { "href": "/actions/grab/123e4567-e89b-12d3-a456-426655", "status": "completed", "timeRequested": "2017-01-24T11:02:45+00:00", "timeCompleted": "2017-01-24T11:02:46+00:00" } } }
event
message
The event
message type is sent from a Web Thing to a web client when an event occurs on the Web Thing. The payload data is consistent with the format of an Event resource in the REST API but messages are pushed to the
client as soon as an event occurs.
{ "messageType": "event", "data": { "motion": { "timestamp": "2017-01-24T13:02:45+00:00" } } }
Web Things can make themselves discoverable to clients via various methods.
A Web Thing can make itself discoverable on a local network by advertising itself via DNS-SD, with a service type of webthing
over TCP, i.e. _webthing._tcp
. The service should define a TXT record named
path
, with its value denoting the URI of the thing description on the Web Thing's web server, e.g. path=/things/lamp
. Additionally, a TXT record named tls
with a value of 1
should be included if the Web Thing supports connections via HTTPS.
Web of Things Integration Patterns
Advice on different design patterns for integrating connected devices with the Web of Things.