# Create a flow: a trigger and steps (wait, email, tag, condition)

`POST /crm/flows` · Rivier · campaigns

Create a flow: a trigger and steps (wait, email, tag, condition).

- Scope: campaigns:write
- Graphql: Mutation.campaignsFlowCreate
- Sdk: campaigns.flow.create
- Cli: campaigns flow create

## Request body

`application/json`

```json
{
  "type": "object",
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160
    },
    "trigger": {
      "type": "object",
      "properties": {
        "event": {
          "type": "string",
          "pattern": "^[a-z_]+(\\.[a-z_]+){0,2}$",
          "maxLength": 60
        },
        "match": {
          "type": "object",
          "properties": {
            "tag": {
              "type": "string",
              "minLength": 1,
              "maxLength": 40,
              "pattern": "^[\\p{L}\\p{N}][\\p{L}\\p{N} _.:/-]*$"
            },
            "channel": {
              "type": "string",
              "enum": [
                "email",
                "sms",
                "whatsapp",
                "phone"
              ]
            },
            "purpose": {
              "type": "string",
              "pattern": "^[a-z][a-z0-9_]{0,29}$"
            },
            "formId": {
              "type": "string",
              "format": "uuid"
            },
            "source": {
              "type": "string",
              "maxLength": 80
            }
          },
          "additionalProperties": false
        },
        "inactiveDays": {
          "type": "integer",
          "minimum": 7,
          "maximum": 1095
        },
        "segmentId": {
          "type": "string",
          "format": "uuid"
        }
      },
      "required": [
        "event"
      ],
      "additionalProperties": false
    },
    "purpose": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]{0,29}$",
      "default": "marketing"
    },
    "kind": {
      "type": "string",
      "enum": [
        "marketing",
        "service"
      ],
      "default": "marketing"
    },
    "steps": {
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]{0,39}$"
              },
              "type": {
                "type": "string",
                "const": "wait"
              },
              "hours": {
                "type": "number",
                "minimum": 0,
                "maximum": 8760
              },
              "days": {
                "type": "number",
                "minimum": 0,
                "maximum": 365
              }
            },
            "required": [
              "id",
              "type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]{0,39}$"
              },
              "type": {
                "type": "string",
                "const": "email"
              },
              "subject": {
                "type": "string",
                "minLength": 1,
                "maxLength": 200
              },
              "preheader": {
                "type": "string",
                "maxLength": 200
              },
              "templateId": {
                "type": "string",
                "format": "uuid"
              },
              "html": {
                "type": "string",
                "maxLength": 500000
              },
              "text": {
                "type": "string",
                "maxLength": 100000
              }
            },
            "required": [
              "id",
              "type",
              "subject"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]{0,39}$"
              },
              "type": {
                "type": "string",
                "const": "sms"
              },
              "body": {
                "type": "string",
                "minLength": 1,
                "maxLength": 640
              }
            },
            "required": [
              "id",
              "type",
              "body"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]{0,39}$"
              },
              "type": {
                "type": "string",
                "const": "tag"
              },
              "add": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 40,
                  "pattern": "^[\\p{L}\\p{N}][\\p{L}\\p{N} _.:/-]*$"
                },
                "maxItems": 10
              },
              "remove": {
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1,
                  "maxLength": 40,
                  "pattern": "^[\\p{L}\\p{N}][\\p{L}\\p{N} _.:/-]*$"
                },
                "maxItems": 10
              }
            },
            "required": [
              "id",
              "type"
            ],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[a-z][a-z0-9_]{0,39}$"
              },
              "type": {
                "type": "string",
                "const": "condition"
              },
              "rules": {
                "anyOf": [
                  {
                    "type": "object",
                    "properties": {
                      "all": {
                        "type": "array",
                        "items": {},
                        "minItems": 1,
                        "maxItems": 50
                      }
                    },
                    "required": [
                      "all"
                    ],
                    "additionalProperties": false
                  },
                  {
                    "type": "object",
                    "properties": {
                      "any": {
                        "type": "array",
                        "items": {},
                        "minItems": 1,
                        "maxItems": 50
                      }
                    },
                    "required": [
                      "any"
                    ],
                    "additionalProperties": false
                  },
                  {
                    "type": "object",
                    "properties": {
                      "not": {}
                    },
                    "required": [
                      "not"
                    ],
                    "additionalProperties": false
                  },
                  {
                    "type": "object",
                    "properties": {
                      "f": {
                        "type": "string",
                        "pattern": "^(email|name|phone|source|lifecycle|country|language|owner|company|tag|created_at|updated_at|last_activity_at|orders_count|orders_total|open_deals|segment|custom\\.[a-z][a-z0-9_]{0,39}|consent\\.(email|sms|whatsapp|phone)(\\.[a-z][a-z0-9_]{0,29})?|suppressed\\.(email|sms|whatsapp|phone))$"
                      },
                      "op": {
                        "type": "string",
                        "enum": [
                          "eq",
                          "ne",
                          "contains",
                          "starts",
                          "in",
                          "nin",
                          "gt",
                          "gte",
                          "lt",
                          "lte",
                          "exists",
                          "within_days",
                          "older_than_days"
                        ]
                      },
                      "v": {}
                    },
                    "required": [
                      "f",
                      "op"
                    ],
                    "additionalProperties": false
                  }
                ]
              },
              "onFalse": {
                "type": "string",
                "enum": [
                  "exit",
                  "skip"
                ],
                "default": "exit"
              }
            },
            "required": [
              "id",
              "type",
              "rules"
            ],
            "additionalProperties": false
          }
        ]
      },
      "minItems": 1,
      "maxItems": 30
    },
    "reenrollAfterDays": {
      "anyOf": [
        {
          "type": "integer",
          "minimum": 1,
          "maximum": 3650
        },
        {
          "type": "null"
        }
      ]
    },
    "exitOn": {
      "type": "array",
      "items": {
        "type": "string",
        "pattern": "^[a-z_]+(\\.[a-z_]+){0,2}$"
      },
      "maxItems": 10
    }
  },
  "required": [
    "name",
    "trigger",
    "steps"
  ],
  "additionalProperties": false
}
```

## Responses

- `201`: Success
- `400`: The request was malformed
- `401`: No valid credential
- `403`: The credential lacks the scope, origin or address
- `429`: Over the key's rate limit; see Retry-After

## Example

```sh
curl -X POST 'https://api.rivier.ai/crm/flows' \
  -H 'Authorization: Bearer $KEY' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "string",
  "trigger": {
    "event": "string"
  },
  "steps": [
    {
      "id": "string",
      "type": "wait"
    }
  ]
}'
```
