Api monitor with variables

My question: I’m trying to set up a monitor for a couple endpoints. I apologize in advance for my knowledge level here. I’m not even completely sure how to describe the problem, so searching for existing answers came up flat.

Here goes:

I want to test an endpoint with a fixed base URL with one variable. The variable will have a list of values that I want tested. Think of the variables as tokens pulling different data from the same endpoint which represents brands (Coke, Pepsi, RC Cola, 7Up…whatever)

Like this - {{base URL}}?api_token={{variable}}

I’d like to run the test every couple hours, pulling a random value (from the list) for the variable each time. Is that possible? How would someone do this. Keep in mind that I can read and follow concepts, but I don’t consider myself experienced working with APIs.

Or, is the easiest thing just to set up a fixed URL for every version of the variable and just run them all, all the time?

Does the question make sense?

Hey @spencer

Where and how will you be storing the variables? Could you provide an example, please?

For example, if you have them at the Collection level as an array [1,2,3,4,5], you could do this to randomly select an item from the list on each monitor run:

let ids = JSON.parse(pm.collectionVariables.get('ids'));

pm.variables.set('variable', `${_.sample(ids)}`);

In the single request view:

In the monitor view:

1 Like

Yes yes! I think this is exactly it.

Your solution seems simple enough that maybe I can even stumble my way through it. I’m not exactly sure how to store the array (which is actually just a list of 64-character API Keys) in the collection. So I googled that bit, and found this link which is definitely getting at what I’m trying to do. It’s good to understand the name of this outcome is to ‘loop through’ the array. I guess I should have realized that.

The example at the link, though, doesn’t seem to be randomizing the swapped-in attribute. He’s calling for it specifically each time, so I have to look closely at what you’re doing here, and what this guy is doing to see if I can figure that out.

It looks to me like he’s storing his array right in the pre-request script (another key concept I learned today). I assume I could do the same in your example?

Thanks Danny,

Wait wait wait.

I studied your code more carefully.

You’re saying that I can store these 64-character keys as variables in the collection, and then pull one variable each run using your script. Okay. That’s great.

When I add the variables to the collection, is each key a new variable, or is there just one variable that I somehow assign an array of possible values to?

I see that when I create a variable, I have an Initial Value choice and a Current Value choice. It seems like this piece of help is telling me that I can store all the options of the array in one variable (which you’re calling ‘ids’) if I first stringify it. I feel like that’s getting warmer?

just having trouble making this mental connection now.

Thanks again.

Glad it’s put you on the right track :trophy:

I was just using a Lodash function in the script, to pick a random item from an array.

https://lodash.com/docs/4.17.15#sample

You could do this:

const dataArray = [
    "TWiYDwAAQBAJ",
    "8ahgioXQ9g8C",
    "K98hhw0IEHgC",
    "mDKphT8_XLsC",
    "XfDOcmJisn0C",
    "vxX2JGsN7PoC",
    "5x4uDwAAQBAJ",
    "mmlgAAAAMAAJ",
    "jetfAAAAMAAJ",
    "38xQHS4h0yEC"
];
pm.variables.set("variable", `${_.sample(dataArray)}`);

For my first example, I just had this stored at the Collection level:

Everything stored in the variable is a string so I’m using JSON.parse to read that as an array.

I’m using the pm.variables.set() to do like a one time setting of a value per request, it’s not storing that at any other scope.

When you set a variable value using pm.variables.set , the value is local and will only persist for the current request or collection run

let ids = JSON.parse(pm.collectionVariables.get('ids'));

pm.variables.set('variable', `${_.sample(ids)}`);

I like this better. I’m gonna try it!

1 Like

This worked, btw. I had to really put my thinking cap on to get this done, though. Just because I have almost no idea what I’m doing. :slight_smile:

Thanks for this great help. Was fun. And now functional.

1 Like