Using Authentication Tokens Across Different Postman Collections: Facing 401 Error


Inquiry: I have an authentication API that provides me with a token. My goal is to use this token automatically in another API, which is in a separate collection, by setting it in a global variable. The response of the authentication API is a token string like this: hrzehrz3hbgr988BD89dh.

I have tried to write a Pre-request script in the collection as follows:

var res = pm.response;
pm.environment.set("token", res);

However, when I try to run another API that uses the token variable, I receive a 401 authentication error.


If I replace the token variable manually with the value hrzehrz3hbgr988BD89dh, the API works properly.


If I console.log(pm);

I have the following response :

{test: "[Function]"}

I don’t understand why I don’t see the response value.

Any help or suggestions would be greatly appreciated.

You would new to use pm.response.json() or pm.response.text(), depending on what type of response you are trying to access.

Also, pm.environment.set() would set that value in an environment variable and not as a global variable.

Is it better to use as a global or as an environment variable ? (I don’t really see the difference between them)

Depends on your overall use case for the usage of it. I only mentioned that because you were talking about setting the value as a global variable but in the script, you were setting this as an environment variable, which are different things.

A little more information about the different scopes can be found here: