> For the complete documentation index, see [llms.txt](https://docs.mozark.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mozark.ai/no-code-studio/advanced/call-an-api-from-a-test.md).

# Call an API from a test

An **Invoke API** step calls an API in the middle of your test. You extract a value from the API response and use it in later steps.

## 1. Add the step

In the Step Builder header, choose **Add → Add step**, search for `invoke api`, and pick it. The step lands in the list as *No request configured yet*. Click **Configure**.

## 2. Configure the request

| Field      | Example                                                      |
| ---------- | ------------------------------------------------------------ |
| Collection | **+ New collection...** → `Optics API Testing`               |
| API name   | `get_name` (letters, digits and underscores only)            |
| Method     | `POST`                                                       |
| URL        | `https://optics-api-testing.free.beeceptor.com/api/get-name` |

Then open the **Params** tab:

| Key       | Value  |
| --------- | ------ |
| `user_id` | `1234` |

The drawer confirms what the step will call:

```
This step will call Optics_API_Testing.get_name
```

{% hint style="info" %}
**Base URL.** The first request in a new collection needs the full URL. Its host becomes the collection's base URL. Every later request in that collection can then be a plain path like `/api/get-name`.
{% endhint %}

**Variables work everywhere.** Params, headers and body all take `${refs}`, resolved when the test runs:

```
Params   user_id: ${user_id}
Header   Authorization: Bearer ${auth_token}
Body     { "email": "${user_email}" }
```

Click **Save**. The request is saved on the collection, so any other test can add a step that calls it by name.

## 3. Send it

Click **Send** to run the request against your live device session (you need a session open, the same one you are recording in). The **Response** panel shows the status, the time, the headers and the body:

```json
{ "name": "randomName" }
```

This is the same call the packaged run makes, so what you see here is what the run will do.

## 4. Use the response in a step

Hover the `name` value in the response and click **extract**. The modal shows the path and suggests a variable name:

```
JSON path   name
Value       "randomName"
Name        name
```

Click **Create**. It appears under **Extracted values** as `${name}`. The value refreshes on every run.

Now any **later** step can use it. Right-click the field on the device, choose **Enter Text**, and type `${name}` into the Text parameter. At run time step 3 types whatever the API returned on that run.

```
1  Invoke API        Optics_API_Testing.get_name   →  ${name}
2  Press Element     Search box
3  Enter Text        ${name}
4  Assert Presence   Results for ${name}
```

## Worth knowing

| Symptom                                  | Cause                                                                                   |
| ---------------------------------------- | --------------------------------------------------------------------------------------- |
| Send is disabled                         | No device session. Start one on **Test Creation**                                       |
| "The response is not JSON"               | Only JSON responses can be extracted from                                               |
| The API name is rejected                 | No dots, spaces or hyphens. The name is half of the identifier the run resolves         |
| A later step types the literal `${name}` | The extract was added after that step last ran. Send again, or replay from the API step |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mozark.ai/no-code-studio/advanced/call-an-api-from-a-test.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
