API testing: the Call API step
Send a request to a REST or GraphQL API with no code, check its answer, save values for the later steps, upload files, sign in with OAuth 2.0, and run one test against several servers with variables.
On this page12
- 1API testing inside your end-to-end tests
- 2Your first request
- 3Check the answer
- 4Save a value for the later steps
- 5Variables: one test, several servers
- 6What a request can send: JSON, forms, files, GraphQL
- 7Authorization: tokens, keys and the OAuth 2.0 sign-in
- 8Waiting and trying again
- 9Read it in the Confidence Report
- 10In your pipeline
- 11From a prompt
- 12Keys and passwords stay out of the test
API testing inside your end-to-end tests
A Call API step sends one request to an API and checks the answer. It needs no code: the request, its checks and the values it saves are rows you fill in, and the server's real answer is one click away while you build it.
| Use it to | Example |
|---|---|
| Prepare what a test needs | Create an order through the API, then check that the app shows it. |
| Check what the app really did | After a click on Save, ask the server whether the order is there, with the right total. |
| Clean up afterwards | Delete the order the test created, so the next run starts from the same state. |
| Test the API itself | A test made of Call API steps alone: sign in, create, read, change, delete. |
The step works in any test: a desktop app, a web app, a phone, or no screen at all. It needs no browser and no web page.
Your first request
- 1
Add the step
In GUI Test Studio, open the Events palette and drag Call API from the Test Flow group onto the timeline.It lands on the time ruler, like a Wait, and runs at that moment of the test.
- 2
Open the request
In the step's Properties, click Edit Request....A line at the top of the window always says the one next thing to do.
- 3
Type the address and press Send
Choose the method, such as
GETorPOST, type the address, then press Send.The server's real answer appears on the right: its status, how long it took, and a JSON answer as a list you can unfold. - 4
Click a value of the answer
Click a value, then Check This Value: the check is written for you.Or Save as Variable, to use the value in the steps that follow.Nobody types a path.
- 5
Apply
Every check says at once whether it holds on the answer shown.Click Apply when the step is right.
Check the answer
| What | How it is checked |
|---|---|
| The status | Any success by default, or the one you choose: 201, a list such as 200, 201, a range such as 200-204, or any status. |
| A value of a JSON answer | It equals a value, contains a text, exists, is empty, is more or less than a number, or matches a pattern. |
| A header | The same comparisons, on the header you name. |
| The whole answer | It contains a text, or does not. |
| The response time | The answer came in less than a number of milliseconds. |
A value is named the way people say it: customer.name, items[0].price, items.length for how many items a list holds.The editor writes it when you click a value.
A request without a token must be refused.Send it: the answer is 401 and the editor offers Expect 401.One click, and the step passes because of the refusal.
Save a value for the later steps
Give a value of the answer a name, such as orderId.Every later step can then use {{orderId}}: in another Call API, in the expected text of a Text Assert, in a Run JavaScript or an Open Web App.
A first Call API signs in and saves token as apiToken.The second one sends {{apiToken}} as its Bearer token.In the editor of the second step, Run the Steps Before This One runs the sign-in first, so Send works at once.
Variables: one test, several servers
Write {{baseUrl}}/orders instead of the full address, and the same test runs against your test server, staging or a colleague's machine.Settings then Variables... lists them in both apps.
A variable takes the first value found, in this order:
| Where the value comes from | Use it for |
|---|---|
| An earlier step of the run saved it | A token, an order's id. |
| A file given to the run on the command line | One file per environment: staging, production. |
| An environment variable named GTS_VAR_ followed by the name | A secret your pipeline keeps. |
| This computer's variables | Your own keys and passwords, kept encrypted for your Windows account. |
| The test's own variables | Values that travel with the test file, such as the default server. |
Some values are made new at every use: {{$uuid}}, {{$timestamp}}, {{$today}}, {{$randomInt}}, {{$randomText}}.Use them for data that must be unique, such as an email address.
What a request can send: JSON, forms, files, GraphQL
| Body | What it is for |
|---|---|
| JSON | What most APIs take. Tidy Up lays it out, and a mistake is said under the text as you type, with its line lit. |
| Form fields | What a web form sends: names and values. |
| Form with files | A form that uploads a photo or a document too. Each row is a text or a file. |
| GraphQL | A query, its variables and, when the query holds several operations, the name of the one to run. |
| Text | Anything else, sent as it is typed, such as XML. |
Upload a file
Choose Form with files, name the field, then press File on its row and choose the file.A file in the test's folder is kept by its path from there, so the test can be moved to another computer with its files.The report shows the file's name, type and size, never its content.
GraphQL
A GraphQL answer can report errors under a status 200.The step therefore fails when the answer reports an error, and says what the server said.The cases to copy are in GraphQL testing.
For a test that checks the error itself, untick Fail the step when the answer reports an error and add your own check on errors[0].message.
Authorization: tokens, keys and the OAuth 2.0 sign-in
| Authorization | What the step does |
|---|---|
| Bearer token | Sends the token you give, usually a variable. |
| User name and password | Sends them with every request. |
| API key | Sends the key in a header or in the address, under the name the API reads. |
| OAuth 2.0 | Signs in first: it asks the token address for a token, then sends the request with it. |
The OAuth 2.0 sign-in
Give the Token address, the Client ID and the Client secret the API gave your app, and a Scope when its documentation names one.The step signs in as the app itself, with no person and no browser.Choose As a test user to send a user's name and password too.
The sign-in is made once: the later steps of the run that sign in the same way use the same token, until it ends. When the sign-in is refused, the step fails with the reason the sign-in service gave.
Which way to sign in, and the cases that must be refused, are in How to test an API with OAuth 2.0.
Waiting and trying again
Wait up to is how long the step waits for the answer, 30 seconds by default.With Keep trying until it passes, the request is sent again every second until its status and every check hold, up to that time.
After the click on Pay, a Call API with Keep trying until it passes asks for the order until its status equals paid.The test goes on the moment it does.
Like every step, a Call API has If It Fails: Stop the test, the default, or Continue the test.
Read it in the Confidence Report
The step's row says what the call did, such as POST /orders answered 201 Created in 182 ms. Select it to see the request, one line for each thing the step looked at, and the answer: as a list you can unfold, as text, its headers, and what was sent.
A bug report words the step and attaches every request of the test with its answer.See Create a bug report.
In your pipeline
Give a run its values with a file, one per environment: gtr-cli run tests --variables staging.env.The file holds one value per line, such as baseUrl = https://staging.example.com/api.
A secret needs no file: set an environment variable named GTS_VAR_ followed by the variable's name, such as GTS_VAR_apiToken, as a secret parameter of your pipeline.
When a Call API step fails, the console and the JUnit file say which check did not hold and the beginning of what the server answered.With --artifacts, the folder holds api_calls.txt: every request of the test with its answer.See Run tests in CI/CD.
The same file works when a web test is generated for other browsers and devices: gtr-cli generate tests --branch missing --variables staging.env.
From a prompt
An AI assistant connected through the connector adds Call API steps too.It can send a request first to read the answer, then add the step with its checks and saved values.See Create a test from a prompt.
Keys and passwords stay out of the test
| Rule | What it means for you |
|---|---|
| A key or a password is never written into a test file. | Keep it under Variables, This computer, or in your pipeline, and write its name between double braces in the step. |
| A secret is never written into a result. | The report, the console, the files and the bug report read (hidden) in its place. |
| A variable is a secret when you tick Secret, or when its name says so. | Names such as apiToken, password, clientSecret or key are hidden by themselves. |
| A token goes only where you sent it. | When a server redirects the request to another server, the authorization is not sent along. |
To go further, read API test cases you can copy, API testing vs UI testing and API testing without code.
Next: Network activity and logs, to check the requests your app itself makes, or Test Flow: waits, apps, phones and scripts.
Still stuck?Tell us what you were trying to do, and we answer within one working day.
Ask our team