When you're building and testing APIs, automation is key. Bruno, as an API client, empowers you to automate your API workflows using powerful JavaScript scripts. But to truly master Bruno's scripting capabilities and write effective, bug-free tests, you need to understand one crucial concept: script execution flow
Bruno offers two distinct ways your scripts can execute during a collection run: the Sandwich Flow and the Sequential (Natural) Flow. Knowing which one to use and how they work will help you write cleaner, more organized, and more efficient scripts.
Let's dive in!
Table of Contents
In Bruno, scripts can be attached at three levels. Each narrower level adds logic specific to that part of the collection:
Collection
Runs for every request in the collection.
Folder
Runs for requests inside that folder.
Request
Runs only for the individual request.
Every level can contain three script types:
Prepare variables, headers, auth, or body before the request is sent.
Read the response, extract data, and perform teardown after the API call.
Run assertions and validate the final response state after post-response scripts.
Bruno provides two modes for how these layered scripts interact during a collection run:
The Sandwich Flow is named for how it "sandwiches" the individual request's execution between the collection and folder level scripts. This is the default and most commonly used flow because it ensures all setup (pre-scripts) happens before the request, and all teardown/validation (post-scripts and test scripts) happens after the request, across all layers.
Imagine it like preparing and cleaning up a meal:
Sandwich execution order
Scroll horizontally on smaller screens. Pre scripts move inward; Post and Test scripts move outward.
This flow is ideal for managing authorization tokens, setting global variables, and performing clean-up or assertions that need to happen around the core request logic — from the request level back out to the collection.
The Sequential Flow, also called Natural Flow, executes each phase (pre, post, then test) from the outermost scope to the innermost. It's more of a top-to-bottom batch execution model.
Think of it as preparing all the ingredients, then cooking, then doing *all* the cleaning and inspection from the top down:
Sequential execution order
Every phase moves in the same direction: Collection → Folder → Request.
While less common for typical API testing, this flow can be useful in specific scenarios where you want each phase — pre, post, and test — to complete from collection → folder → request before moving on.
Regardless of whether you're using Sandwich or Sequential flow, there's a crucial rule to remember when running a collection:
One request means one complete script cycle
Any collection-level Pre-request, Post-response, or Test script executes once for every request that runs. The same applies to folder-level scripts for requests inside that folder.
This means if you have 10 requests in a collection and a script at the collection level:
This behavior is fundamental for tasks like:
At the most granular level:
These request-level scripts are where you define the specific setup, teardown, and validation logic unique to each API call, working in concert with the broader collection and folder scripts.
While the Sandwich Flow is the default behavior, you can explicitly set the desired execution flow (either Sandwich or Sequential) for your collection in your collection configuration.
For Bru collections, set the flow property in bruno.json:
bruno.json
{
"scripts": {
"moduleWhitelist": ["crypto", "buffer", "form-data"],
"filesystemAccess": {
"allow": true
},
"flow": "sequential" // Or "sandwich"
}
}
For YAML (OpenCollection) collections, set it in opencollection.yml:
opencollection.yml
extensions:
bruno:
scripts:
flow: sequential # Or "sandwich"
By setting "flow": "sequential", all collection runs for this collection will use the Sequential Flow. If this property is omitted, Bruno will default to the Sandwich Flow.
Quick tip: If your existing collection does not define flow, no change is required as Bruno automatically uses Sandwich Flow.
Bruno's script execution flows, particularly the default Sandwich Flow, provide a powerful framework for organizing your API automation. By understanding when and where each pre-request, post-response, and test script executes, you can write sophisticated tests, manage complex authentication, and streamline your entire API development and testing workflow.
Dive into Bruno's scripting features, experiment with different flows, and build API test suites that are not just functional but also clean, efficient, and robust!
For more detailed examples and advanced use cases, refer to the official Bruno documentation on Script Flow.
Join our Discord server to connect with the Bruno community!