Save Automatically Access Token from API in Global Variables: Postman, NodeJS
Copy Below Code
View As A Text File
Show Text Only
Show API
Edit Code
If you wish to save your Access token automatically when you login API hits in global variables and is available for every request. You can use below script.
Go in Scripts -> Post Request .. and add it.
// Ensure the API response contains the token
if (pm.response.code === 200) {
// Parse the response body
const responseBody = pm.response.json();
// Assuming the token is in the response body under 'access_token'
const accessToken = responseBody.data.accessToken;
// Store the token in a Postman global variable
pm.globals.set("Budget-Token", accessToken);
// Optionally, log the stored token for confirmation
console.log("Access Token saved to Postman globals:", accessToken);
} else {
console.error("Failed to retrieve access token:", pm.response.text());
}