logo

NJP

ServiceNow Scripted REST APIs: Part 12 – Versioning

Import · Mar 16, 2023 · video

foreign and welcome back to Part 12 in this series of creating scripted rest apis in servicenow in previous videos in this series we've talked about what a scripted rest API is and we've gone ahead and created our very first scripted rest API with a total of five resources for retrieving records for creating new records as well as deleting records so if you've come this far congratulations on creating your first version of your API however just remember it is just that a first version no doubt you'll want to make enhancements to that API in the future but at the same time you want to allow current applications and clients that are using what you've already developed to continue doing that you don't want to break those current connections and this is where versioning comes into play so in this video we're going to go ahead and create a new version and make a very small but yet very significant change to our API let's have a look at our API as it currently exists in particular the get vehicles resource that resource defines five query parameters but all of those query parameters default to the equals operator in other words if we put in a model a year a city the way that we've developed the script in our API for this resource means that we're looking for a model that is equal to whatever we put in here that is equal to the year and so forth so there's no possibility here to say if the year of the vehicle is more than or later than 2005 or if the model is this model or this other model or if a model contains a certain string so if I put in XC90 here it means it's equal to XC90 I can't actually put in something like this to say is it this model or this other model so let's make that small enhancement to our script so before we go ahead and do that we'll actually need to enable versioning for our API so if I come over to the enable versioning link here and click on it that will enable versioning now before I do that pay attention to the resource path notice that there is no version identifier in any of those paths okay but once I click on enable versioning we're going to make version one the default so we're actually going to create a version one here if I click OK here the system will save those new resources now if we scroll down now and have a look at those resources once more we will see that if there is a version identifier see the version one right there okay that means that that is the default version the only version at the moment and when we do our testing later we actually won't need to put in the V1 as the identifier because we'll actually Define that this is the default in other words if you don't put in a version identify the system or default to whatever version you've defined as a default so now that we've done that we'll go ahead and click on add new version okay we're going to not make this the default version so in other words the original version will still be the default and we're going to copy existing resources from version one okay so we don't need to start all over again we can just copy what we have right now into the new version two and then make our changes so we'll click ok once we do that and scroll down once more now we can see we've got double the amount of resources a resource for each of the ones that we defined already for version one but the same one's now for version two so if we come down to our version 2 for get vehicles and open that we're going to make a small change to this script here so if we scroll down to where we make the Glide record query here I'm going to place one line of code here with these five lines whereby the first four lines are simply comments but if you have a look at line 36 here adding the query here to be exactly the same as the model that we've specified in the query parameter so we're not actually doing anything here we're not having defining an operator here so with the other ones we actually haven't defined and operator but if you do that then the system will just default to equals okay so if the make is equal to whatever the year is equal to that but for the model we've made it a little bit different okay so we're actually going to accept an encoded query here and we'll take a look at an example in just a moment okay but first of all I'm going to save that record and if we come back to our vehicles table here and just put in a filter such as where the model contains the string XC so that could be the XC60 model the XC90 the xc40 whatever you like now some of you may know this already but if you actually come to the filter in servicenow and right click on it you can actually copy that encoded query so that's what the system will accept in that query parameter okay so if I just paste it just to show you what it looks like you can see where the model is like XC okay or contains XC all right so if we come now to our rest API Explorer and just refresh the page here and then come along and select our API once more and this time we'll need to accept the API version as version two so we take advantage of the new script or the new version of a script that we've just defined we'll go to get vehicles and then I'll just pop that encoded query straight into the model field here okay so we're not looking for a model that is equal to model like XC this will be accepted and translated as as is as an encoded query so if we go ahead and send that we get a 200 response back and then we get those three records that we just saw in the list so now we're allowing the query to be a little bit more flexible we're not saying okay we only accept equal operators here you can Define your own encoded query here and we'll accept that okay let's do one more quick test let's come back to our list here and this time I'm going to modify the query here to say where the model is one of XC90 or XC70 okay run that we get the same records back I'll copy that query to the clipboard and just show you what it looks like and this time we'll do our test in Postman so before we go ahead and do the test on the left hand side I've actually created a new folder called V2 and I've just copied all the requests over to this new folder okay so this is something that you can do in Postman it allows you to really take control and organize your testing efforts here our V1 requests in the V1 folder and our V2 ones are in V2 now before we go ahead and test the new version let's see if the old version still works so let's go to get vehicles here I'm not going to make any changes to this whatsoever this is the same test that we ran in an earlier video and just click on send and we get the response back that works okay notice also in the request URI that the version number is not defined there and because we've said that version one is the default if you don't specify a version number then that's what the system will use all right let's go ahead now and go to V2 now I'm going to change this parameter here to model and then put in that encoded query that we saw before and then click on send now before I do will this work what do you think let's click on it and find out send hmm no records found failure why is that well just take a look at the URI here okay where model is equal to model in XC90 XC70 so actually looking for a model that is that model number and that doesn't really look right if you look at the URI as well you also notice that there's no version number there so what I haven't done yet is updated that URI to include the V2 the version 2 okay we need to do that to define or to tell the system we want to use the V2 version of this API because if we don't do that we're just going to use the default which is version one so what we'll do here is just come back to our version to get vehicles and I'll just copy that resource path here and then come back over to postman and then just paste that in now we'll come back to our query parameters and put in the model again and that query once more and we'll click on send and we get records back actually I've just noticed that I've put in XC60 instead of XC 70 so this is why we only get two records back so if we just quickly change that to 70 try it again we get those three those same three records back so what we would need to do then for all the requests that I've got saved now in my version two folder I'll need to go ahead and then just update the Uris for each of them respectively to include V2 so that we make it explicitly known to the server that we want to use the version 2 of that API so that's our versioning of servicenow apis works it allows us to do two things first create newer versions of our API and allow further developments and enhancements to take place and allow clients and applications to take advantage of those new features while at the same time too allow other clients and applications who want to continue to use their existing version to do that so in other words we're making developments in our API without breaking existing connections we can of course turn off those older versions whenever we like so we've actually finished developing our scripted rest API however there is one big problem with it do you know what that problem is security our API is a long way off from being secure so in the next six videos in this series we're going to take a close look at various platform features that will allow us to secure our API so I strongly recommend that you take a look at those videos foreign

View original source

https://www.youtube.com/watch?v=_Z4Br6dUPME