#Idiots Guide to Using littleBits cloudBit API ##Introduction 1. Connect a cloudBit to your network using the instructions at [http://control.littlebitscloud.cc/](http://control.littlebitscloud.cc/) 2. Find out your AccessToken - visit [http://control.littlebitscloud.cc/](http://control.littlebitscloud.cc/) - on the left hand side select any of your cloudBits - using the tab bar at the bottom select settings - scroll down and copy your AccessToken 2. Find a REST client to test with; I use either [Chrome app REST client ](https://chrome.google.com/webstore/detail/rest-console/cokgbflfommojglbmbpenpphppikmonn?hl=en) or [http://apigee.com/console](http://apigee.com/console) ##Setting Up Headers 1. Include an 'Authorization' header 'Bearer XXXX' where XXXX is your AccessToken **you need the word Bearer** 2. Include an 'Accept' header 'application/vnd.littlebits.v2+json' 3. Include a 'Content-Type' header 'application/json' ##Current URL 'http://api-http.littlebitscloud.cc/v2/' ##GET All Connected Devices 1. Change the request url to = 'http://api-http.littlebitscloud.cc/v2/devices/' 2. Ensure you have: - Authorization header - Accept Content Type header - Content Type header 3. Click GET ```text \\\request POST /v2/devices HTTP/1.1 Authorization: Bearer d64e5f77f9c60f00b4999ca2539ef61394e7b3beef00ce0856cb1aa9976871fa Host: api-http.littlebitscloud.cc Content-Length: 46 X-Target-URI: http://api-http.littlebitscloud.cc Accept: application/vnd.littlebits.v2+json Content-Type: application/json Connection: Keep-Alive ``` ```json \\RESPONSE [ { "id": "00e04c2241e8", "user_id": "20702", "label": "Oshawott", "is_connected": true, "ap": { "ssid": "BTHub4-JWPJ", "mac": "CC:33:BB:21:63:30", "strength": "81" }, "subscribers": [], "subscriptions": [] } ] ``` ##POST Data to Connected Device 1. Connect a green output bit preferably something you can see numbers on, either a number bit or bargraph. 2. Using 'GET all connected devices' request establish which cloudBit you need to talk to - save it's device_id somewhere 3. Change the request url to 'http://api-http.littlebitscloud.cc/v2/devices/XXXX/output' where XXXX is the device_id 4. In the request payload supply the following json: ```json { "percent":50, "duration_ms":5000 } ``` - percent needs to be a whole number between 0 and 100. - duration_ms is the number of milliseconds to run, -1 runs forever (or until something interrupts it) 5. Ensure you have: - Authorization header - Accept Content Type header - Content Type header 6. Click POST ```text ///REQUEST POST /v2/devices/00e04c2241e8/output HTTP/1.1 Authorization: Bearer d64e5f77f9c60f00b4999ca2539ef61394e7b3beef00ce0856cb1aa9976871fa Host: api-http.littlebitscloud.cc Content-Length: 46 X-Target-URI: http://api-http.littlebitscloud.cc Accept: application/vnd.littlebits.v2+json Content-Type: application/json Connection: Keep-Alive { "percent": 50, "duration_ms": 5000 } ``` ```text ///RESPONSE OK ``` ##Connect 2 CloudBits Together 1. Connect a purple input bit before one cloud bit (eg. squirtle) 2. Connect a green output bit to after another cloud bit (eg. bulbasaur) 3. Using 'GET all connected devices' request establish the device_ids of the 2 cloud bits you want to link together. 4. Open the REST API client and set the request url to 'http://api-http.littlebitscloud.cc/v2/subscriptions', and the method to POST. 5. Ensure you have: - Authorization header - Accept Content Type header - Content Type header 6. In the request payload supply the following json. The publisher_id is the device ID of the sender with the input (squirtle). The subscriber_id is the device ID of the reciver with the output (bulbasaur). ```json { "publisher_id":"00e04c035a67", "subscriber_id":"00e04c037b69" } ``` 7) Click 'Send'. 8) Press on the purple input of squirtle and you should see the output happen on bulbasaur. ```text \\\request POST /v2/subscriptions HTTP/1.1 Host: api-http.littlebitscloud.cc Connection: keep-alive Content-Length: 71 Accept: application/vnd.littlebits.v2+json Authorization: Bearer d64e5f77f9c60f00b4999ca2539ef61394e7b3beef00ce0856cb1aa9976871fa Content-Type: application/json Accept-Language: en-US,en;q=0.8 ``` ```json \\RESPONSE { "publisher_id":"00e04c035a67", "subscriber_id":"00e04c037b69", "publisher_events":[ { "name":"amplitude:delta:ignite" } ] } ```