> ## Documentation Index
> Fetch the complete documentation index at: https://docs.threadify.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Capturing Workflows

> Capture business workflows as threads (execution graphs)

### Starting a Thread

A thread represents a workflow execution instance (e.g., an order, a support ticket, a user onboarding). A thread is an execution graph containing all actions performed by services involved in delivery an outcome to a customer's request

<CodeGroup>
  ```javascript JavaScript theme={null}
  const thread = await threadify.start('User-Onboarding-cust-123');
  ```

  ```go Go theme={null}
  thread, err := conn.Start(ctx, "User-Onboarding-cust-123", "")
  if err != nil {
      log.Fatal(err)
  }
  ```

  ```python Python theme={null}
  thread = await connection.start("User-Onboarding-cust-123")
  ```
</CodeGroup>

### Recording Steps

Always create the step **before** executing business logic:

<CodeGroup>
  ```javascript JavaScript theme={null}
  const step = thread.step('order_received');

  // Execute business logic
  const order = await receiveOrder();

  // Record success with context
  await step.addContext({ orderId: order.id, total: order.total }).success();
  ```

  ```go Go theme={null}
  step := thread.Step("order_received")

  // Execute business logic
  order, err := receiveOrder()
  if err != nil {
      log.Fatal(err)
  }

  // Record success with context
  _, err = step.AddContext(map[string]any{"orderId": order.ID, "total": order.Total}).Success(ctx)
  if err != nil {
      log.Fatal(err)
  }
  ```

  ```python Python theme={null}
  step = thread.step("order_received")

  # Execute business logic
  order = await receive_order()

  # Record success with context
  await step.add_context({"orderId": order.id, "total": order.total}).success()
  ```
</CodeGroup>

### Step Methods

| Method              | Description                                 |
| ------------------- | ------------------------------------------- |
| `.success()`        | Mark step as successful                     |
| `.success(data)`    | Success with additional data                |
| `.failed(data)`     | Mark step as failed with error data         |
| `.addContext(data)` | Add business context (analytics, debugging) |

<Warning>
  Non-contract threads remain "active" indefinitely unless explicitly ended. [View Closing a Thread](/core-concepts/ending-threads)
</Warning>

### Idempotency

Threadify automatically handles idempotency - recording the same step multiple times only stores the first execution.

### Next Steps

<CardGroup cols={2}>
  <Card title="Handling Failures" icon="triangle-exclamation" href="/core-concepts/handling-failures">
    Learn how to handle and record failures
  </Card>

  <Card title="Closing Threads" icon="circle-check" href="/core-concepts/ending-threads">
    Complete or cancel threads manually
  </Card>

  <Card title="External References" icon="link" href="/core-concepts/external-references">
    Link to external systems like Stripe or Shopify
  </Card>
</CardGroup>
