# Test Workflow Step

Run one step of the workflow's draft on its own, exactly as Run in the editor does: with its saved configuration and connection, the saved sample outputs of the steps it references, and real side effects (a create step creates). The output is saved as the step's sample, which later steps' references and their tests read; a step that fails answers status failed with the error, not an HTTP error. A Loop, Wait, Flow control, Respond to webhook, Continue or Break step only runs inside a workflow run, and a Run workflow step, which queues another workflow's run, is not tested on its own. A function step's test checks the connections of the function's own steps too. Saving the sample changes the draft's revision. If-Match is optional on this route, unlike the draft's other changes, and recommended: send the revision you last read and a stale one is refused with 412 before the step runs; left out, the step runs on whatever the draft is now. Use meta.revision on your next change.

`POST https://connect.mindcloud.co/v2/workflows/:workflowId/steps/:stepId/test`

Permissions: `workflow:run`, `workflow:edit`.

## Parameters

### Path

- `workflowId` (string, required): The workflow's id, from List Workflows.
- `stepId` (string, required): The step's id, from List Workflow Steps or Get Workflow Definition.

### Headers

- `X-Company-Id` (string): Optional. The id of the organization to act in. Leave it out to act in the API key's own organization. A key can also act in its child organizations, and, with "Use this key across all of your organizations" on, in every organization its creator can open in the app. List them with GET /v2/companies. Every response's X-Company-Id header names the organization it acted in. See [Organizations](https://mindcloud.co/docs/api/rest/introduction/organizations).

### Body

- `environmentId` (string): The environment to run in, from List Environments. Defaults to the company's default environment.
  The environment to run in, from List Environments. Left out, the company's default environment.

## Example request

```bash
curl --request POST \
  --url 'https://connect.mindcloud.co/v2/workflows/Wq3kZ8pLx2Nb/steps/SyDtUxIQeocK/test' \
  --header 'Authorization: Bearer $MINDCLOUD_API_TOKEN' \
  --header 'Content-Type: application/json' \
  --data '{
  "environmentId": "Ev8kT3nW5qLz"
}'
```

## Response fields

- `success` (boolean): True when the step ran without an error: the same flag List Run Steps and Get a Run Step answer for a step of a run.
- `status` (string): succeeded or failed: the step's outcome, the same as success. It is not a run status: a run's status (finished, failed, ...) says where the whole run is.
  Allowed values:
    - `succeeded`
    - `failed`
- `output` (object): The first records of the output (a JSON array as the step answered it), capped at 200 records and about 60 KB. Empty when there is no sample.
- `total` (number): How many records the output had in all. Null when there is no sample.
- `truncated` (boolean): True when output holds fewer records than total: the rest were left out of the sample, not lost. Later steps still learn their fields from the records kept.
- `truncatedBy` (string): Why output stopped short of total. Null unless truncated is true.
  Allowed values:
    - `records`: It reached the 200-record cap.
    - `size`: Its records reached about 60 KB first, so it keeps fewer than 200.
- `outputFields` (array of objects): The fields of the output records, learned from the sample and offered to later steps.
  - `key` (string): The field's path in each record: dotted, with [] marking an array (lines[].sku). Reference it as {{ steps.<stepId>.output[0].<key> }}.
  - `type` (string): Its type as seen in the sample: string, number, boolean, object, date, a type with [] for an array, or mixed.
- `error` (object): Why the run failed. Null unless status is failed.
  - `code` (string): The runtime's error code (ARGUMENTS_MISSING, 401_UNAUTHORIZED, DEFAULT, ...).
  - `message` (string): What the code means, for a person. What the app answered, if anything, is in output.
  - `status` (number): The HTTP status the app answered, when it answered one.
- `savedOn` (date): When the sample was saved. Null when there is none.
- `revision` (string): The draft's revision after the sample was saved (a sample is part of the step): send it in If-Match on your next change to the draft.
- `durationMs` (number): How long the step took to run, in milliseconds.
