Hey there stranger!

Sign up to get access.

Build a License Key Generator and API in 20 Minutes

About this Tutorial

How to leverage Apps Script to create an API in less than 20 minutes. Create a license key for use in your own apps or as a cloud based API service. We'll create the API using Apps Script and the front end system using a basic Carrd page. Also I'll show you how to test your API in Postman, a free api checker web app.

Video Transcript

00:00 So in this video, we're going to create a license key generator. We're going to do this from scratch. I'm going to show you this is the end result, uh.
00:07 And what we can do with this is we created an API which is a URL that can take some parameters like an email and return a license key.
00:16 We're generating a license key here with a unique, a universal unique ID. We're creating that ID. We're then adding it based on the email into our list.
00:26 And then we're doing another API or essentially the same URL, but using doget. get to create a get request for the API to say hey, check this ah ID, does it exist or does it not exist?
00:41 And so one, we can create the generator that generates the keys, and we can also create the checker. Now, an API can if you don't know, or if you're unfamiliar with it, it's just a URL or a server really that exists that we can enter some information and then get some information back.
00:57 So, something like the OpenMovie database is something that I enjoy doing where you can create a API call where you say hey, let's create what's the information about this title, and you enter the title and then you get back poster information, you get back actors, you get back a summary of that movie
01:12 , and that's an API, right? An API is sort of let the internet exist on a data level. It doesn't look very good because it's just a URL with some parameters, but we can absolutely create something like a front end for this, and I'm going to do that in card, so I'm going to show you that as well, where
01:32 in card, C-A-R-R-D, I've created just two form here, forms here, one create a license key, you enter your email, you get a key, and then you can check that key, now these two forms could be on separate pages of a website, or separate subdomains, check or create or, and also to create a license key, you
01:50 don't necessarily need a front end. It could be very much like, hey, every Stripe buyer for a certain product, you create a license key for them, and then email it to them.
01:59 We can do that. But in this case, we're just gonna generate the license key, we're gonna create this API, and I'm gonna show you how simple it is in Google Script and in Google Apps Script.
02:07 Um, but. This, uh, you can totally check this out at licensekeygenerator.card.co. I'm gonna test it in this video. Look at this, we're gonna do that right now.
02:16 So if we do Andrew at BetterSheets.co, which is enter a uh email address, we're going to hit enter or create key.
02:24 And now we have a key. Now we can save this key any way we want. And now I'm gonna go to licensekeygenerator.card.co for some reason.
02:34 It doesn't work unless I'm in cognitomode. So I'm gonna check this license key. Uh and, And see do I get a yep it exists.
02:42 So now if it didn't exist if I didn't get that correct let's say I only got the first part let's check that.
02:48 And see we get nope. So the license key checker works and the license key generator works. But we're gonna create this from scratch.
02:56 Thank watching. I'm gonna show you how to do that. And this you are uh this API we're gonna create for this back end of this and uh and this front end.
03:03 We can also test via something like Postman. Postman is an API tester uh that you can just enter this this URL that we're gonna get and we're gonna create.
03:11 Add the parameters of uh check key and we can send this and we can get a yep or a nope here.
03:17 Um this is not standard uh uh API uh yep and nope you sort of want to have 400 codes or 200 codes.
03:26 200 codes means yes. Thanks for watching. 400 code means there's like an error. Um but in this case we're just doing this for fun.
03:33 Um just creating this in app script. So the first thing we're going to do is go to sheet.new. We're going to create a brand new sheet and I'm going to do this absolutely from scratch and so we're going to call this uh key generator.
03:45 And uh if you're watching this video on bettercheats.co down below you can grab this uh done and everything coded for you if you wish.
03:55 So first thing we're going to do is create a sheet called keys. And this is going to be, uh a keys in a column and emails in the B column extensions app script.
04:06 Let's just go into our app script and start typing because it is so simple to do this. We need two simple umm what are called simple triggers.
04:15 Do get and do post. We don't need to do anything substantial of creating a server. We're going to do that.
04:19 We're going to umm create the server on Google systems with the deploy button up there. But the first thing we need is a do post.
04:27 We want to create the uhh we want to create the key when we get. A e-mail. So we need some e here.
04:37 This is a variable this e could be any text you want. Umm but we want to get the e-mail from this variable e-mail equals e dot parameter dot e-mail.
04:47 So the e-mail, e-mail here is what parameter we create for this uh, API. We can say e-mail, we can say e-mail, address whatever we want.
04:57 We can say e-mail address. And we can add that in our URL. But for our case we're just going to use e-mail.
05:02 And I'll show you how to do that in, the API uh for the API later. Okay, now we need to actually create the unique universal ID of the ID.
05:13 And actually we do want to use const here uh, and so we do that. const id actually key equals utilities dot get uuid.
05:30 And just get uuid means it's going to create the universal unique universal ID. Okay, next step is we want to get the sheet name uh keys over here.
05:43 And so what this is is just spreadsheet app dot get active spreadsheet dot get sheet by name keys. This is all of the keys that are umm.
05:52 Here actually it's just the sheet named keys that we've named here. Again you can rename this keys whatever you want to name it.
05:59 Okay now we need to set the uhh new key and the email but the key we want to actually we want to name that new key here.
06:10 But first we want to do keys dot insert row and we want to just insert a row so that we never have to make sure that there's enough rows for us to insert the data in.
06:27 Uh we just want to insert a row add to it every time we get a new thing. We can always come back to here delete some extra columns if we want and delete all of these rows.
06:40 We only have like a few extra here and it's always going to insert a row so we always have room to insert it.
06:45 Uh we want to do keys.getrange which will be just the first row first column one row one column so one one one one.
06:56 dot set value and the first thing we want to do is add the new key then the second thing we want to do is the same keys.getrange and instead of row one column one we want to do row one column two but only Thanks watching! one cell size one row one column set value and the value is going to be the email
07:19 that we get up here we're just going to set those once we create that idea we're going to want to match them up so in our system we know which key is with which email address and and the last thing we're going to do here is return something uh so our API needs to say hey something came back uh we're 
07:37 going to do content service can save there create text output and our text output is going to be the new Thank you. key and I believe I did it to string uh we can add to string here to make sure that it is a string and it doesn't represent it as a number or something um anytime we're sending back output
07:57 we want to make sure just that it is a string and so this do post post. ok we're going to command s now uh and save this is all we need to do before we create a web app now we can create the web app for this post but we do want to do one more thing is I do get and this is just the other side of it uh
08:16 I where we say hey if we have a key give me back uh that it is a good key or not a good key is it is this a good key we're just going to check and we're going to use this function do get the reason we're just typing into post and do get and we're not naming these normally when we're creating functions
08:32 in Google Sheets we're going to create a new function and name it very something specific like checker but in this case we're using the installed triggers this do post and this do get to do all of the stuff for the API that we want as a get request and all the stuff was we want post request and you'll
08:51 see that later on in this video ok so what we're doing here is we're going to check the key uh so we need the parameter so we go const advanced.
09:04 ah check key equals e dot parameter and again anything we want to put in here and we're just going to write uh key.
09:19 Actually we can name check key. Okay and that is going to be the text that we put into the URL as a parameter that says check key equals and then we're going to put the put our license key there and we'll you'll see that in a second after we get this done.
09:35 Wait a minute. Next we need to get all of the keys again so we're going to take exactly the same const up here because we need to check them all.
09:45 And then we want to make sure that we're getting the last row const last row equals keys dot. Get last row.
09:57 That makes sure we are only grabbing all of the uh, license keys that are in existence in our sheet. Uh, then we want to check the keys so we're going to get all of the values.
10:10 We're going to call this check keys. We're going to go keys dot get range and our range is going to be row one column.
10:23 column. row one. Uh actually no row. Yeah one. Number of rows is going to be last row so we just want to know however many rows there are.
10:34 And then how many columns? We're going to get two columns out of this. Actually we only need one. I think.
10:39 well. What we do is. Yeah we just need to check it. We just need to say hey does this exist or not we don't need to know the e-mail address there.
10:49 And then we're going to do get values. Now we are going to get our output. So we want to make sure that our output is a variable output equals nope.
11:04 We're going to say if nothing else changes from now on we're going to output a nope. But if we do find this key in our list uhh we want to change it to a yep.
11:15 So for i equals 0 we're going to create a for loop here, i is less than check keys dot length and then i plus plus.
11:28 And this creates a for loop so it's going to go through the eyes, let's just double check we have if here.
11:36 Now we're saying if the check keys i meaning we're going to check each iterator from 0 up we're going to check this and if it's equal to this check key.
11:44 We're that we're getting we're going to change our variable to yep. So our variable right now for output is nope but once we find one we're going to change it to yep.
11:54 And then we need to output the result and so we're going to do another go back down. Here return content service dot create text output.
12:06 Our text output is going to be output dot to string. So just again make sure that we get the string output there.
12:13 We have everything. We save it. We're going to save name this uh key generator. And now in order to get this actually into an uh API URL we're going to go up here to deploy new deployment.
12:30 employment. We want to select type web app and we're going to execute it as ourselves and not only us but anyone can access this.
12:41 I'm going to hit deploy. We're going to have to authorize this access first time we do this but once we authorize it we won't have to uh do this if we need to update this deployment uh later on.
12:52 Okay we copy this URL. Now this URL allows us to generate a license key. So what we need to do is have this URL we add a question mark at the end then we type an email equals and we can do Andrew at bettersheets.co.
13:08 Now this is plain we're just using a URL uh plain API we're using a get request it's gonna do a post request actually but we're gonna hit that enter button.
13:20 and we don't get anything because it doesn't have a post request so we go to go to postman let's do this uh URL with this exact one we copied we're gonna change this to a post request to help us generate this.
13:36 we are going to need to add a key parameter and our parameter is going to be email value is going to be Andrew at bettersheets.co and now we have a post request we're gonna send that and see what happens what request sending a request.
13:52 ooh what's gonna happen and there we go we get returned a license key so now we can add here instead of an email we're gonna do a get request we're gonna get umm check key . he was what we said our parameter was we're gonna check the key here send and let's see if we get nope ah I think I see the error
14:19 it's we have a capital K here so let's change this check key to a capital K and send that again and do a get request and see if we get a yep yep we get a yep so we have checked our thing we have to make sure we have the correct spelling of our parameter there okay so this works all together we are getting
14:39 a data int and he error from that URL we are doing get request post request in Google Sheets now let's create a front end for it so I'm gonna go to my card dashboard and create a brand new I'm starting at zero with card just creating a brand new bear.
14:57 bear bones thing and so we're gonna go get a create a a key here we're going to add a form arm.
15:14 where's the form there it is a form we want the type to be custom we don't want to send email we want to run code load.
15:28 No sorry we want to send to URL and the URL we're going to send was our deployment here so we can go to manage deployments check here for our URL go back to our new site.
15:44 And we're just going to put that exec all the way that that whole script method we're going to do a post umm and then over in form field we want the label to be email we want text actually we can say email here this allows us this is card allowing us to put an email and we're going to call this email
16:06 this idea here is what we need uh to say in this parameter here so if you want those to match uh just name them the same so they can be email it can be ID it can be anything you want okay done and now we have created a key let's name this key creator we're going to publish this to a See ya.
16:32 new card let's check if it is available it is publish and now we have a front end to our uh key creator let's type in an email address let's just do enjoy better sheets.co again hit submit and it should take us and create this license key it is also in our sheet so it is right here in the first spot 
16:57 is the same one as we got so now let's create the checker part of this so go back say okay we're gonna just take this page actually just take this and copy it duplicate it and then duplicate this text and instead of submit actually I Actually, I won't.
17:20 something else here button let me say create and this one here check your key here and instead of an email we're gonna get a check Check.
17:37 Key enter key. This is just the text here, but our ID is check key with a capital K. Our type is gonna be just text and instead of submit on a- call this check.
17:54 Click done. Actually one more thing, we need to change this send URL to a get request. Okay, so that's uh what we need to do because in our code we have a do get here.
18:10 This is do post at the top to create it to say hey I want to get this information uh and put it into a sheet.
18:16 Do get is going to just get the information return our string. Let's go back click done. Go to our work.
18:24 Our page page published changes. Now in this case I was testing this before and it was not working unless I put it into a incognito tab.
18:36 Uh and if we have a key that we want to check actually Thanks for First let's just check that it is gonna give us nope first.
18:47 Click check. And here it is nope so hit back and now we have our key and we're- Or are we gonna get out of this once we hit check.
18:58 It takes a little bit of time. Yup. So we are checking our keys. We get a yep, a nope. Uh, based on this text and we have a front end system to check this.
19:06 Really easily with card. Set up this API super easily unless we've done this in less than 20 minutes. We've created our own API.
19:14 We've created our own uh, license key generator and checker. And front end is all done. I'm so excited about this.
19:22 And again, if you're watching this on better sheets down below, copy this sheet and get in your Google Drive right away and start using it and testing it out and for yourself, uh, if you're watching this somewhere else, go become a member of better sheets.
19:34 Hang out with us. Find out all this cool stuff to do in Google Sheets.