# Creating a workflow

A **workflow** is defined as a class that contains a number of **steps** using functions, and the **workflow state** modelled as a separate class.

A workflow can be started by one or more different types of messages. Each time a workflow is started, a new workflow state is created. This state will be available to each of the steps in the workflow, and each step can mutate the state by returning some of its properties.

### Creating

Create a workflow state by declaring a class that extends **WorkflowState**.

The `$name` property should be unique among all of your workflows.

```typescript
// fulfilment-workflow-state.ts
import { WorkflowState } from '@node-ts/bus-core'

export class FulfilmentWorkflowState extends WorkflowState {
  static NAME = 'FulfilmentWorkflowState'
  $name = FulfilmentWorkflowState.NAME
}
```

Create the workflow definition that extends **Workflow**, passing it a generic parameter of the workflow state.

By extending this class you'll need to provide a`configureWorkflow(mapper: WorkflowMapper<TState, TWorkflow>): void` implementation. This mapper is used to configure how messages are dispatched to functions in the workflow.

```typescript
// example-workflow.ts
import { Workflow } from '@node-ts/bus-core'

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
  configureWorkflow (
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper.withState(FulfilmentWorkflowState)
  }
}
```

Lastly register the workflow with the bus configuration on startup.

```typescript
const bus = await Bus.configure()
  .withWorkflow(FulfilmentWorkflow)
  .initialize()
```


---

# 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://bus.node-ts.com/guide/workflows/creating-a-workflow.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.
