Once development teams begin using Postman to test their APIs, a logical follow-up question is how Postman can interact with databases. Postman can interact with any available HTTP endpoint, including REST-enabled database endpoints. Instead of querying a database directly, developers and testers use Postman to read and write to a database via a REST API.
Why would you want to do this? During development when you’re working with a local version of your database, you can use Postman to read and write to your database and make assertions about those responses. In this manner, you can test the internal state of your database that isn’t exposed by your API. Furthermore, using Postman can be a faster way to learn and use the database for someone who’s unfamiliar with SQL.
There are 2 ways to access your database using Postman:
- You can start a local database server, use scripts in Postman to build a request to send to that server, and log entries in a local instance of a database, kind of like how you would write to your local file system.
- Additionally, many database providers, like MySQL, MS SQL, and Mongo, provide an HTTP API to access their databases.
In this example, we’ll take the second approach, walk through how to set up a database using Restdb.io, and then access the database using Postman.
Setting up the database in Restdb.io
In their quick start guide, Restdb.io walks through setting up a database, creating a restdb.io collection (an abstraction similar to a database table), and adding fields to the collection.
Once our database is set up, we will have a unique URL as its main access point, such as
https://example-5a1e.restdb.io, that we’ll need for the next step.
In this example, I’ve created a restdb.io collection called
contact and entered some records containing fields like
Setting up Postman to hit the database
In the Postman app, enter the URL from the previous step into the request builder. If you’re going to be switching between with different server environments or databases, consider swapping out path elements for environment variables using a double curly brace syntax. The full URL should look something like this:
Under the Headers tab, specify your
Still under the Headers tab, add your
x-apikey along with the value of your restdb.io key from their Users and Settings page for the database. In Postman, you can paste the key directly in the field, or store it as an environment variable like we did in the first step.
Read, Write, and Test
Now, Postman is ready to send a request to your database. Hit Send and inspect the database API’s response.
Just like testing your API to ensure responses are successful and as we would expect, we can add some tests to make assertions about your database responses as well.
For example, if we added a new field called
active, but didn’t plan to expose it to our users via API, we could test if this new field exists with a database query using Postman.