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

# Extracting Common Initialization Flow Tasks

You don't t like to repeat yourself. And that holds true for your API flows, too! The [*init flow*](/flat/reference/openapi-4/routing.md#init-flow) helps you avoid that. You can extract common initialization tasks that every flow would need into an *init flow* that is executed before the regular flow for your API paths.

This is a good place to

* set common response headers,
* initialize global variables,
* force error handling by setting status `500`,
* gather configuration data,
* or validate JWT tokens.

In your `swagger.yaml`, point the top-level property `x-flat-init` to a flow file:

```yaml
…
x-flat-init: init.xml       # ⬅ init flow
paths:
  x-flat-flow: default.xml  # ⬅ fallback flow
  /:
    get:
      x-flat-flow: get.xml  # ⬅ regular path flow
…
```

In this example, we use `init.xml` to set some global configuration, and provide the request's unique id to downstream systems in the `Request-ID` header (for log correlation).

```markup
<flow>
  <template out="$cfg">
    {
      "stage": {{ $server/role ?? "prod" }},
      "upstream_api": ${{ $env/API_ORIGIN ?? "localhost:3000" }}
    }
  </template>
  <set-response-headers>
    {
      "Request-ID": {{ $request/id }}
    }
  </set-response-headers>
</flow>
```

Now, we can rely on these preparations in all API flows. You can use the `$cfg` variable to build your upstream request URL, or to check the current stage. Every response for a valid endpoint will have a `Request-ID` header.


---

# 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 dynamically by asking a question.

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

```
GET https://sevenval.gitbook.io/flat/cookbook/init-flow.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
