Query datasets from scripts and mocks in Postman
Datasets are available on Postman Solo, Team, and Enterprise plans. For more information, see the pricing page.
You can query a dataset at runtime with the pm.datasets function in pre-request and post-response scripts and in mock implementations. This is useful when you need data beyond what a selected view exposes as iteration variables, such as looking up a row by a value from the current request, response, or iteration.
Use executeQuery() to run a SQL query, or executeView() to run a saved view instead of writing a query in the script. Query results return rows as an async iterable, so use a for await...of loop to read them. Learn more in the pm.datasets reference.
In the Postman app, pm.datasets requires the Postman desktop app. It isn’t available in the Postman web app.
Example dataset
In the following examples, assume you have a dataset named Customer Dataset with a local CSV file source named source_customers that includes the following data:
Reference a dataset in a script
To query a dataset, load it with pm.datasets() using the dataset’s ID. You can reference a dataset, source, or view anywhere in a request, including the base URL, auth, path parameters, headers, and body. Use the {{variable}} syntax, where variable is the name of the entity. For example, you can enter {{Customer Dataset}} as a path parameter in your request URL GET /customers/{{Customer Dataset}}. Postman resolves the variable to the entity’s ID when the request runs, so you can access it in a script without hard-coding the ID.
Query a dataset in a script
Use executeQuery() to run a SQL query with parameters, then read the returned rows with a for await...of loop. The following example uses an email from the current iteration, so it runs during a collection run, monitor run, or private runner run, including runs started with the Postman CLI. Values from pm.iterationData are available only during a run.
You can also use executeView() to run a saved view instead of writing a custom query in the script.
Write to a database source
When your dataset includes a database source, you can run write statements against it, such as INSERT, UPDATE, and DELETE, in addition to reads. This is useful when you need to prepare data in a database before a run, such as seeding the records your API reads during a test.
To run a write against a database source, pass the { source } option with the source’s name. The statement runs directly against the connected database, in its native SQL dialect, and changes its live data. For more information, see Query a database source directly.
The following example inserts a row into an orders database source in the dataset:
Postman uses the connected database’s own access control to determine what a query can do. To prevent writes, connect the database source with a database user that has read-only permissions.
Write statements change the live data in the connected database. Use a database and database user that are appropriate for the environment you’re testing, such as a dedicated test database.
Validate a response against a dataset
You can query a dataset in a post-response script to validate response data against the data stored in the dataset. This works in individual requests and monitors, enabling you to compare API responses with expected values, test multiple scenarios, and reuse the same data across workflows.
Use the following example to validate a response against a dataset:
-
Set up a request to an endpoint that returns customer data, such as:
-
In the request’s Scripts > Post-response tab, load the dataset and query it using a value from the response.
-
Click Send.
When the request runs, the script queries the dataset and compares the response data with the matching row. The Response matches dataset test passes when the response’s email, first_name, and status match the dataset row. It fails if any value differs, or if no row matches the response’s customer_id, which flags a mismatch between the API and the expected data.
Query a dataset in a mock
You can use pm.datasets in a mock to return dynamic responses based on queryable data. This enables you to use the same dataset across requests, filter data for specific endpoints, and simulate more realistic API behavior instead of returning only static responses.
Use the following example to query a dataset in a mock:
-
In your mock implementation file, load the dataset using
pm.datasets(). -
Run a query against the dataset in your request handler and return the matching row in the response.
-
Start the mock and send a request to the endpoint. For example, you can send a GET request to the following:
The mock queries the dataset when the request runs and returns the matching data in the response.
Learn more about writing pre-request scripts and post-response scripts, and using datasets in the Postman Sandbox.