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..

This document includes a proposed plain JSON serialisation of a Thing Description and a concrete HTTP and WebSockets protocol binding for the Web of Things. This proposal is intended to complement the current W3C Web of Things Working Group's work on an abstract data model and API for the Web of Things, by defining a simple concrete serialisation and protocol binding using existing web technologies which can be used today, and don't require web clients to implement a new Scripting API, multiple non-web IoT protocols, or an RDF-based parser.

While the standardisation process continues, this document will continue to be maintained to reflect the current API implemented by Mozilla's own open source Web of Things implementation through Project Things. We encourage developers to continue to implement this API to build web things compatible with Mozilla's Things Gateway, and provide feedback to help us further improve the specification.

Introduction

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.

Web Thing Description

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.

{
  "name":"On/Off Switch",
  "description": "A web connected switch",
  "properties": {
    "on": {
      "label": "On/Off",
      "type": "boolean",
      "description": "Whether the lamp is turned on",
      "href": "/things/lamp/properties/on"
    }
  }
}
        
{
  "@context": "https://iot.mozilla.org/schemas/",
  "@type": ["Light", "OnOffSwitch"],
  "name":"My Lamp",
  "description": "A web connected lamp",
  "properties": {
    "on": {
      "@type": "OnOffProperty",
      "type": "boolean",
      "label": "On/Off",
      "description": "Whether the lamp is turned on",
      "href": "/things/lamp/properties/on"
    },
    "brightness" : {
      "@type": "BrightnessProperty",
      "type": "number",
      "label": "Brightness",
      "description": "The level of light from 0-100",
      "minimum" : 0,
      "maximum" : 100,
      "href": "/things/lamp/properties/brightness"
    }
  },
  "actions": {
    "fade": {
      "@type": "FadeAction",
      "label": "Fade",
      "description": "Fade the lamp to a given level",
      "input": {
        "type": "object",
        "properties": {
          "level": {
            "type": "number",
            "minimum": 0,
            "maximum": 100
          },
          "duration": {
            "type": "number",
            "unit": "milliseconds"
          }
        }
      },
      "href": "/things/lamp/actions/fade"
    }
  },
  "events": {
    "overheated": {
      "label": "Overheated",
      "@type": "OverheatedEvent",
      "type": "number",
      "unit": "celsius",
      "description": "The lamp has exceeded its safe operating temperature",
      "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.

Example experimental schema repositories can be found at http://iotschema.org/ and https://iot.mozilla.org/schemas.
"@context": "https://iot.mozilla.org/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"]

name member

The name 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 member

The 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 member

The properties member is a map of Property objects which describe the attributes of the device.

actions member

The 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 member

The 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 member

The links member provides an array of link relations ([[!web-linking]]) to other resources of a thing. Links can include a link relation type (rel) and an optional media type (mediaType). Examples of links include a link to a Properties resource ("rel":"properties"), an Actions resource ("rel":"actions") or an Events resource. 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 object

A property object describes an attribute of a Thing and is indexed by a property id. A property description may include its primitive type (one of null, boolean, object, array, number, integer or string as per [[!json-schema]]), semantic @type (as per the Thing Description's @context), unit ([[!SI]] unit), label (human friendly name), description (human friendly description), href (URL of a Property resource) and a minimum and maximum value.

"level" : {
  "label": "Level",
  "description": "The level of light from 0-100",
  "@type": "LevelProperty",
  "type": "number",
  "unit": "percent",
  "minimum" : 0,
  "maximum" : 100,
  "href": "/things/lamp/properties/level"
}
        

Action object

An action object describes a function which can be carried out on a device. An action definition may include a label (human friendly name), description (human friendly description) and a href (URL of an Action resource).

An action may also specify an input object with a primitive type (one of null, boolean, object, array, number, integer or string as per [[!json-schema]]), semantic @type (as per the Thing Description's @context), unit ([[!SI]] unit) and a minimum and maximum value.

"fade": {
  "label": "Fade",
  "description": "Fade the lamp to a given level",
  "input": {
    "@type": "FadeAction",
    "type": "object",
    "properties": {
      "level": {
        "type": "number",
        "minimum": 0,
        "maximum": 100
      },
      "duration": {
        "type": "number",
        "unit": "milliseconds"
      }
    }
  },
  "href": "/things/lamp/actions/fade"
}
      

Event object

An event object describes a kind of event which may be emitted by a device. This may include a primitive type (one of null, boolean, object, array, number, integer or string as per [[!json-schema]]), semantic @type (as per the Thing Description's @context), unit ([[!SI]] unit), label (human friendly name), description (human friendly description), href (URL of an Event resource) and a minimum and maximum value.

"overheated": {
  "label": "Overheated",
  "description": "The lamp has exceeded its safe operating temperature",
  "@type": "OverheatedEvent",
  "type": "number",
  "unit": "celsius",
  "href": "/things/lamp/events/overheated"
}
      

Media Type

The JSON serialization of the Web Thing Description is identified by the Media Type application/td+json.

This media type has not yet been registered with IANA.

Alternative Encodings

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.

Web Thing REST API

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 resource

A 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

Request
GET http://mythingserver.com/things/pi
Accept: application/json
Response
200 OK
{
  "name":"WoT Pi",
  "description": "A WoT-connected Raspberry Pi",
  "properties": {
    "temperature": {
      "label": "Temperature",
      "type": "number",
      "unit": "celsius",
      "description": "An ambient temperature sensor",
      "href": "/things/pi/properties/temperature"
    },
    "humidity": {
      "label": "Humidity",
      "type": "number",
      "unit": "percent",
      "href": "/things/pi/properties/humidity"
    },
    "led": {
      "label": "LED",
      "type": "boolean",
      "description": "A red LED",
      "href": "/things/pi/properties/led"
    }
  },
  "actions": {
    "reboot": {
      "label": "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 resource

A 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

Request
GET http://mythingserver.com/things/pi/properties
Accept: application/json
Response
200 OK
{
  "temperature": 21,
  "humidity": 50,
  "led": true
}

Property resource

A 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 href member of a Property object in a Thing Description.

Example: Get a property

Request
GET http://mythingserver.com/things/pi/properties/temperature
Accept: application/json
Response
200 OK
{
  "temperature": 21
}

Example: Set a property

Request
PUT http://mythingserver.com/things/pi/properties/led
{
  "led": true
}
Accept: application/json
Response
200 OK
{
  "led": true
}

Actions resource

An 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 resource

An 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 href member 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 resource

An 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 resource

An 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 resource

An 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 href member 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 resource

A 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

Request
GET /things
Accept: application/json
Response
200 OK
[
  {
    "name":"WoT Pi",
    "description": "A WoT-connected Raspberry Pi",
    "properties": {
      "temperature": {
        "label": "Temperature",
        "type": "number",
        "unit": "celsius",
        "description": "An ambient temperature sensor",
        "href": "/things/pi/properties/temperature"
      },
      "humidity": {
        "label": "Humidity",
        "type": "number",
        "unit": "percent",
        "href": "/things/pi/properties/humidity"
      },
      "led": {
        "label": "LED",
        "type": "boolean",
        "description": "A red LED",
        "href": "/things/pi/properties/led"
      }
    },
    "actions": {
      "reboot": {
        "label": "Reboot",
        "description": "Reboot the device"
      }
    },
    "events": {
      "reboot": {
        "description": "Going down for reboot"
      }
    },
    "href": "/things/pi"
  }
]

Alternative Protocol Bindings

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.

Web Thing WebSocket API

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.

Protocol Handshake

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.

Request
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
Response
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.

Example
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.

Example
{
  "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.

Example
{
  "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.

Example
{
  "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.

Example
{
  "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.

Example
{
  "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.

Example
{
  "messageType": "event",
  "data": {
    "motion": {
      "timestamp": "2017-01-24T13:02:45+00:00"
    }
  }
}

Further Information

Web of Things Integration Patterns
Advice on different design patterns for integrating connected devices with the Web of Things.