Check out a free preview of the full Nodebots / Hardware course:
The "Using Third-Party APIs with Hardware" Lesson is part of the full, Nodebots / Hardware course featured in this preview video. Here's what you'd learn in this lesson:

Steve writes functionality to send him an email whenever a door has been opened with the SendGrid API.

Get Unlimited Access Now

Transcript from the "Using Third-Party APIs with Hardware" Lesson

>> Steve Kinney: So what could we do with this? Now it would be awesome if like this were something where we are keeping our diary. Cuz I want to know when someone is trying to read my diary, right? We could have it send us some kind of notification. What kind of notification is up to you.

[00:00:15] You have it send you a text, some kind of push notification, something along those lines. What we're going to do, what I'm gonna use really quickly is a SendGrid API, not because I work at SendGrid. I do work at SendGrid, but in this room right now we have, they cell signal turn off the text messages like probably I don't like, not,

>> Steve Kinney: If someone did open my secret drawer or door that I didn't want opened, I don't know if an email is definitely the first thing that's gonna get to me. I would probably wanna text, but that is one way to do it. So if we take a quick stop break for a second.

[00:00:52] If you need to make a SendGrid account if you wanna get an API key, or you can just watch and follow along, it is up to you. But I wanna give you an opportunity to do that if it's something you wanna follow along and implement if you don't have one already.

[00:01:03] So I've just logged in real quick. If I wanna make a new API key, I scroll down to Settings. That's closed, you can open it like so.
>> Steve Kinney: We go ahead and create an API key. You can't see previous ones for security reasons so we'll go ahead and create one.

[00:01:22] Give it a name, tussle run take two,
>> Steve Kinney: And I'll grab that and very quickly close the window. [LAUGH] Sweet, so I have an API key which I'm going to paste into another text file. So my big plan on obscuring that. It's not gonna work out too well, but I will go ahead and there is an environment.sample.json in place.

[00:01:59] You can make a copy of that and put your API keys in there. Env.json is in gitignore so you don't have to worry about accidentally publishing your keys. So I'll go make a new file code inv.json, cool. Just paste a little quick right there, but if I look in inv.sample this is basic like structure that some of my code is going to expect.

>> Steve Kinney: Neat, so go ahead.
>> Steve Kinney: We'll just paste that one in there, then we'll never see it again. All right, so then I can basically,
>> Steve Kinney: Just, he says, I didn't capitalized the g, cool.
>> Steve Kinney: ./env, so we'll have that, we're pulling that key from there, sweet. I'm actually just gonna pull in the entire thing because I'm probably gonna use the variable SendGrid again.

[00:03:14] So we'll say env.
>> Steve Kinney: All right, neat. And the other thing I'm gonna need is,
>> Steve Kinney: Equals.
>> Steve Kinney: Pull in our mail API which is in our package json right here. All right, so now I have this API. I do need just a little bit of configuration. So let's make a const openMessage

>> Steve Kinney: And we'll say, who will we send it to? I'm gonna make a quick Mailinator account, which is just the way to send a disposable.
>> Steve Kinney: Test will run. Cool.
>> Steve Kinney: So we'll grab that email address.
>> Steve Kinney: Is hard, sometimes it's really hard to select some text on the screen.

[00:04:33] Cool, and we'll say that it's from,
>> Steve Kinney: Should be like no reply. Let's see if we have done that. That would be fun. But it will give it a subject.
>> Steve Kinney: The Door Has Been Opened. The text is going to be everything we feared is happening now. So fun fact about email, is If you ever wanna talk about email for a few hours I'm always happy to to do that.

[00:05:15] I have to support Outlook 2007. So I've a lot, a lot of thoughts. I might need to have a few drinks before I'm ready to share them all. Did you know that Outlook doesn't use Internet Explorer's rendering engine? It's uses Microsoft Words. [LAUGH] So if you thought developing for I86 was bad, hold my beer.

[00:05:41] Cool, so we got this open message. And so when the door's opened, I'm actually gonna be polite bring back in that throttle function. Let's not blow up my inbox, cuz he saw me as I was reaching over to touch the door sensor before I was triggering a lot of open and close.

