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

# Workflows

> Automate actions based on triggers using Formal workflows

## Overview

Workflows enable automation of actions based on triggers. A workflow consists of:

* **Trigger**: What starts the workflow (e.g., an API request, a new log event, or a new autodiscovered resource)
* **Actions**: What the workflow does when triggered (e.g., send a Slack message, call the Formal API)

Workflows are defined in YAML and can be managed via Terraform or the Formal API.

## Triggers vs Actions

| Component | Purpose | When it runs |
| - | - | - |
| **Trigger** | Defines what starts the workflow | Once, when the triggering event occurs |
| **Actions** | Defines what the workflow does | After the trigger fires, in sequence based on `depends_on` |

A workflow has exactly **one trigger** and **one or more actions**. Actions can be chained together using `depends_on` and conditionally executed using `if`.

## Workflow Structure

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "my-trigger"           # Unique name for this trigger
  type: "api-request"          # Trigger type
  args:                        # Type-specific arguments
    allow: "user.email == 'admin@example.com'"

actions:
  - name: "first-action"       # Unique name for this action
    type: "send-slack-message" # Action type
    args:                      # Type-specific arguments
      text: "Workflow triggered!"
      recipient_email: "ops@example.com"

  - name: "second-action"
    type: "formal-app-command"
    depends_on:                # Wait for these actions to complete
      - "first-action"
    if: "actions.first_action.message_sent == true"  # Conditional execution
    args:
      # ...
```

## `depends_on` - Action Chaining

The `depends_on` field controls the execution order of actions. Actions with empty `depends_on` run immediately after the trigger. Subsequent actions run after their dependencies complete.

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  # Runs immediately after trigger
  - name: "step-1"
    type: "send-slack-message"
    args:
      text: "Starting workflow..."
      recipient_email: "ops@example.com"

  # Runs after step-1 completes
  - name: "step-2"
    type: "formal-app-command"
    depends_on:
      - "step-1"
    args:
      # ...

  # Runs after step-2 completes
  - name: "step-3"
    type: "send-slack-message"
    depends_on:
      - "step-2"
    args:
      text: "Workflow complete!"
      recipient_email: "ops@example.com"
```

## Referencing Previous Trigger and Action Outputs

