How can I call an API from the same Collection before calling any API in the Collection

I have a list of apis in collection the first of is get Token API now , when i call any other apis from the collection , i want to called that get Token and fill an collection environment variable ,
how i can do that?

Hey @flight-meteorologis1 :waving_hand:

You could try using the new runRequest feature in a Pre-request script. This is an example of what that might look like:

// Create a new API key using the runRequest function which calls the required API endpoint
let getToken = await pm.execution.runRequest('<requestUUID>');

// Only set the variable if the get Auth token response is successful
if (getToken.code === 200) {
    pm.collectionVariables.set('token', getToken.json().token);
}

That will run the referenced request, if that was successful it will set the response value (the token) as the Collection Variable to be used in other requests. I don’t know the structure of your response so it’s likely going to be something different.

1 Like

Thank you Danny

there is not any solution to use an api from the my collection ?.

i want to call api (get token) from collection that i have.

instead of using a raw request from scratch

something like this flow :
response=pm.collection(NameOfCollection).run("nameOfApiInCollection);

That’s what’s going to give you the ability to do that and call that request.

If you start typing the name of the request in the runRequest() method you will see a list of requests.

// Create a new API key using the runRequest function which calls the required API endpoint
let getToken = await pm.execution.runRequest('<requestUUID>');

// Only set the variable if the get Auth token response is successful
if (getToken.code === 200) {
    pm.collectionVariables.set('token', getToken.json().token);
}

Did you try it? What’s not working for you?