API tests are a great way to get quick feedback that your APIs are working as expected. In mabl, you can test your APIs using API steps in browser tests and by creating API tests in the API Test Editor.
This tutorial explains how to create and configure an API test, from setting up auth, to configuring requests, to validating responses.
Before you start
In this tutorial, you will create an API test to validate the functionality of the query users endpoint in the mabl API. Before creating your test, gather the following pieces of information:
- Workspace ID: Go to Settings > workspace and copy the workspace ID.
- "Viewer" API key: Go to Settings > API and create a "Viewer" API key
API privileges
If you have an editor or viewer role, ask a workspace owner for permission to access the API keys.
Create a new test
Click on the New test button and choose API test. Give your test a name. For example: "Tutorial - API test - Name"
In Core settings, add the base API URL:
- Check the box "Enter a starting API URL to use to train your test."
- Select a protocol:
https://
orhttp://
- Enter
api.mabl.com
as the base API URL.
Creating a new API test
Click on the Create test button to launch the API Test Editor.
Set up Auth
When you send requests to mabl API endpoints, you need to provide an API key. To set up authentication, click on the pencil to edit test properties.
Adding auth
In the AUTH tab, select "Basic auth." Add "key" as the username and insert the "Viewer" mabl API key as the password.
Adding authentication for API requests
Set up your first request
For the first request, send a GET request to the Query Users endpoint. Click on the plus sign button to get started.
The URL for the Get Users endpoint is https://api.mabl.com/users
. Instead of entering the entire URL, use mabl variable syntax to simplify your request to {{@api.url}}/users
.
{{@api.url}}
{{@api.url}}
represents your base API URL: https://api.mabl.com
.
Click on the PARAMS tab and add the workspace ID as a parameter:
Adding request details
After configuring your request, click on the Send button. In the Results section, you should see a list of workspace users in the response body.
Checking the response body
Validate a successful response
By default, the API Test Editor creates an assertion that the HTTP status is 200. Add another assertion that the response returned a users
property:
- In the Validation and variable assignment section, select the Assertions tab.
- Click on the Add button.
- Enter the following JSON path:
users
- Select "Is present" as the type of assertion.
Creating an assertion
Send the request again, and view the results of your assertion in the Assertions tab of the Results section.
Testing an assertion
Create a variable
You can also create variables from the response. Let's create a variable that stores the email address of the first user:
- In the Validation and variables assignment section, select the Variables tab.
- Click on the Add button.
- Enter the following JSON path:
users[0].email
- Assign the following variable name:
email
Creating a new variable from the response
Add another request
For the final step in this tutorial, send another GET request to the Query Users endpoint with the "email" parameter.
Enter the URL {{@api.url}}/users
. Click on the PARAMS tab and add the following parameters:
-
workspace_id
: paste your workspace ID here -
email
:{{@email}}
*
*This is the value of the email address variable.
Adding a second request
Click Send and view the response in the Results section.
Validate the response
Create assertions to validate the response.
For example, the assertions in the following screenshot validate that the endpoint returns exactly one user and that the user's email address matches the value of the email
variable:
Validating the filtered response
Click Send again and check the Assertions tab in Results to validate that the assertion passed.
Putting it together
Click on the play button the top left corner to run the entire test in the API Test Editor. You've just created your first API test. 🎉
Next steps
Now that you've familiarized yourself with the API Test Editor, think about how you can use it to test your team's APIs. Some questions for further exploration:
- What functionality do you want to validate?
- Which endpoints do you need to send requests to in order to validate this functionality?
- How can you use your API endpoints to augment end-to-end browser tests in mabl?
Learn more
Learn more about how you can use API testing in mabl to increase test coverage: