Hacking on the Spark API with Node.js

I just banged out a web app using Node.js to interface with the Spark API. I was yeah baby this is going to be awexome!

\node spark.js

"getaddrinfo ENOTFOUND"

:cry: I'm getting errors that the address is not valid.

So then going back to simple requests through my browser:

https://api.sprk.io/v1/devices/elroy%20-d%20message=winning

https://api.sprk.io/v1

both give me a similar response

not found

The URL you want to access is

https://api.sprk.io/v1/devices/elroy

You have to do this with a POST request, and the data you want to send is

message=winning

If this still doesn’t make sense, try pasting your spark.js into a gist:
https://gist.github.com/
and share the URL to the gist here. Then someone can help you debug.

Thanks for the fast reply! That totally makes sense to me, but...

I was starting with HTTP GET requests since the documentation states:

HTTP GET requests retrieve data from a device without changing its state.

I guess I should read better… seems like for GET I should use this test URL

https://api.sprk.io/v1/devices/elroy_box/events

However I’m getting the same response.

Yeah, that’s a general guidance rule about REST APIs. The only GET request documented in the API right now is the SSE route (e.g., https://api.sprk.io/v1/devices/elroy_box/events) which you can try and see that the connection stays open, but there’s no core sending any data to that stream. The two routes for controlling a device are POST requests.

Oh come on… you guys don’t have a Spark Core in a closet somewhere spitting out digits of Pi? :wink:

Will there be a more simple GET request that we can just query a snapshot of the pins, or even one specific pin?

Also so far I kind of have POST requests working with the ā€˜request’ library, but Node’s built in ā€˜https’ library is complaining a lot! :slight_smile: I’ll keep debugging… this is how I learn.

What kind of response should I get from sending { ā€˜message’: ā€˜winning’ } ?

Currently I’m getting

{
  "ok": false,
  "errors": [
    "No valid pin or custom message found"
  ]
}

My code:

var request = require('request');
request.post(
    'https://api.sprk.io/v1/devices/elroy',
    //{'pin':'D0', 'level':'HIGH'},
    {'message':'winning'},
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
        else
            console.log('ERROR: '+error+' '+response+' '+body);
    }
);

You can see how I tried to set the pin high as well… I get the same response with that one. Probably something I’m not understanding about the request library yet.

I’m trying this as well with the same response.

var request = require('request');
request.post(
    'https://api.sprk.io/v1/devices/elroy',
    { 'content-type': 'application/json',
        body: JSON.stringify({'message':'winning'})
    },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
        else
            console.log('ERROR: '+error+' '+response+' '+body);
    }
);

Please don’t tell me there is no other response currently (like anything that sets {ok:true} because I’ll have to cry… briefly.

Ok, I got it working and saw the {ā€œokā€: true} response… then I cried a bit… now I’m all smiles. I can set pins and see the error message when the pin numbering is out of scope. I’ll try to wrap this up into a nice example for anyone that might want to use Node.js :smiley:

That would be cool. Still waiting on mine, though.

I don't have one either... just passing the time with my addiction to Node.js :smile:

Check out this simple Node.js wrapper I whipped up today for the Spark API

Currently supports simple pin setting high/low and custom messages, with callbacks!

digitalWrite(D0,HIGH);

sendMessage('winning');

Right now the examples are at the bottom of the sparkapi.js file. I'll break them out later in a separate examples folder to keep the wrapper clean.

Hopefully I can add the simple GET request for digitalRead() when Spark documents it. SSE Streaming is cool, but I'm not sure how to handle it well yet.

1 Like

Cool, I starred it. Hey, we both forked Alain Gilberts code for turntable bots. :slight_smile:

Gonna check this out later, man. Thanks!

That’s where I recognize your name from! I was like… dang I know this guy. I just retired from a year of TT.FM and well over 10,000 lines of code for many different projects there.

Spark Core is going to be a wild ride for sure.

1 Like