[00:06:04] Let's not have that affect my inbox.
>> Steve Kinney: Pull in that throttle method again. Cool.
>> Steve Kinney: handleOpen, we'll give it a function, to throttle.
>> Steve Kinney: I'll say like two seconds, that should be sufficient. And what I'll do is I'll say sendgrid,
>> Steve Kinney: .send(openmessage).
>> Steve Kinney: And then that'll return our promise when it succeeds.

>> Steve Kinney: We'll console.log
>> Steve Kinney: Sent the warning message, preparing to panic. I don't know what's in the drawer and if it fails for some reason I do wanna know about that. Cuz I don't want any one of those live coding situations where I repeatedly check the email and I don't see it because like there was some error.

[00:07:16] Something went terribly, terribly wrong. Cool, so we've got that in the handleOpen, and we'll go ahead and do that.
>> Steve Kinney: And this time, I'll define my function before I attempt to use it. The other thing I'm gonna do is just give myself a console.log.
>> Steve Kinney: All right, cool, so now when this opens we're gonna go send me an email, and I'll check it whenever I check email.

>> Steve Kinney: We'll go ahead and push that out. All right, I'm going to very gently separate these.
>> Steve Kinney: I didn't console log. Let's see. Let's go take a look cuz I'm definitely consol log closing.
>> Steve Kinney: Wonder if my throttle's a little too ambitious.
>> Steve Kinney: You know what, let's just not be nice to my inbox.

[00:08:37] Let's just do it.
>> Steve Kinney: Send that back over. I have apparently gotten no better at adjusting that thing from afar. It's generally speaking a little bit. As I was playing with it, sitting like a normal person, it's pretty tolerant of actually opening and closing, but when you're reaching over it's extra fun.

>> Speaker 2: Are you actually executing handleOpen or are you kind of like incurring it by accident?
>> Steve Kinney: Yep, good catch. When I placed that console log I just simply announced that it existed and didn't actually call it. So this should do the trick now, maybe we put that throttle back though.

[00:09:28] [LAUGH]
>> Steve Kinney: I think it's really great that in a workshop where there's gonna be a whole bunch of live wiring and hardware that most of our biggest mistakes are the world's simplest JavaScript errors. [LAUGH] It's just really gratifying. Principal Engineer, everyone. [LAUGH]
>> Steve Kinney: Cool, so let's set this back up.

>> Steve Kinney: The moment there where I stopped spinning filled my heart with dread, but now it's spinning again so, crisis averted.
>> Steve Kinney: I'm really excited.
>> Steve Kinney: All right. Ready, I'm ready.
>> Steve Kinney: All right,
>> Steve Kinney: No, something went terribly wrong.
>> Steve Kinney: I know what the issue is.
>> Steve Kinney: That's giving it one second then I will, a few important in API key, but you don't actually use it.

[00:11:03] [LAUGH] APIs don't let you send stuff, so we just get that going real quick.
>> Steve Kinney: So what I was missing, as I just mentioned, is you can require a library, you can get an API key, but unless you tell the library about the API key that doesn't work.

[00:11:40] Just having an API key, the same great API, very cool but it doesn't psychically know who you are and what your API key is. So we'll pull everything in, and we'll just do this sengrid.setApiKey and from the MPIK we have in there. All right, one more time with feeling, let me put these back together.

>> Steve Kinney: All right, moment we've been waiting for.
>> Steve Kinney: All right. Email was sent and then we close it back up again. The door has been opened, right? So you can use whatever API you want. I used the API, I don't actually work on the API, so it wasn't like a self plug but, you can use Twilio.

[00:12:36] You can use any API you want, you can do push notifications, anything. The basic point here is again, now we have hardware components, our tester board available to talk to anything. We used the door sensor I mentioned earlier. You can use it with the hue bulbs, anything you want, right?

[00:12:53] There's no actual limits as you start to mix and match APIs and different web technologies and these different components. There's a lot of really, really, really interesting things that you can do, and if you think about it, we've done a bunch of them over the course of the last few hours, right?

[00:13:09] Yeah, you can do some really complicated things, but getting started with this stuff is pretty easy.