Actions can reference previous triggers and actions outputs via the following syntax in [CEL expressions](https://cel.dev/):

* `trigger.<output_field>`: reference any output field of the trigger
* `actions.<action_name>.<output_field>`: reference any output field of an action by its name

Some arguments, such as `if` or the `api-request` `allow` argument, take CEL expressions by default. For arguments that take strings
instead, embed CEL expressions via the `${{}}` syntax.

## Secrets

Store sensitive workflow data as secrets instead of writing them in the workflow YAML.
Secrets are scoped to a single workflow.

To add a secret, open the workflow in the editor and click **Secrets**. Click **Create secret**, then enter a name and value.

![Workflow editor header with the Secrets button highlighted](https://mintcdn.com/formal/co3PshRaROUuP8qX/assets/images/workflow-secrets-button.png?fit=max\&auto=format\&n=co3PshRaROUuP8qX\&q=85\&s=617f80232fef71406b8cef36a5901593)

![Create workflow secret dialog with Name and Value fields](https://mintcdn.com/formal/co3PshRaROUuP8qX/assets/images/workflow-secrets-create-dialog.png?fit=max\&auto=format\&n=co3PshRaROUuP8qX\&q=85\&s=4102621f1109588d9239a8b75790e626)

Reference a secret in action args with `secrets.<name>` inside `${{}}`:

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
headers:
  Authorization: "Bearer ${{ secrets.soar_token }}"
```

Secret names must be valid CEL identifiers, such as `soar_token`. Values are never returned by the API or shown in the Control Plane.
`if` conditions can't read secrets.

> [!NOTE]
>
> If an action's output or error contains a secret value, Formal replaces it with `***` before storing the step result.
> Formal only masks the exact value, not encoded or transformed forms such as base64.

## `if` - Conditional Execution

The `if` field is a CEL expression that determines whether a subsequent action should execute based on the previous actions or trigger.

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "notify-on-success"
    type: "send-slack-message"
    depends_on:
      - "create-resource"
    if: "actions.create_resource.status_code == 200"
    args:
      text: "Resource created successfully!"
      recipient_email: "admin@example.com"

  - name: "notify-on-failure"
    type: "send-slack-message"
    depends_on:
      - "create-resource"
    if: "actions.create_resource.status_code != 200"
    args:
      text: "Failed to create resource: ${{ actions.create_resource.body.message }}"
      recipient_email: "admin@example.com"
```

## Trigger Types

### `api-request`

Triggered when a user calls the `CreateWorkflowTrigger` API endpoint. Useful for manually triggered workflows or integrations.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `allow` | string | No | CEL expression to authorize the request. If omitted, all authenticated users can trigger. |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `user` | object | The user who triggered the workflow |
| `payload` | object | Custom payload passed in the API request |

When the caller authenticates with an [OIDC integration](https://docs.formal.ai/docs/guides/integrations/oidc.md), `user.oidc` is also available:

| Output | Type | Description |
| - | - | - |
| `user.oidc.integration_id` | string | OIDC integration ID |
| `user.oidc.issuer` | string | Token issuer |
| `user.oidc.claims.*` | any | Verified IdP JWT claims (shape varies by IdP) |
| `user.oidc.expires_at` | string | Upstream JWT expiry |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "manual-trigger"
  type: "api-request"
  args:
    allow: "user.groups.exists(g, g == 'groupname')"
```

OIDC callers can reference verified claims directly instead of re-embedding them in `payload`:

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "background_agent_access_request"
  type: "api-request"
  args:
    allow: "has(user.oidc)"
actions:
  - name: "ask_end_user"
    type: "ask-in-chat"
    args:
      integration: "slack"
      recipient_email: "${{ trigger.user.oidc.claims.owner_email }}"
      message: "Approve access for `${{ trigger.user.oidc.claims.owner_email }}`?"
```

**Triggering via API:**

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
curl -X POST -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"workflow_id": "workflow_abc123", "payload": {"key": "value"}}' \
  https://api.formal.ai/core.v1.WorkflowService/CreateWorkflowTrigger
```

***

### `new-log`

Triggered when a new log event matches a specified condition. Useful for reacting to specific database queries, policy violations, or access patterns.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `if` | string | Yes | CEL expression to filter logs. Only logs matching this condition trigger the workflow. |
| `unique_by` | object | No | Only trigger for logs whose key values are new. See [Unique logs](#unique-logs). |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `log` | object | The full log event that matched the condition |
| `unique` | object | With `unique_by`, the key values that triggered this run, by key name |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "blocked-query-alert"
  type: "new-log"
  args:
    if: "log.event_type == 'session-login-failed' && log.triggered_policies.exists(p, p.status == 'active' && p.type == 'block')"
```

#### Unique logs

`unique_by` triggers the workflow only when a matching log's key values are new, for example the first time a user loads a skill, or at most once an hour per user and resource.

| Field | Type | Required | Description |
| - | - | - | - |
| `keys` | map | Yes | Up to five key names (lowercase letters, digits, and underscores) mapped to CEL expressions over `log`. An expression that returns a list triggers one run per new element. Without `keys`, the other fields are ignored and every matching log triggers. |
| `reset_on` | string | No | `trigger` (default) triggers again once `window` has passed since the key last triggered, however often it appears in between. `sighting` triggers again only after the key has gone unseen for the whole `window`. |
| `window` | string | No | A duration such as `1h` or `30d`, from `1m` to `3650d`. Omit it to trigger once per key, ever. |

A key that is missing from a log, null, or an empty list counts as null, so logs missing the same key share it. To ignore such logs instead, check for the field in `if`, as the example below does. To give a missing key its own value, default it in the key expression, for example `has(log.user.username) ? log.user.username : "unknown"`.

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "first-skill-use"
  type: "new-log"
  args:
    if: "has(log.llm) && has(log.llm.loaded_skills) && size(log.llm.loaded_skills) > 0"
    unique_by:
      keys:
        user: log.user.username
        skill: log.llm.loaded_skills.map(s, s.name)
      reset_on: sighting
actions:
  - name: "notify"
    type: "send-slack-message"
    args:
      text: "${{ trigger.unique.user }} used the ${{ trigger.unique.skill }} skill for the first time."
      recipient_channel: "ai-governance"
```

> [!NOTE]
>
> Keys are tracked from the moment the workflow is saved; logs from before then are not considered. Changing `keys` forgets every key seen so far, while changing `reset_on` or `window` keeps them. Encrypted key values differ on every log, so they are grouped as `<encrypted>`; key on unencrypted fields. A log whose keys expand to more than 100 combinations triggers one run without deduplication. Each workflow keeps at most one million keys, forgetting the least recently seen first.

***

### `new-autodiscovered-resource`

Triggered when a new resource is autodiscovered via cloud integration (AWS, GCP, etc.). Useful for automatically onboarding new databases.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `if` | CEL expression | No | Condition over `resource`, such as `resource.technology` or `resource.provider`. `resource.tags` is not available here. The workflow runs only for resources where it is true. Omit it to run for every resource. |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `resource` | object | The autodiscovered resource object |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "new-resource-discovered"
  type: "new-autodiscovered-resource"
  args:
    if: "resource.technology == 'postgres'"
```

***

### `updated-autodiscovered-resource`

Triggered when an existing autodiscovered resource is updated.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `if` | CEL expression | No | Condition over `resource`, such as `resource.technology` or `resource.provider`. `resource.tags` is not available here. The workflow runs only for resources where it is true. Omit it to run for every resource. |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `resource` | object | The updated autodiscovered resource object |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "resource-updated"
  type: "updated-autodiscovered-resource"
  args:
    if: "resource.technology == 'postgres'"
```

***

### `deleted-autodiscovered-resource`

Triggered when an autodiscovered resource is deleted because it no longer exists in the cloud provider.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `if` | CEL expression | No | Condition over `resource`, such as `resource.technology` or `resource.provider`. `resource.tags` is not available here. The workflow runs only for resources where it is true. Omit it to run for every resource. |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `resource` | object | The deleted autodiscovered resource object |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "resource-removed"
  type: "deleted-autodiscovered-resource"
  args:
    if: "resource.technology == 'postgres'"
```

***

### `cron-schedule`

Triggered on a recurring schedule defined by a standard 5-field cron expression (`minute hour day-of-month month day-of-week`). All times are evaluated in **UTC**. Useful for periodic checks, scheduled reports, and recurring maintenance tasks.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `schedule` | string | Yes | 5-field cron expression (e.g., `"0 9 * * 1-5"` for weekdays at 9am UTC) |
| `recovery` | string | No | Controls behavior when scheduled triggers are missed during a Formal outage. One of `none` (default), `latest`, or `all`. See below. |

**Outputs (available in actions):**

| Output | Type | Description |
| - | - | - |
| `trigger.scheduled_time` | string | The time (UTC, RFC 3339) the cron was scheduled to run, always truncated to the minute. |
| `trigger.execution_time` | string | The actual time (UTC, RFC 3339) the trigger fired. Matches `scheduled_time` under normal operation. |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "workday-check"
  type: "cron-schedule"
  args:
    schedule: "0 8,17 * * 1-5"   # 8am and 5pm UTC on weekdays

actions:
  - name: "notify"
    type: "send-slack-message"
    args:
      text: "Workday check ran at ${{ trigger.execution_time }} (scheduled for ${{ trigger.scheduled_time }})"
      recipient_channel: "ops"
```

**Recovery policy (optional):** Under normal operation, cron triggers fire reliably every minute and `recovery` has no effect. It only matters in the unlikely event of a Formal platform outage where the workflow engine is temporarily unable to process scheduled triggers. When the platform recovers, any missed ticks are handled according to the `recovery` policy. If omitted, defaults to `none`. Missed triggers are capped at a 24-hour lookback window.

| Policy | Behavior |
| - | - |
| `none` | Skips all missed triggers and resumes from the next future tick on the schedule. |
| `latest` | Fires a single trigger for the most recent missed tick, then resumes normally. The `scheduled_time` output reflects the missed tick, while `execution_time` reflects the actual recovery time. |
| `all` | Fires every missed tick (up to 24 hours) in chronological order, each with its own `scheduled_time`, then resumes normally. |

> [!NOTE]
>
> Cron schedules are always evaluated in UTC. To schedule at a local time, convert manually (e.g., 9am US Eastern = `"0 14 * * *"` during EDT or `"0 13 * * *"` during EST).

***

### `form-submission`

Triggered when a user submits a [Formal form](https://docs.formal.ai/docs/guides/configuration/forms.md) via Slack. Useful for approval workflows, access requests, and other structured data collection scenarios.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `id` | string | Yes | The ID of the form to listen for submissions on |

**Outputs (available in actions via `trigger.form_submission`):**

| Output | Type | Description |
| - | - | - |
| `form_id` | string | The ID of the submitted form |
| `form_name` | string | The name of the submitted form |
| `submitter_id` | string | Formal user ID of the submitter; empty if a Slack submitter has no matching Formal user |
| `submitter_email` | string | Email of the user who submitted the form |
| `submission` | object | Map of field IDs to submitted values |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
trigger:
  name: "access-request"
  type: "form-submission"
  args:
    id: "form_abc123"
```

**Referencing form values in actions:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "notify-approver"
    type: "send-slack-message"
    args:
      text: "${{ trigger.form_submission.submitter_email }} submitted a request: ${{ trigger.form_submission.submission.field_reason }}"
      recipient_channel: "approvals"
```

> [!NOTE]
>
> See the [Forms documentation](https://docs.formal.ai/docs/guides/configuration/forms.md) for details on creating forms and the full Slack integration.

## Action Types

### `send-slack-message`

Sends a direct message to a Slack user or channel. Requires a Slack integration to be configured.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `text` | string | Yes | Message text to send (supports Slack markdown) |
| `recipient_email` | string | One of | Email address of the Slack user (mutually exclusive with `recipient_channel`) |
| `recipient_channel` | string | One of | Slack channel name without `#`, channel ID such as `C0123456789`, or DM channel ID (`D…`) (mutually exclusive with `recipient_email`) |
| `thread_ts` | string | No | Timestamp of a message to reply to in its thread, usually `${{ actions.<name>.message_ts }}`. Use with `recipient_channel` set to that message's `channel_id`. |

**Outputs:**

| Output | Type | Description |
| - | - | - |
| `recipient_email` | string | Email of the recipient (if email was used) |
| `recipient_channel` | string | Name of the channel (if channel was used) |
| `channel_id` | string | ID of the channel the message was posted to |
| `message_ts` | string | Timestamp of the posted message, usable as `thread_ts` |
| `message_sent` | boolean | Whether the message was successfully sent |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  # Send to a user by email
  - name: "notify-admin"
    type: "send-slack-message"
    args:
      text: "New resource discovered: ${{ trigger.resource.name }}"
      recipient_email: "admin@example.com"

  # Send to a channel
  - name: "notify_channel"
    type: "send-slack-message"
    args:
      text: "New resource discovered: ${{ trigger.resource.name }}"
      recipient_channel: "security-alerts"

  # Reply in the thread of a previous message
  - name: "notify_thread"
    type: "send-slack-message"
    depends_on: ["notify_channel"]
    args:
      text: "Request approved"
      recipient_channel: "${{ actions.notify_channel.channel_id }}"
      thread_ts: "${{ actions.notify_channel.message_ts }}"
```

***

### `ask-in-chat`

Sends an interactive message with Yes/No buttons. The workflow pauses until the user responds. With `denial_reason`, choosing No opens a form where the approver gives a reason for the denial.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `message` | string | Yes | Message text to display |
| `integration` | string | Yes | Must be `"slack"` |
| `recipient_email` | string | One of | Email of the Slack user, or up to 25 comma-separated emails (mutually exclusive with `recipient_channel`) |
| `recipient_channel` | string | One of | Slack channel name without `#`, or channel ID such as `C0123456789` (mutually exclusive with `recipient_email`) |
| `denial_reason` | string | No | `none` (default), `optional` or `required`. `none` records No without asking for a reason; `optional` and `required` open a form for one |

With several emails, each recipient gets the message as a direct message. The first answer decides the step, and every other copy is updated to show who answered.

**Outputs:**

| Output | Type | Description |
| - | - | - |
| `response` | string | `yes` or `no` |
| `responder_email` | string | Email of the Slack user who answered |
| `comment` | string | Reason the approver gave when answering No; empty otherwise |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "approve_access"
    type: "ask-in-chat"
    args:
      message: "Approve access request from ${{ trigger.user.email }}?"
      integration: "slack"
      recipient_channel: "security-approvals"
      denial_reason: "required"

  - name: "notify_denied"
    type: "send-slack-message"
    depends_on:
      - "approve_access"
    if: "actions.approve_access.response == 'no'"
    args:
      recipient_email: "${{ trigger.user.email }}"
      text: "Your access request was denied by ${{ actions.approve_access.responder_email }}: ${{ actions.approve_access.comment }}"
```

To ask several approvers directly instead of a channel:

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "approve_access"
    type: "ask-in-chat"
    args:
      message: "Approve access request from ${{ trigger.user.email }}?"
      integration: "slack"
      recipient_email: "alice@example.com, bob@example.com"
```

***

### `formal-app-command`

Calls a [Formal API endpoint](https://docs.formal.ai/docs/api/introduction.md) using a machine user's credentials.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `app` | string | Yes | Service name (e.g., `Resource`, `User`, `Policy`) |
| `command.name` | string | Yes | Method name without prefix (e.g., `Resource` for `CreateResource`) |
| `command.type` | string | Yes | Method prefix (e.g., `Create`, `Update`, `Delete`, `Get`) |
| `input` | object | Yes | Request payload for the API call |
| `machine_user_id` | string | Yes | ID of the machine user to authenticate as |

**Outputs:**

| Output | Type | Description |
| - | - | - |
| `status_code` | int | HTTP status code of the response |
| `body` | object | Parsed JSON response body |

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "create-resource"
    type: "formal-app-command"
    args:
      app: "Resource"
      command:
        name: "Resource"
        type: "Create"
      input:
        name: "${{ trigger.resource.name }}"
        technology: "${{ trigger.resource.technology }}"
        hostname: "${{ trigger.resource.hostname }}"
        port: "${{ trigger.resource.port }}"
      machine_user_id: "user_abc123"
```

***

### `http-request`

Sends an HTTPS request to any external API or webhook. Later actions can read the response.

**Args:**

| Arg | Type | Required | Description |
| - | - | - | - |
| `method` | string | Yes | `GET`, `POST`, `PUT`, `PATCH` or `DELETE` |
| `url` | string | Yes | HTTPS URL to send a request to |
| `headers` | object | No | Request headers as string key-value pairs |
| `body` | string | No | Raw request body (mutually exclusive with `json`) |
| `json` | object | No | Request body encoded as JSON. Sets `Content-Type: application/json` by default (mutually exclusive with `body`) |

**Outputs:**

| Output | Type | Description |
| - | - | - |
| `status_code` | int | HTTP status code of the response |
| `headers` | object | Response headers |
| `body` | string | Raw response body |
| `json` | object | Parsed response body, set only when the response is JSON |

A non-2xx response doesn't fail the action. Check `status_code` in a later action's `if`.

**Example:**

```yaml theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
actions:
  - name: "notify_webhook"
    type: "http-request"
    args:
      method: "POST"
      url: "https://hooks.example.com/formal"
      headers:
        Authorization: "Bearer ${{ secrets.webhook_token }}"
      json:
        event: "resource_discovered"
        resource: "${{ trigger.resource.name }}"
```

## Terraform Examples

Inside a Terraform string, write `$${{` so that Terraform passes `${{` through to the workflow instead of treating it as interpolation.

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_workflow" "autopromote_prod_resources" {
  name = "Autopromote Production Resources"

  code = <<-YAML
    trigger:
      name: "resource_discovered"
      type: "new-autodiscovered-resource"

    actions:
      - name: "promote_resource"
        type: "formal-app-command"
        depends_on: ["resource_discovered"]
        if: trigger.resource.tags.Environment == "Production"
        args:
          app: "Resource"
          command:
            name: "Resource"
            type: "Create"
          input:
            name: "$${{ trigger.resource.name }}"
            technology: "$${{ trigger.resource.technology }}"
            hostname: "$${{ trigger.resource.hostname }}"
            port: "$${{ trigger.resource.port }}"
          machine_user_id: "user_abc123"
  YAML
}
```

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_workflow" "request_policy_suspension_from_api_request" {
  name = "Request oneoff SQL queries in Slack from API requests"

  code = <<-YAML
    trigger:
      type: api-request
      name: request_query
      args:
        allow: true # write a condition on who can perform approval requests if applicable
    actions:
    - type: ask-in-chat
      name: ask_approver
      args:
        recipient_email: <recipient email here>
        message: 'Do you want to allow `$${{ trigger.payload.query }}` from $${{ trigger.user.email }}?'
        integration: slack
    - type: formal-app-command
      name: suspend_policy
      depends_on: [ask_approver]
      if: actions.ask_approver.response == "yes"
      args:
        app: Policies
        command:
          name: PolicySuspension
          type: Create
        machine_user_id: "user_abc123"
        input:
          policy_id: "policy_1234"
          identity_type: user
          identity_id: $${{ trigger.user.id }}
          input_condition: $${{ trigger.payload.query }}
          oneoff: false
          expiration_minutes: 60
  YAML
}
```

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_workflow" "request_policy_suspension_from_form" {
  name = "Request oneoff SQL queries in Slack from API requests"
  # assume we have a form `form_abc123` with two fields:
  # one field with id `field_resource` that should be the resource name
  # one field with id `field_access_un` that should store the timestamp that the requester is requesting access until
  code = <<-YAML
    trigger:
      type: form-submission
      name: form_submission
      args:
        id: form_abc123
    actions:
      - type: ask-in-chat
        name: ask_approval
        args:
          message:  'Approve access request to "$${{trigger.form_submission.submission.field_resource }}" from $${{ trigger.form_submission.submitter_email }} until <!date^$${{ int(timestamp(trigger.form_submission.submission.field_access_un)) }}^{date_short_pretty} at {time}|invalid_time>?'
          recipient_channel: some-slack-channel
          integration: slack
      - type: formal-app-command
        name: get_user_id
        depends_on: [ask_approval]
        args:
          app: User
          command:
            name: Users
            type: List
          machine_user_id: user_abc123
          input:
            limit: 1
            filter:
              field:
                key: "db_username"
                operator: "contains"
                value:
                  value: $${{ trigger.form_submission.submitter_email }}
                  "@type": "type.googleapis.com/google.protobuf.StringValue"
      - type: formal-app-command
        name: suspend_policy
        depends_on: [get_user_id]
        if: actions.ask_approval.response == "yes" && (int(timestamp(trigger.form_submission.submission.field_access_un)) > int(now) + 60) && actions.get_user_id.status_code == 200 && size(actions.get_user_id.body.users) > 0
        args:
          app: Policies
          command:
            name: PolicySuspension
            type: Create
          machine_user_id: user_abc123
          input:
            policy_id: policy_abc123
            identity_type: user
            identity_id: $${{ actions.get_user_id.body.users[0].id }}
            input_condition: 'input.resource.name == "$${{ trigger.form_submission.submission.field_resource }}"'
            oneoff: false
            expiration_minutes: $${{(int(timestamp(trigger.form_submission.submission.field_access_un)) - int(now))/60}}
  YAML
}
```

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_workflow" "daily_compliance_report" {
  name = "Daily Compliance Report"

  code = <<-YAML
    trigger:
      name: "daily-report"
      type: "cron-schedule"
      args:
        schedule: "0 9 * * 1-5"   # weekdays at 9am UTC
        # recovery: latest   # optional, defaults to none

    actions:
      - name: "send-report"
        type: "send-slack-message"
        args:
          text: "Daily compliance report (scheduled: $${{ trigger.scheduled_time }}, ran: $${{ trigger.execution_time }})"
          recipient_channel: "compliance"
  YAML
}
```

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
resource "formal_workflow" "blocked_query_alert" {
  name = "blocked-query-alert"

  code = <<-YAML
    trigger:
      name: "blocked_query"
      type: "new-log"
      args:
        if: "log.event_type == 'request' && log.triggered_policies.exists(p, p.status == 'active' && p.type == 'block')"

    actions:
      - name: "alert_security"
        type: "send-slack-message"
        args:
          text: |
            Query $${{ log.request.query.normalized }} blocked by policy!
            User: $${{ trigger.log.user.email }}
            Resource: $${{ trigger.log.resource.name }}
          recipient_email: "security@example.com"
  YAML
}
```

```hcl theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
variable "slack_webhook_url" {
  type      = string
  sensitive = true
  ephemeral = true
}

resource "formal_workflow" "notify_new_resource" {
  name = "Notify Slack about new resources"

  code = <<-YAML
    trigger:
      name: "resource_discovered"
      type: "new-autodiscovered-resource"

    actions:
      - name: "notify_slack"
        type: "http-request"
        args:
          method: "POST"
          url: "$${{ secrets.slack_webhook_url }}"
          json:
            text: "New resource discovered: $${{ trigger.resource.name }}"
  YAML
}

resource "formal_workflow_secret" "slack_webhook_url" {
  workflow_id      = formal_workflow.notify_new_resource.id
  name             = "slack_webhook_url"
  value_wo         = var.slack_webhook_url
  value_wo_version = 1
}
```

## Next Steps

> [card] [Policies](https://docs.formal.ai/docs/guides/policies/introduction.md)
>
> Learn about Formal policies for access control

> [card] [Terraform Provider](https://docs.formal.ai/docs/guides/configuration/terraform.md)
>
> Full Terraform provider documentation


## Related topics

- [Create workflow](https://docs.formal.ai/api-reference/corev1workflowservice/create-workflow.md)
- [Delete workflow](https://docs.formal.ai/api-reference/corev1workflowservice/delete-workflow.md)
- [Get workflow](https://docs.formal.ai/api-reference/corev1workflowservice/get-workflow.md)
- [List workflows](https://docs.formal.ai/api-reference/corev1workflowservice/list-workflows.md)
- [Update workflow](https://docs.formal.ai/api-reference/corev1workflowservice/update-workflow.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.