# Run flows via API

You can execute every flow using API, and the structure of requests stays the same regardless of a particular flow internals.

There are basically two requests: one to initiate the flow execution and another one to get results. And to perform any requests you need to obtain a secret key

## Get the key

Go to the [API & credentials](https://app.scade.pro/api-credentials) section, click the **Add new** button and pick the **Server Key** option

<figure><img src="/files/CWQYjfKfuhflNpRB5vSq" alt=""><figcaption></figcaption></figure>

Choose some comprehensive name for your key, and after the key is generated, make sure you save it somewhere in a safe place, because the key is shown only once and can be retrieved later

## Execute the flow

Pick the workflow that you want to run. Go to **Workflow builder** and proceed to the **Publish** button

<figure><img src="/files/T4Y7cRiK3REsXYZdqv5F" alt=""><figcaption></figcaption></figure>

### Form the request

If you have one **Start Node** and one **End Node** form in your workflow, and have already obtained the key, you are all set. You'll get code snippets which you can use right away — just don't forget to replace placeholders with your data.&#x20;

Here's a breakdown of out example

```
curl --location 'https://api.scade.pro/api/v1/scade/flow/1607/execute' \
  --header 'Authorization: Basic {ACCESS_TOKEN}' \
  --header 'Content-Type: application/json' \
  --data '{
      "start_node_id": "SYnn-start",
      "end_node_id": "y5iI-end",
      "result_node_id": "y5iI-end",
      "node_settings": {
        "SYnn-start": {
          "data": {
            "source": {STRING},
            "target": {STRING}
          }
        }
      }
  }'

```

`https://api.scade.pro/api/v1/scade/flow/<flow_id>/execute` — URL to send your request to. You can get `flow_id`  from the address bar of your browser

`"start_node_id": "SYnn-start"`  — the node that should be run first

`"end_node_id": "y5iI-end"` — the node that should be run last

`"result_node_id": "y5iI-end"` — the node from which results will be extracted; in most cases its `id` is the same as the `end_node_id`&#x20;

```
      "node_settings": {
        "SYnn-start": {
          "data": {
            "source": {STRING},
            "target": {STRING}
          }
        }
      }
```

— it's a place to put your data into. Replace placeholders like `{STRING}` with your actual data (say, URLs of a video and an image in this case)

`--header 'Authorization: Basic {ACCESS_TOKEN}'`  — replace `{ACCESS_TOKEN}`  with your key. See **Get the key** section if needed

#### Picking other nodes

It’s strongly recommended to have one **Start Node** and one **End Node.** But sometimes flows have more than one of each. In this case you'll have to go to the **Overview** on the left sidebar menu and from the dropdowns choose which node should be run first and which one should be run last upon your flow execution.

Aside from that, the drill of request formation is the same.

<figure><img src="/files/4nPbvpA7ECakdVRtPZfG" alt=""><figcaption></figcaption></figure>

### Retrieve task\_id

Once you get your request formed, send it using the `POST` method. If everything is correct, the response would be like this:

```
{
  "id": 22311,
  "data": {
    "node_id": null,
    "start_node_id": "SYnn-start",
    "end_node_id": "y5iI-end",
    "result_node_id": "y5iI-end",
    "init_context": {},
    "node_settings": {
      "SYnn-start": {
        "data": {
          "source": "https://example.com/video.mp4",
          "target": "https://example.com/image.jpg",
        }
      }
    }
  },
  "status": 2,
  "created": "2024-04-18T21:14:34.800001",
  "finished": null,
  "result": null,
  "execution_time": null,
  "execution_cost": null,
  "parent_id": null,
  "ref_id": "flow:1607;",
  "task_type": 4,
  "is_hide": false,
  "public_access_token": null,
  "file_result_link": null
}
```

We need to take `"id"` from the response to get the result

## Get the result

It's pretty straightforward. Here's the url:

`https://api.scade.pro/api/v1/task/<task_id>`&#x20;

Replace `<task_id>` with the `id` that you get upon the flow execution and send a `GET` request. Once the flow ends execution, you'll get the result.&#x20;

It may take some time, so send requests with intervals between them, and look for the `result` object in the response


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://scade.gitbook.io/scade-knowledge-base/publish/run-flows-via-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
