github.com/simpleiot/simpleiot@v0.18.3/docs/ref/api.md (about)

     1  # API
     2  
     3  **Contents**
     4  
     5  <!-- toc -->
     6  
     7  The Simple IoT server currently provides both Http and NATS.io APIs. We've tried
     8  to keep the two APIs a similar as possible so it is easy to switch from one to
     9  the other. The Http API currently accepts JSON, and the NATS API uses protobuf.
    10  
    11  **NOTE, the Simple IoT API is not final and will continue to be refined in the
    12  coming months.**
    13  
    14  ## NATS
    15  
    16  [NATS.io](https://nats.io/) allows more complex and efficient interactions
    17  between various system components (device, cloud, and web UI). These three parts
    18  of the system make IoT systems inherently distributed. NATS focuses on
    19  simplicity and is written in Go which ensures the Go client is a 1st class
    20  citizen and also allows for interesting possibilities such as embedding in the
    21  NATS server in various parts of the system. This allows us to keep our
    22  one-binary deployment model.
    23  
    24  The `siot` binary embeds the NATS server, so there is no need to deploy and run
    25  a separate NATS server.
    26  
    27  For the NATS transport, protobuf encoding is used for all transfers and are
    28  defined [here](https://github.com/simpleiot/simpleiot/tree/master/internal/pb).
    29  
    30  - Nodes
    31    - `nodes.<parentId>.<nodeId>`
    32      - Request/response -- returns an array of `data.EdgeNode` structs.
    33      - `parent="all"`, then all instances of the node are returned.
    34      - `parent is set and id="all"`, then all child nodes of the parent are
    35        returned.
    36      - `parent="root" and id="all"` to fetch the root node(s).
    37      - The following combinations are invalid:
    38        - `parent="all" && id="all"`
    39      - parameters can be specified as points in payload
    40        - `tombstone` with value field set to 1 will include deleted points
    41        - `nodeType` with text field set to node type will limit returned nodes to
    42          this type
    43    - `p.<nodeId>`
    44      - used to listen for or publish node point changes.
    45    - `p.<nodeId>.<parentId>`
    46      - used to publish/subscribe node edge points. The `tombstone` point type is
    47        used to track if a node has been deleted or not.
    48    - `phr.<nodeId>` (not currently used)
    49      - high rate point data
    50    - `phrup.<upstreamId>.<nodeId>`
    51      - high rate point data re-broadcasted upstream. `upstreamId` is the parent
    52        of the node that is interested in HR data (currently the db node).
    53        `nodeId` is the node that is providing the HR data. In the case of a
    54        custom HR Dest Node (serial client), the serial client may not be a child
    55        of the upstream node.
    56    - `up.<upstreamId>.<nodeId>`
    57      - node points are rebroadcast at every upstream ID so that we can listen for
    58        point changes at any level. The sending node is also included in this. The
    59        store is responsible for posting to `up` subjects. Individual clients
    60        should not do this.
    61    - `up.<upstreamId>.<nodeId>.<parentId>`
    62      - edge points rebroadcast at every upstream node ID.
    63    - `history.<nodeId>`
    64      - Request/response -- payload is a JSON-encoded `HistoryQuery` struct.
    65        Returns a JSON-encoded `data.HistoryResult`.
    66  - Legacy APIs that are being deprecated
    67    - `node.<id>.not`
    68      - used when a node sends a [notification](notifications.md) (typically a
    69        rule, or a message sent directly from a node)
    70    - `node.<id>.msg`
    71      - used when a node sends a message (SMS, email, phone call, etc). This is
    72        typically initiated by a [notification](notifications.md).
    73    - `node.<id>.file` (not currently implemented)
    74      - is used to transfer files to a node in chunks, which is optimized for
    75        unreliable networks like cellular and is handy for transfering software
    76        update files.
    77  - Auth
    78    - `auth.user`
    79      - used to authenticate a user. Send a request with email/password points,
    80        and the system will respond with the User nodes if valid. There may be
    81        multiple user nodes if the user is instantiated in multiple places in the
    82        node graph. A JWT node will also be returned with a token point. This JWT
    83        should be used to authenticate future requests. The frontend can then
    84        fetch the parent node for each user node.
    85    - `auth.getNatsURI`
    86      - this returns the NATS URI and Auth Token as points. This is used in cases
    87        where the client needs to set up a new connection to specify the no-echo
    88        option, or other features.
    89  - Admin
    90    - `admin.error` (not implemented yet)
    91      - any errors that occur are sent to this subject
    92    - `admin.storeVerify`
    93      - used to initiate a database verification process. This currently verifies
    94        hash values are correct and responds with an error string.
    95    - `admin.storeMaint`
    96      - corrects errors in the store (current incorrect hash values)
    97  
    98  ## HTTP
    99  
   100  For details on data payloads, it is simplest to just refer to the Go types which
   101  have JSON tags.
   102  
   103  Most APIs that do not return specific data (update/delete) return a
   104  [StandardResponse](https://github.com/simpleiot/simpleiot/blob/master/data/api.go)
   105  
   106  - Nodes
   107    - [data structure](https://github.com/simpleiot/simpleiot/blob/master/data/node.go)
   108    - `/v1/nodes`
   109      - GET: return a list of all nodes
   110      - POST: insert a new node
   111    - `/v1/nodes/:id`
   112      - GET: return info about a specific node. Body can optionally include the id
   113        of parent node to include edge point information.
   114      - DELETE: delete a node
   115    - `/v1/nodes/:id/parents`
   116      - POST: move node to new parent
   117      - PUT: mirror/duplicate node
   118      - body is JSON api/nodes.go:NodeMove or NodeCopy structs
   119    - `/v1/nodes/:id/points`
   120      - POST: post points for a node
   121    - `/v1/nodes/:id/cmd`
   122      - GET: gets a command for a node and clears it from the queue. Also clears
   123        the CmdPending flag in the Device state.
   124      - POST: posts a cmd for the node and sets the node CmdPending flag.
   125    - `/v1/nodes/:id/not`
   126      - POST: send a
   127        [notification](https://github.com/simpleiot/simpleiot/blob/master/data/notification.go)
   128        to all node users and upstream users
   129  - Auth
   130    - `/v1/auth`
   131      - POST: accepts `email` and `password` as form values, and returns a JWT
   132        Auth
   133        [token](https://github.com/simpleiot/simpleiot/blob/master/data/auth.go)
   134  
   135  ### HTTP Examples
   136  
   137  You can post a point using the HTTP API without authorization using curl:
   138  
   139  `curl -i -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '[{"type":"value", "value":100}]' http://localhost:8118/v1/nodes/be183c80-6bac-41bc-845b-45fa0b1c7766/points`
   140  
   141  If you want HTTP authorization, set the `SIOT_AUTH_TOKEN` environment variable
   142  before starting Simple IoT and then pass the token in the authorization header:
   143  
   144  `curl -i -H "Authorization: f3084462-3fd3-4587-a82b-f73b859c03f9" -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '[{"type":"value", "value":100}]' http://localhost:8118/v1/nodes/be183c80-6bac-41bc-845b-45fa0b1c7766/points`