> For the complete documentation index, see [llms.txt](https://jexia.gitbook.io/semaphore/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jexia.gitbook.io/semaphore/cookbook/flows.md).

# Flows

![](/files/-MBhbZZmGihDkhzcYMr0)

## Introduction

A flow contains a collection of resources which is executed when a endpoint gets triggered. If an error occurs during the execution of the flow are all rollbacks of executed calls called.

```bash
flow "todo" {
    resource "query" {
        request "service" "GetTodos" {}
    }
}
```

{% hint style="info" %}
Services are defined inside the schema definitions
{% endhint %}

You could expose flows by defining an endpoint. An endpoint defines the protocol to be used and protocol-specific options.

```bash
endpoint "todo" "http" {
    method = "GET"
    endpoint = "/todos"
}
```

## Resources

### Requests and rollbacks

A resource inside a flow could contain a request and/or a rollback. Requests are called when a flow is triggered. If any of the defined resources returns an unexpected error the rollbacks of all executed resources executed. Errors returned by rollbacks are ignored.

```bash
resource "create" {
    request "service" "CreateUser" {
        username = "{{ input:username }}"
    }

    rollback "service" "DeleteUser" {
        id = "{{ create:id }}"
    }
}
```

![sample SAGA pattern](/files/-M3NvAUCMvFWwGFmZ767)

#### Headers

Headers contain keys and values which define constants or references. Headers are passed to the called and send to the service. Headers could be defined in both requests and rollbacks.

```bash
input "schema.Object" {
    header = ["Authorization"]
}

resource "create" {
    request "service" "Authenticate" {
        header {
            Authorization = "{{ input.header:Authorization }}"
        }
    }
}
```

{% hint style="success" %}
When referencing header properties inside other resources remember that they have to be defined.
{% endhint %}

## Flow resource branches

Nodes (resources) are executed concurrently from one another. When a node is executed a check is performed to check whether the dependencies have been met. Only if all of the dependencies have been met is the node executed.

```css
+------------+
|            |
|    Node    +------------+
|            |            |
+------+-----+            |
       |                  |
       |                  |
+------v-----+     +------v-----+
|            |     |            |
|    Node    |     |    Node    |
|            |     |            |
+------+-----+     +------+-----+
       |                  |
       |                  |
+------v-----+            |
|            |            |
|    Node    <------------+
|            |
+------------+
```
