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

# Create Workflow

> Create a workflow that runs custom code on events.

Create a new workflow that executes custom JavaScript/TypeScript code in response to events. Workflows run in Cloudflare Workers with access to room data, stores, and file storage.

## Endpoint

```
POST /v1/accounts/:accountId/workflows
```

## Authentication

**Bearer token** required.

## Request Body

<ParamField body="name" type="string" required>
  A descriptive name for the workflow.
</ParamField>

<ParamField body="code" type="string" required>
  JavaScript or TypeScript source code to execute when triggered.
</ParamField>

<ParamField body="events" type="string[]" required>
  List of events that trigger this workflow. At least one event is required. Same event types as [webhooks](/api-reference/webhooks/create-webhook#available-events).
</ParamField>

<ParamField body="roomId" type="string">
  Scope the workflow to a specific room. If omitted, the workflow triggers for events in all rooms.
</ParamField>

### Available Events

| Event                | Description                  |
| -------------------- | ---------------------------- |
| `participant.joined` | A user came online in a room |
| `participant.left`   | A user went offline          |
| `session.started`    | A new session started        |
| `session.closed`     | A session ended              |
| `user.created`       | A new user was added         |
| `user.updated`       | A user's fields changed      |
| `user.deleted`       | A user was removed           |

### Worker Bindings

Your workflow code has access to these bindings:

| Binding | Type           | Description                           |
| ------- | -------------- | ------------------------------------- |
| `store` | `StoreBinding` | Read-only access to KV stores         |
| `room`  | `RoomBinding`  | Read-only room and user metadata      |
| `files` | `FilesBinding` | Scoped R2 file operations             |
| `event` | `EventBinding` | The event that triggered the workflow |

## Response

<ResponseField name="workflow" type="WorkflowRegistration">
  The created workflow. See [`WorkflowRegistration`](/types/workflows#workflowregistration).
</ResponseField>

## Example

<CodeGroup>
  ```bash curl theme={null}
  curl -X POST https://api.collab-kit.com/v1/accounts/${ACCOUNT_ID}/workflows \
    -H "Authorization: Bearer ${TOKEN}" \
    -H "Content-Type: application/json" \
    -d '{
      "name": "Welcome message",
      "code": "export default async function(event, { store, room }) {\n  console.log(`User ${event.data.userId} joined room ${event.data.roomId}`);\n  return { success: true };\n}",
      "events": ["participant.joined"]
    }'
  ```

  ```typescript JavaScript theme={null}
  const res = await fetch(`https://api.collab-kit.com/v1/accounts/${accountId}/workflows`, {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      name: 'Welcome message',
      code: `export default async function(event, { store, room }) {
    console.log(\`User \${event.data.userId} joined\`);
    return { success: true };
  }`,
      events: ['participant.joined'],
    }),
  });
  const { data } = await res.json();
  ```
</CodeGroup>

```json Response (201) theme={null}
{
  "type": "response",
  "success": true,
  "description": "Workflow created",
  "data": {
    "workflow": {
      "id": "wf_abc123",
      "organization_id": "a1b2c3d4-...",
      "name": "Welcome message",
      "code": "export default async function(event, { store, room }) { ... }",
      "events": ["participant.joined"],
      "room_id": null,
      "enabled": true,
      "created_at": "2026-05-29T10:00:00.000Z",
      "updated_at": "2026-05-29T10:00:00.000Z"
    }
  },
  "error": null
}
```
