‫Let's now create a more advanced Postman setup,
‫in order to make our lives a bit easier
‫when working with this tool.
‫So, back here in Postman,
‫the first concept that I want to show you
‫is the concept of environment.
‫So, maybe you've noticed that up here it says
‫no environment, right?
‫And, so that's because, well,
‫at this point we haven't created any environment.
‫All right.
‫And, so, basically, an environment is like a context
‫where our app is running.
‫And, we can then specify a couple of variables
‫for each of these environments.
‫And, the two ones that make the most sense is,
‫just as we have in our express application,
‫the development and the production environment.
‫So, let's start by adding our first, our environment here.
‫OK.
‫And, I'm gonna call this one depth and then for naters,
‫so the name of our application.
‫OK.
‫And, so the variable that I'm gonna create here is called
‫URL.
‫Because that's the most important thing
‫that I want to change from one environment to the other.
‫So, in development, we will have one URL,
‫and then in production, we will have another one.
‫Right.
‫And, so, let's now set the URL here to
‫http
‫127001,
‫which stands for localhost.
‫Then, port 3000
‫and, I think that's actually it.
‫So, that's the initial value of our URL variable.
‫OK.
‫So, let's now add
‫And, so just like this, we have our new depth environment.
‫We can then add one for production as well.
‫But, right here we don't really have a URL
‫just yet.
‫So, that simply said Naters dot io,
‫which does not exist.
‫OK.
‫But, this is simply, let's say, a placeholder.
‫All right?
‫OK.
‫So, let's now actually go ahead and use
‫this environment variable that we just created.
‫So, remember, it's called URL.
‫And, so now, what I want to do
‫is to go ahead and replace this URL here
‫so, this here, with the variable that we just created.
‫And, we do that by using curly braces,
‫the name of the variable,
‫and then curly braces again.
‫All right.
‫And, so this will basically now be replaced
‫with the variable coming from our environment.
‫Now, when we hover this, we can see that it is
‫unresolved, but that's because we didn't choose
‫our environment yet.
‫So, we need to come to this menu here
‫and then we select depth to Naters.
‫And, so if we hover it now, you will see
‫that it's actually the variable that we just defined before.
‫OK.
‫So, if we try to send a request now, then, of course,
‫we get the correct response just like we did before.
‫OK.
‫And, so I hope you can see how useful this will be
‫later when we're in production.
‫Because, if we didn't have these environment variables,
‫then later on, when we want to switch to testing
‫this API in production,
‫then we would have to go ahead and
‫manually change all of these URLs in all of the requests.
‫And, for now, we don't have that many.
‫But, imagine we have 30 or 50 or 100 requests,
‫and we would then manually have to change these URLs
‫in all of them.
‫And, that would not be fun at all.
‫And, so now we will do that once
‫and then it's done for the rest of
‫the project development.
‫Right?
‫So, if we change now to production, for example,
‫then, well, here we would have Naters dot io
‫and then API v1 and tours.
‫All right.
‫So, let's now go ahead and copy this
‫and actually paste it into all of these.
‫So, of course we have to do it once now.
‫But again, it's better to do it now that we don't
‫have that many endpoints here yet,
‫than to do it later.
‫So, save this one.
‫Save this one again.
‫Actually, let's get rid of this property.
‫So, save.
‫Save.
‫And, save this one as well.
‫So, I'll do it in all of them now.
‫OK, just very quickly.
‫And, these two already have it.
‫All right.
‫Then, get all tours.
‫Get tour does not.
‫All right, save.
‫And, actually, we can hit Command-S,
‫just like in any other app.
‫And, then Command-W to close the tab.
‫OK.
‫So, just like in Google Chrome,
‫for example.
‫So, Command-Save, Command-S,
‫and then Command-W.
‫So, just to make it a bit faster,
‫And, now we're almost done here.
‫And, that's it.
‫OK.
‫And, so just like this we have basically
‫future-proofed our API development here with Postman.
‫Next up, remember how in the last lecture,
‫I said that I was gonna automate basically
‫the process of copying this token here.
‫And, then pasting it right here into this bare header.
‫All right.
‫And, so let's now actually do that.
‫And, for that, we will actually have to
‫write a little bit of code.
‫But, that's no problem at all.
‫So, let's do that.
‫And, sign up and then login.
‫Cause these are the two places,
‫so the two endpoints
‫from which we will receive a json web token, right?
‫So, we come here to the test tab,
‫which we haven't used yet.
‫And, so here we will programmatically set
‫an environment variable.
‫OK.
‫So, we created environment variables before
‫using this dialogue here, OK?
‫But, now here with code, we will basically
‫programmatically create an environment variable as well.
‫And, we will create one for the json web token
‫that we receive in the request.
‫OK.
‫And, so basically all we need to do is to use
‫the snippets that we have here.
‫All right.
‫So, in this case, we want to set an environment variable
‫and, so let's just click here.
‫And, indeed, we get this pre-written code for us here.
‫OK.
‫This is basically just some JavaScript
‫that we can access here in Postman.
‫So, PM, which probably stands for Postman,
‫dot environment, which is the current environment.
‫And, then, set.
‫So, set the variable called JWT.
‫And, then, of course, the value of that.
‫And, where is that value?
‫Well, it is in PM dot response
‫dot json.
‫So, basically, to convert it to json.
‫And, then dot token.
‫And, it's dot token here, of course,
‫because that's the name of our property
‫in the response object.
‫OK.
‫So, basically, this code here gets the response for us
‫and, then, on there we can write the dot token property,
‫and then assign to the JWT environment variable.
‫OK.
‫And, so now we have an active test here,
‫which is why we get this blue dot
‫or a green one, maybe, (laughing)
‫and so let's now test it out.
‫So, let's sign up with a new user.
‫I'm calling it test user.
‫We need to get rid of this comma.
‫And, now let's send it.
‫And, we could not get any response.
‫And, that's because I have naters dot io here.
‫So, I'm in the wrong environment.
‫So, of course, we want to be in development.
‫So, send it again.
‫And, so here indeed, we get our token.
‫And, so let's now check if it is in our
‫environment as well.
‫So, for that, we come here.
‫Def, naters, and indeed, here we now get JWT
‫and then this token that we have also down here.
‫OK.
‫That token variable that we have here
‫should be the exact same as the one down here.
‫So, Postman got it from here,
‫and then posted it as this new environment variable.
‫OK.
‫So, that's the first step and the second step
‫is to now go to one of our protected routes,
‫which, in this case, is only this one.
‫And, then here we can actually delete
‫this at all.
‫So, right over on this side,
‫OK.
‫So, get rid of all of that.
‫And, then here we have,
‫as you maybe have seen,
‫an authorization tab.
‫OK.
‫So, here we can now specify the type
‫of authorization.
‫And, you see all of these differents here,
‫but we are going to choose the Bearer Token.
‫OK.
‫So, just like we have specified Bearer
‫in our token,
‫we also have to name Bearer here.
‫OK.
‫So, Bearer Token.
‫And, now all we need to do is to specify the token.
‫And, remember how we do that?
‫Well, actually the exact same way as we did up here.
‫So, curly braces, curly braces,
‫JWT, and then close it.
‫So, it turns orange
‫and that means that it should now work.
‫And, let's now test this.
‫And, bam, here we go.
‫So, we really got access to the data,
‫which means that the token was successfully sent.
‫All right.
‫And, we can actually see that here
‫in the headers.
‫So, it automatically pre-filled this header
‫now for us.
‫So, we have authorization where we have Bearer
‫and all of this good stuff here.
‫And, actually, it also now
‫put all of this stuff here for us.
‫OK.
‫So, actually, this is exactly what we saw before
‫when we previewed the request dot headers object
‫in express.
‫Remember that?
‫OK.
‫You see here that it's actually just
‫temporary generated.
‫So just so we get a preview.
‫All right.
‫And, so, that's actually it.
‫This makes our life so much easier
‫when we're working here with authentication.
‫Because, each time we want to add a new
‫protected route,
‫all we need to do is to come here
‫to the authorization tab and then add
‫this variable here
‫and specify that we're working
‫with a Bearer token.
‫All right.
‫And, actually, I just realized that
‫we're not just yet.
‫Because, we have this here in sign up,
‫so, this test.
‫But, of course, we also need it in login.
‫So, that's the other place
‫from which we can get a new, fresh token.
‫Right?
‫So, let's copy this,
‫give this one a save,
‫also save this one,
‫and, then here at login,
‫post it here.
‫Save it.
‫And, I don't even need to test it
‫because, well it should work just the same
‫as with sign up.
‫All right.
‫OK.
‫This was very important
‫for us in order to move on
‫and to make our lives a bit easier
‫when you're working with Postman
‫for your API development.