# Your first workflow

Write a workflow that sorts incoming support tickets, upload it, start a run with an API key and read what it decided.

## 1. Write the workflow

A workflow is a YAML file: the request it accepts, the steps it runs and what it returns. This one asks the model to read a ticket and return a fixed shape, so whatever starts it can rely on the answer.

```yaml title="triage.yaml"
Workflow: TriageTicket
Description: Reads a support ticket and says what it is about and how urgent it is.
Triggers:
  - New: Ticket
    Schema:
      type: object
      required: [Prompt]
      properties:
        Prompt: { type: string }
Output:
  Structured:
    type: object
    required: [Topic, Urgency]
    properties:
      Topic: { type: string, enum: [Billing, Refund, Bug, Account, Other] }
      Urgency: { type: string, enum: [Low, Normal, High] }
Steps:
  - ModelCall: Triage
    Instructions: Read the ticket. Say what it is about and how urgent it is.
    Output:
      Structured:
        type: object
        required: [Topic, Urgency]
        properties:
          Topic: { type: string, enum: [Billing, Refund, Bug, Account, Other] }
          Urgency: { type: string, enum: [Low, Normal, High] }
```

Model step, Ask the model:

- Step kind: `ModelCall`
- Name: `Triage`
- Model: `Auto`
- Output: `Structured`

## 2. Upload it

Send the YAML as the request body with an API key of your Workflow organization, created in [Settings › API keys](/settings/workflow/api-keys) and shown once. Momor compiles it before storing anything: a workflow that fails is refused with `422` and one line per problem, giving the line, column and field. A workflow that passes comes back with its id and the content hash of this version; keep the id to start runs on it.

```bash
curl -X POST "https://api.momor.ai/workflow/workflows" \
  -H "Authorization: Bearer $MOMOR_API_KEY" \
  -H "Content-Type: application/yaml" \
  --data-binary @triage.yaml
```

```json
{
  "id": "4f6d2c1e-8a3b-4f0e-9d2a-7c5b1e0f3a21",
  "name": "TriageTicket",
  "contentHash": "9b1c…"
}
```

You can also save it in [Workflows](/workflows): choose Start from nothing, switch the editor to YAML, paste the file and choose Save version. No Workflow organization yet? [Create one](/organization/new/workflow).

## 3. Start a run

Start a run on the workflow with the same key and send the ticket in the same request. The body is checked against the `Ticket` schema; when it passes, the run opens its first turn and Momor answers `202` with that turn while the steps run.

```bash
curl -X POST "https://api.momor.ai/workflow/workflows/$WORKFLOW_ID/runs" \
  -H "Authorization: Bearer $MOMOR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "Prompt": "I was charged twice for my October invoice." }'
```

## 4. Follow the run

Ask for the same request as an event stream and the response follows the turn to its end instead: an `opened` frame, every event the turn records, then `closed`. The answer is in the `result.committed` event.

```bash
curl -N -X POST "https://api.momor.ai/workflow/workflows/$WORKFLOW_ID/runs" \
  -H "Authorization: Bearer $MOMOR_API_KEY" \
  -H "Accept: text/event-stream" \
  -H "Content-Type: application/json" \
  -d '{ "Prompt": "I was charged twice for my October invoice." }'
```

```text
event: opened
data: {"runId":"…","turnId":"…","queueItemId":null}

event: turn.started
id: …
data: {"event":"turn.started","path":"…","turnId":"…","payload":{…}}

event: model.replied
id: …
data: {"event":"model.replied","path":"triage","turnId":"…","payload":{…}}

event: result.committed
id: …
data: {"event":"result.committed","path":"…","turnId":"…","payload":{…}}

event: closed
data: {"state":"Completed"}
```

> **Read it again later**
> Every run keeps its record. `GET /workflow/runs/{runId}` returns the run and its turns, and the same address as an event stream replays every event from the start. See [Run history](/docs/workflows/run-history/).

## Next

- [Core ideas](/docs/workflows/core-ideas/): Workflows, runs, turns, steps and the record every run keeps.
- [Ask the model](/docs/workflows/steps/ask-the-model/): Every field of the step you just used.
- [Start a run](/docs/workflows/api/start-a-run/): The request and response in full.
