How to dynamically set an Environment Variable and use that in subsequent Requests

Just as a bit of background: I have an API that consists of 100 routes. The data is only available as GET, we don’t allow insert or creation of data via the API. I currently have several dozen collections, 1 for each of our environments, that allow me to run the collection and do a simple test on each of the 100 routes, to see if we are getting a 200 or 204 response.

Obviously, that’s getting to be a sizeable maintenance issue, particularly since we are now talking about supporting multiple versions of the API, necessitating even more collections. As such, its high-time I switched it all over to environment variables

It all works fine if I set those environment variables manually, but that means going through dozens of collections, and extracting the variables manually. I’d much rather make it dynamic, so that the requests update the environment variables and move on to the next request.

What I’m struggling with is the script to take my response body values, and assign those to variables.

At a simplified level, the body looks like this:

{
“LocalId”: “123”,
“SchoolName”: “St Augustin School for Girls”,
“RefId”: “B7724F26-77AA-4F19-AC03-9D3E1D6DB300”
}

I am trying to save the LocalID, which gets used in subsequent requests.

In Test, I am using the following:

let LocalID = pm.response.json().LocalID
pm.environment.set(“LocalID”,LocalID)

Which is resulting in NULL being assigned to the Current Value of the LocalID environment variable.

If I replace:
let LocalID = pm.response.json().LocalID
with:
let LocalID = “123”

Then that works fine, but it’s not dynamic.

How do I alter that script so that the LocalID environment variable is set correctly from the response?

@siftester01,
please check the spelling of your JSON elements, it’s case sensitive.

Your JSON contains LocalId, whereas your code contains LocalID. The “D” makes the difference.

Once you fix this, your sequence may work.

Best regards,
Karel