Flow Designer - REST Step | how to pass a token from script step to the REST Step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 02:00 AM
Hi, due to different requirements on the body I need to query the token in a script step before the rest step.
The token I get is currently still in the log.
Now I have problems to pass the token to the rest step.
The token uses a connection alias and in my opinion expects the token in the oauth_credential table.
For example, if I request the token manually via the UI action ‘get Oauth Token’. The token is stored in the oauth_credential table and the REST step then accesses this token.
How do I do this in a script step for the REST step?
How do I transfer the token?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 02:26 AM
There are 2 solutions for this:
Option 1: Use a Scripted REST API to Return the Token
*If you prefer not to embed credentials in Flow Designer:
1. Create a Scripted REST API to fetch and return the token securely.
2. Call this API in Flow Designer (REST step) before your actual API call.
Option 2:
Token
1. Create a Subflow:
• Go to Flow Designer → Create a new Subflow.
• Add a Script step at the beginning to retrieve the OAuth token.Token
2.Script Step Logic (example):
(function execute(inputs, outputs) {
var client = new sn_ws.RESTMessageV2();
client.setEndpoint('https://your.oauth.server/token');
client.setHttpMethod('POST');
client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
client.setRequestBody("grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET");
var response = client.execute();
var responseBody = response.getBody();
var parsed = JSON.parse(responseBody);
outputs.access_token = parsed.access_token;
})(inputs, outputs);
3.Add REST Step in the Subflow:
• Use the access_token output from the script step to set the Authorization header:Add REST Step in the Subflow:
• Use the access_token output from the script step to set the Authorization header:
Authorization: Bearer ${access_token}
4. Use the Subflow in your main Flow:
• Replace your REST step with a call to the Subflow you created. 4. Use the Subflow in your main Flow:
Please mark helpful if you are satisfied with my response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 03:38 AM
you can use Script Step to get the token, store it in Output variable and then use it in next step
refer below links on how to get token from script
OAuth2.0: Get new Access Token from existing Refresh Token
How to Setup OAuth2 authentication for outbound RESTMessageV2 integrations
OAuth : Script to Automate Token Request
How to generate the access token once it is expired from Business Rule?
How to get Auth Token using script when grant type is Authorization code
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2025 05:46 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2025 11:39 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader