{
  "openapi": "3.0.3",
  "info": {
    "title": "Workflows REST API",
    "version": "1.0.0",
    "description": "CRUD for saved workflows (prompt templates). All endpoints require a task API key (`Authorization: Bearer tsk_...`): reads need the `workflows:read` capability, writes need `workflows:write`. The organization scope is inferred from the key and cannot be overridden. Within the organization the key acts as its owning principal: it can only see and change workflows that principal holds the matching `workflow:*` grant on."
  },
  "servers": [
    {
      "url": "https://{deployment}/api/v1/workflows",
      "description": "Your Context deployment",
      "variables": {
        "deployment": {
          "default": "context.example.com",
          "description": "The host your organization signs in to"
        }
      }
    }
  ],
  "security": [
    {
      "TaskApiKey": []
    }
  ],
  "tags": [
    {
      "name": "Workflows",
      "description": "List, read, create, update and delete the saved workflows (prompt templates) in the key's organization, and list the tags used in a workspace. A key sees and changes only the workflows its owning principal holds the matching `workflow:*` grant on."
    }
  ],
  "components": {
    "securitySchemes": {
      "TaskApiKey": {
        "type": "http",
        "scheme": "bearer",
        "bearerFormat": "tsk_<prefix>_<secret>",
        "description": "Task API key carrying the capability named on each operation. Create keys from Settings → API → Task keys."
      }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "string",
            "description": "Human-readable error message"
          },
          "code": {
            "type": "string",
            "description": "Optional machine-readable error code"
          },
          "details": {
            "description": "Optional per-field validation details"
          }
        }
      }
    }
  },
  "paths": {
    "/": {
      "get": {
        "tags": [
          "Workflows"
        ],
        "summary": "List workflows",
        "description": "Workflows the key's principal can read, org-wide, newest-updated first. Rows the principal lacks `workflow:read` on are omitted. The prompt body is not included — fetch a single workflow for `content`.",
        "operationId": "listWorkflows",
        "parameters": [
          {
            "in": "query",
            "name": "workspaceId",
            "required": false,
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Only workflows in this workspace"
            }
          },
          {
            "in": "query",
            "name": "tags",
            "required": false,
            "schema": {
              "anyOf": [
                {
                  "type": "string",
                  "minLength": 1
                },
                {
                  "type": "array",
                  "items": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              ],
              "description": "Only workflows carrying ANY of these tags (repeat the parameter for several)"
            }
          },
          {
            "in": "query",
            "name": "search",
            "required": false,
            "schema": {
              "type": "string",
              "minLength": 1,
              "maxLength": 200,
              "description": "Name/description search, applied before paging so a match can never be paged out"
            }
          },
          {
            "in": "query",
            "name": "cursor",
            "required": false,
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Opaque cursor from the previous page's nextCursor"
            }
          },
          {
            "in": "query",
            "name": "limit",
            "required": false,
            "schema": {
              "type": "integer",
              "exclusiveMinimum": true,
              "minimum": 0,
              "maximum": 200,
              "description": "Page size (default 50, max 200)"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "id": {
                            "type": "string",
                            "description": "Unique workflow identifier"
                          },
                          "workspaceId": {
                            "type": "string",
                            "description": "Workspace this workflow belongs to"
                          },
                          "name": {
                            "type": "string",
                            "description": "Workflow display name"
                          },
                          "description": {
                            "type": "string",
                            "nullable": true,
                            "description": "Optional description"
                          },
                          "tags": {
                            "type": "array",
                            "items": {
                              "type": "string"
                            },
                            "description": "Tags for organizing workflows"
                          },
                          "createdAt": {
                            "type": "string",
                            "format": "date-time",
                            "description": "Creation time (ISO 8601)"
                          },
                          "updatedAt": {
                            "type": "string",
                            "format": "date-time",
                            "description": "Last modification time (ISO 8601); stamped on every write"
                          }
                        },
                        "required": [
                          "id",
                          "workspaceId",
                          "name",
                          "description",
                          "tags",
                          "createdAt",
                          "updatedAt"
                        ],
                        "additionalProperties": false,
                        "description": "Workflow metadata without the prompt body"
                      }
                    },
                    "nextCursor": {
                      "type": "string",
                      "nullable": true,
                      "description": "Pass as `cursor` to fetch the next page; null on the last page"
                    }
                  },
                  "required": [
                    "items",
                    "nextCursor"
                  ],
                  "additionalProperties": false,
                  "description": "One page of workflows the key's principal can read"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:read` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "post": {
        "tags": [
          "Workflows"
        ],
        "summary": "Create a workflow",
        "description": "Creates a workflow in the given workspace and grants the key's user the `workflow_owner` role on it. Requires `ws:write` on the workspace and a user-principal key (service-account keys cannot create workflows). Any `{{connector_instance:...}}` mention in `content` must reference an instance the principal can use. Not idempotent: a retried request creates a second workflow.",
        "operationId": "createWorkflow",
        "parameters": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "workspaceId": {
                    "type": "string",
                    "description": "Workspace ID"
                  },
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 100,
                    "description": "Workflow name"
                  },
                  "content": {
                    "type": "string",
                    "minLength": 1,
                    "description": "Markdown prompt content"
                  },
                  "description": {
                    "type": "string",
                    "maxLength": 500,
                    "description": "Optional description"
                  },
                  "tags": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "maxLength": 50
                    },
                    "maxItems": 20,
                    "default": [],
                    "description": "Tags"
                  }
                },
                "required": [
                  "workspaceId",
                  "name",
                  "content"
                ],
                "additionalProperties": false,
                "description": "Input for creating a new workflow"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Resource created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "description": "Unique workflow identifier"
                    },
                    "workspaceId": {
                      "type": "string",
                      "description": "Workspace this workflow belongs to"
                    },
                    "name": {
                      "type": "string",
                      "description": "Workflow display name"
                    },
                    "description": {
                      "type": "string",
                      "nullable": true,
                      "description": "Optional description"
                    },
                    "tags": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Tags for organizing workflows"
                    },
                    "createdAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Creation time (ISO 8601)"
                    },
                    "updatedAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Last modification time (ISO 8601); stamped on every write"
                    },
                    "content": {
                      "type": "string",
                      "description": "Markdown prompt content"
                    }
                  },
                  "required": [
                    "id",
                    "workspaceId",
                    "name",
                    "description",
                    "tags",
                    "createdAt",
                    "updatedAt",
                    "content"
                  ],
                  "additionalProperties": false,
                  "description": "Saved prompt template"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters, or a malformed / invalid request body",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:write` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Request body must be sent as application/json",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/tags": {
      "get": {
        "tags": [
          "Workflows"
        ],
        "summary": "List workflow tags in a workspace",
        "description": "Sorted, de-duplicated tags across every workflow in the workspace. Requires read access to the workspace.",
        "operationId": "listWorkflowTags",
        "parameters": [
          {
            "in": "query",
            "name": "workspaceId",
            "required": true,
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Workspace whose tags to aggregate"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  },
                  "description": "Sorted, de-duplicated tags used in the workspace"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:read` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/{workflowId}": {
      "get": {
        "tags": [
          "Workflows"
        ],
        "summary": "Get a workflow",
        "description": "Full workflow including the markdown prompt `content`. Requires `workflow:read` on the workflow.",
        "operationId": "getWorkflow",
        "parameters": [
          {
            "in": "path",
            "name": "workflowId",
            "required": true,
            "description": "Workflow identifier",
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Workflow identifier"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "description": "Unique workflow identifier"
                    },
                    "workspaceId": {
                      "type": "string",
                      "description": "Workspace this workflow belongs to"
                    },
                    "name": {
                      "type": "string",
                      "description": "Workflow display name"
                    },
                    "description": {
                      "type": "string",
                      "nullable": true,
                      "description": "Optional description"
                    },
                    "tags": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Tags for organizing workflows"
                    },
                    "createdAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Creation time (ISO 8601)"
                    },
                    "updatedAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Last modification time (ISO 8601); stamped on every write"
                    },
                    "content": {
                      "type": "string",
                      "description": "Markdown prompt content"
                    }
                  },
                  "required": [
                    "id",
                    "workspaceId",
                    "name",
                    "description",
                    "tags",
                    "createdAt",
                    "updatedAt",
                    "content"
                  ],
                  "additionalProperties": false,
                  "description": "Saved prompt template"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:read` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "patch": {
        "tags": [
          "Workflows"
        ],
        "summary": "Update a workflow",
        "description": "Partial update: only the fields present in the body change; `description: null` clears it; `tags` replaces the whole list. Last write wins — there is no version precondition; compare `updatedAt` to detect a concurrent write after the fact. Requires `workflow:write` on the workflow.",
        "operationId": "updateWorkflow",
        "parameters": [
          {
            "in": "path",
            "name": "workflowId",
            "required": true,
            "description": "Workflow identifier",
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Workflow identifier"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "name": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 100,
                    "description": "New name"
                  },
                  "content": {
                    "type": "string",
                    "minLength": 1,
                    "description": "New content"
                  },
                  "description": {
                    "type": "string",
                    "maxLength": 500,
                    "nullable": true,
                    "description": "New description"
                  },
                  "tags": {
                    "type": "array",
                    "items": {
                      "type": "string",
                      "maxLength": 50
                    },
                    "maxItems": 20,
                    "description": "New tags"
                  }
                },
                "additionalProperties": false,
                "description": "Fields to change; omitted fields are left as-is"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "description": "Unique workflow identifier"
                    },
                    "workspaceId": {
                      "type": "string",
                      "description": "Workspace this workflow belongs to"
                    },
                    "name": {
                      "type": "string",
                      "description": "Workflow display name"
                    },
                    "description": {
                      "type": "string",
                      "nullable": true,
                      "description": "Optional description"
                    },
                    "tags": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "description": "Tags for organizing workflows"
                    },
                    "createdAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Creation time (ISO 8601)"
                    },
                    "updatedAt": {
                      "type": "string",
                      "format": "date-time",
                      "description": "Last modification time (ISO 8601); stamped on every write"
                    },
                    "content": {
                      "type": "string",
                      "description": "Markdown prompt content"
                    }
                  },
                  "required": [
                    "id",
                    "workspaceId",
                    "name",
                    "description",
                    "tags",
                    "createdAt",
                    "updatedAt",
                    "content"
                  ],
                  "additionalProperties": false,
                  "description": "Saved prompt template"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters, or a malformed / invalid request body",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:write` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "415": {
            "description": "Request body must be sent as application/json",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "delete": {
        "tags": [
          "Workflows"
        ],
        "summary": "Delete a workflow",
        "description": "Permanently deletes the workflow and revokes every access grant on it, in one transaction. There is no undo. Requires `workflow:delete` on the workflow.",
        "operationId": "deleteWorkflow",
        "parameters": [
          {
            "in": "path",
            "name": "workflowId",
            "required": true,
            "description": "Workflow identifier",
            "schema": {
              "type": "string",
              "minLength": 1,
              "description": "Workflow identifier"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "deleted": {
                      "type": "boolean",
                      "enum": [
                        true
                      ]
                    }
                  },
                  "required": [
                    "deleted"
                  ],
                  "additionalProperties": false,
                  "description": "Acknowledgement that the workflow was deleted"
                }
              }
            }
          },
          "400": {
            "description": "Invalid query or path parameters",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "401": {
            "description": "Missing or invalid task API key",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "403": {
            "description": "Task API key is missing the `workflows:write` capability, or the key's principal is not permitted to perform this action",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "404": {
            "description": "Resource not found in this organization",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "500": {
            "description": "Unexpected server error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    }
  }
}
