Postman API Tips: Use Flows to Order Folders and Requests in Collections

Postman Collections enable you to link API elements and facilitate documenting, sharing, and testing APIs. Collections are sets of requests organized and grouped into folders, and can be sorted manually to follow a specific order or hierarchy.

But what if you’ve already got a collection with hundreds of requests and folders, but want it alphabetically? Doing this manually is a tedious and error-prone process. But with the Postman API you can solve this problem!

The collection transfers APIs

With the Postman API’s collection transfers endpoints you can move or copy requests, folders, and examples. When moving requests, you can specify where in the collection you want to move it. For example, you can pass a location object with the position=end property, which orders the request at the end of the collection.

We can leverage these endpoints to sort a collection’s requests:

  1. Call the GET /collections/{collectionId} endpoint to get all the collection’s requests.
  2. Compile a list of the request IDs and names.
  3. Sort the request list by the request name alphabetically.
  4. For each request (in alphabetical order), use the POST /collection-requests-transfers endpoint to move the request to the end of the collection. Eventually, all the requests will be in alphabetical order.

For the sake of simplicity in this example, we’ll consider that the collection contains only requests and no folders. You can extend this example if you’d like, as these APIs also support moving collection folders.

Sort collection requests with Postman Flows

Postman Flows is a tool integrated in the Postman ecosystem that allows the creation of workflows that you can run on demand. These workflows consist of a set of steps that can include API calls. We’ll implement the previous workflow using Postman Flows.

We’ve made the flow and its related collection available in the Postman Public Workspace.

Step 1. Get the collection information with the Postman API

We’ll use flows inputs and scenarios to add a new input (the collection’s ID) to the flow. Then, we define a scenario where we’ll run the flow with the collection we want to sort:

When we run the scenario, we can pass the input to the next steps and use it to fill variable values in the requests. We’ll use the GET /collection endpoint to get the collection’s information:

Step 2. Compile a list of IDs and names

Next, we’ll parse the list of collection items, extract the requests’ ID and names, then alphabetically sort them:

Step 3. Reorder requests with the Postman API

Lastly, we’ll run a loop and call the collection transfers API to move all the requests, in order, to the end of the collection, which will eventually sort all the requests in alphabetical order:

{
    "ids": [
        "{{requestId}}"
    ],
    "target": {
        "model": "collection",
        "id": "{{collectionId}}"
    },
    "location": {
        "position": "end",
        "model": null,
        "id": null
    },
    "mode": "move"
}

…and these will be the steps in our flow to perform the loop:

Next steps

You can use webhooks to run flows, which enables the sending of information in the request payload that you can use in the flow. In this case, we could send the collection ID in the payload and read it in the flow instead of using a scenario and manual run. The ability to run flows programmatically is very powerful, and together with the Postman API you can automate many complex tasks related to your Postman account and resources.

Do you have any use cases you’d like to solve? We’d love to hear about and help you solve them, so please let us know in the comments below!

5 Likes

Oh this is neat! Thanks for sharing @david-espi-hernandez

1 Like