Reusing Functions

Under Main Collections, I have created 2 folders which are defined at same level.

  • Main Collection
    • Reusable Function
      • Folder With Feature Name1
      • Folder With Feature Name1
    • API Tests

I’ve created a function inside Folder With Feature Name1 which is a sub folder for Reusable Function folder. For ex:

Utils = {
functionName: async function(){
//// My Code Here ///

How to call this function in any subfolder or request created under API Tests folder? Calling this way Utils.functionName() is not at all working.

Please share the suggestions.

Hi @gourish-mahale,

I found an example on one of the older posts a while back. You are along the right line though.

if you add the helper / Utils to the folder Pre-script or Test area you should be able to call it. I found the scope is restricted to the Pre-script and Test areas so if you want a function that can be used in both areas you need to add to both tabs.


In the Test Tab of the Folder or Collection add the following:

helper = {
    decodeBase64: function (data) {
        return Buffer.from(data, "base64").toString("ascii");

Then in the requests, you can add the following code:

const decodedData = helper.decodeBase64(data)

I’ve not used async, so not sure if that’s whats causing your issue.

Thanks for responding Paul…

I ain’t sure how is this gonna help me with the solution that I’m looking for.

Hi @gourish-mahale.

Can you add your shared code to the Main collection pre-request to Test Tabs and call it from the subfolders?

Yes Paul. Problem with reusing a function only occurs between child folders created on same level of main collection.

The function that I created under Reusable Function folder, I’m unable to call it in any requests created under API Tests folder.

I see what you are saying now.

if your function is in the Reusable Function folder it will be out of the scope of the API Tests folder.

If you move the code to the main collection, it should work how you want it to. Effectively it will give it global scope.

I have done that a lot, standard functions I add to the collection level, so I know I can use them anywhere in the collection.

This is the problem, Paul. If we keep adding all the functions in Collections level, then the code added in the pre-requisite level will be too much. If it difficult to follow design patterns in Postman to make our code more maintainable and reusable.

Yes, that’s a fair comment.

You may have to change the structure of the folders to work around it.
if you don’t want to add all the shared code to the collection level.

no tool is perfect, unfortunately.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.