openapi: 3.0.3
info:
  title: naturali.ai — Triggers API
  version: 1.0.0
  description: >
    Triggers — the cron surface (`PRODUCT.md` §10, API.md). Before this, the
    only way to run an agent was an inbound call from a caller holding its own
    scheduler; a trigger lets the runtime hold the clock instead.

    **Scope: a schedule trigger targeting an agent or an orchestration.** A
    `target_type: orchestration` trigger is the scheduled counterpart of
    `POST /v1/projects/{project_id}/orchestration-runs` — each fire starts a
    run of the graph, and the trigger's `input` seeds that run's input.

    The runtime's own trigger primitive also has `manual` / `webhook` types and
    a `tool` target, which naturali does not front. A webhook trigger has no
    naturali-side receiver to front it with; a tool target also needs an
    `action` naming which operation of the tool to call, which is a surface of
    its own rather than one more enum value. `type` is still on the wire (fixed
    to `schedule`) rather than omitted, so a client that reads it today keeps
    reading it once a second value exists.

    A trigger that was created directly against the runtime (by a formation,
    bypassing naturali) in a shape this surface doesn't front — a different
    `type`, a different `target_type` — reads as `404` here, the same
    treatment `traces.yaml` gives a trace's `file_id`: the contract only
    speaks for what it fronts.

    Every firing is recorded and readable per trigger — what ran, when, and
    what came back — so a schedule is auditable, not just "it's probably
    running".
  contact:
    name: naturali.ai
    url: https://naturali.ai
servers:
  - url: '{baseUrl}'
    description: Host of your naturali.ai deployment; every path carries the /v1 prefix.
    variables:
      baseUrl:
        description: Base host URL.
        default: https://api.naturali.ai
tags:
  - name: Triggers
    description: Schedule an agent or an orchestration to run on a cron, fire it manually, and read what happened.
security:
  - bearerAuth: []
paths:
  /v1/projects/{project_id}/triggers:
    parameters:
      - $ref: '#/components/parameters/ProjectId'
    get:
      tags: [Triggers]
      summary: List triggers
      description: >
        The project's schedule triggers, of either fronted target kind.

        `total` is the runtime's count of the project's schedule triggers, so it
        can exceed the rows returned when a trigger was authored directly
        against the runtime with a target this surface doesn't front (the same
        bypass `getTrigger` answers `404` for). Pass `target_type` — which is
        filtered and counted upstream — when an exact count matters.
      operationId: listTriggers
      parameters:
        - $ref: '#/components/parameters/TargetTypeFilter'
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Offset'
      responses:
        '200':
          description: A page of triggers.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TriggerList'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
    post:
      tags: [Triggers]
      summary: Create a trigger
      description: >
        Schedules `target_id` — an agent or an orchestration in this project,
        per `target_type` — to run on `cron`, a 5-field cron expression
        evaluated in UTC.
      operationId: createTrigger
      parameters:
        - $ref: '#/components/parameters/IdempotencyKey'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TriggerCreate'
      responses:
        '201':
          description: Trigger created.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Trigger'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
  /v1/projects/{project_id}/triggers/{trigger_id}:
    parameters:
      - $ref: '#/components/parameters/ProjectId'
      - $ref: '#/components/parameters/TriggerId'
    get:
      tags: [Triggers]
      summary: Get a trigger
      operationId: getTrigger
      responses:
        '200':
          description: The trigger.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Trigger'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
    patch:
      tags: [Triggers]
      summary: Update a trigger
      description: >
        Retune the schedule, its target, or whether it fires at all. At least
        one field is required. `type` is immutable; `target_type` may be changed
        only together with `target_id`.
      operationId: updateTrigger
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TriggerUpdate'
      responses:
        '200':
          description: Trigger updated.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Trigger'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
    delete:
      tags: [Triggers]
      summary: Delete a trigger
      description: Removes the trigger. Its firing history is kept.
      operationId: deleteTrigger
      responses:
        '204':
          description: Trigger deleted.
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
  '/v1/projects/{project_id}/triggers/{trigger_id}:fire':
    parameters:
      - $ref: '#/components/parameters/ProjectId'
      - $ref: '#/components/parameters/TriggerId'
    post:
      tags: [Triggers]
      summary: Fire a trigger manually
      description: >
        Runs the trigger's target right now, outside its schedule, and waits
        for the run to finish. This is the `…:fire` action; the path segment
        is `{trigger_id}:fire`.

        `input` is shallow-merged over the trigger's own stored `input` for
        this run only — the trigger's configuration is unchanged.
      operationId: fireTrigger
      requestBody:
        required: false
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/TriggerFire'
      responses:
        '200':
          description: The terminal firing record.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TriggerFiring'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          description: The trigger is inactive, so it cannot be fired.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorResponse'
  /v1/projects/{project_id}/triggers/{trigger_id}/firings:
    parameters:
      - $ref: '#/components/parameters/ProjectId'
      - $ref: '#/components/parameters/TriggerId'
    get:
      tags: [Triggers]
      summary: List a trigger's firings
      description: Every time this trigger ran, newest first — scheduled and manual alike.
      operationId: listTriggerFirings
      parameters:
        - $ref: '#/components/parameters/Limit'
        - $ref: '#/components/parameters/Offset'
      responses:
        '200':
          description: A page of firings.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TriggerFiringList'
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
  /v1/projects/{project_id}/triggers/{trigger_id}/firings/{firing_id}:
    parameters:
      - $ref: '#/components/parameters/ProjectId'
      - $ref: '#/components/parameters/TriggerId'
      - $ref: '#/components/parameters/FiringId'
    get:
      tags: [Triggers]
      summary: Get a trigger firing
      operationId: getTriggerFiring
      responses:
        '200':
          description: The firing.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/TriggerFiring'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '404':
          $ref: '#/components/responses/NotFound'
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: A naturali API key (nat_sk_…) or a session JWT.
  parameters:
    TargetTypeFilter:
      name: target_type
      in: query
      required: false
      description: >
        Narrow the page to one target kind. Filtered and counted upstream, so
        `total` is exact. Omit it to list both kinds — see the operation's note
        on `total`.
      schema:
        $ref: '#/components/schemas/TriggerTargetType'
      example: orchestration
    Limit:
      name: limit
      in: query
      required: false
      description: Maximum items to return (1–100).
      schema:
        type: integer
        minimum: 1
        maximum: 100
        default: 20
    Offset:
      name: offset
      in: query
      required: false
      description: >
        Items to skip. These lists page by offset rather than by naturali's
        usual opaque cursor because the upstream ordering is offset-based; a
        cursor here would only imitate a keyset.
      schema:
        type: integer
        minimum: 0
        default: 0
    IdempotencyKey:
      name: Idempotency-Key
      in: header
      required: false
      description: Client-supplied key to make this mutating POST idempotent.
      schema:
        type: string
    ProjectId:
      name: project_id
      in: path
      required: true
      description: Project public ID (proj_ prefix).
      schema:
        type: string
        example: proj_V1StGXR8Z5jdHi6B
    TriggerId:
      name: trigger_id
      in: path
      required: true
      description: Trigger public ID (trg_ prefix) — the runtime trigger id.
      schema:
        type: string
        example: trg_V1StGXR8Z5jdHi6B
    FiringId:
      name: firing_id
      in: path
      required: true
      description: Firing public ID — the runtime firing id.
      schema:
        type: string
        example: trg_fire_V1StGXR8Z5jdHi6B
  responses:
    Unauthorized:
      description: Missing or invalid credentials.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    BadRequest:
      description: The request was malformed or failed validation.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
    NotFound:
      description: The resource does not exist (existence is not leaked).
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
  schemas:
    ErrorResponse:
      type: object
      required: [error]
      properties:
        error:
          type: object
          required: [code, message]
          properties:
            code:
              type: string
              example: bad_request
            message:
              type: string
              example: '`cron` is required.'
            details:
              type: object
              additionalProperties: true
    TriggerType:
      type: string
      description: >
        Fixed to `schedule` in v1 — the only trigger type naturali fronts
        today.
      enum: [schedule]
      example: schedule
    TriggerTargetType:
      type: string
      description: >
        What the trigger runs. `agent` starts an agent generation;
        `orchestration` starts a run of an orchestration graph. The runtime's
        third kind, `tool`, is not fronted here.
      enum: [agent, orchestration]
      example: agent
    Trigger:
      type: object
      required:
        - id
        - project_id
        - name
        - description
        - type
        - target_type
        - target_id
        - input
        - cron
        - active
        - next_fire_at
        - created_at
        - updated_at
      properties:
        id:
          type: string
          description: Public trigger ID (trg_ prefix) — the runtime trigger id.
          example: trg_V1StGXR8Z5jdHi6B
        project_id:
          type: string
          x-naturali-ref: project
          example: proj_V1StGXR8Z5jdHi6B
        name:
          type: string
          example: health-check
        description:
          type: string
          nullable: true
          example: Daily dossier + performance health check
        type:
          $ref: '#/components/schemas/TriggerType'
        target_type:
          $ref: '#/components/schemas/TriggerTargetType'
        target_id:
          type: string
          description: >
            The agent or orchestration this trigger runs, per `target_type`.
          example: agent_V1StGXR8Z5jdHi6B
        input:
          type: object
          nullable: true
          additionalProperties: true
          description: >
            Static input passed to the target on every fire, scheduled or
            manual — shallow-merged under any input a manual `…:fire` call
            supplies for that one run. For an `orchestration` target this is
            the run's initial input.
        cron:
          type: string
          description: 5-field cron expression, evaluated in UTC.
          example: 0 9 * * *
        active:
          type: boolean
          description: >
            Whether the trigger fires at all — on its schedule and on a
            manual `…:fire` call alike. `…:fire` on an inactive trigger is
            `409`.
          example: true
        next_fire_at:
          type: string
          format: date-time
          nullable: true
          description: Server-computed; the next time the schedule fires.
          example: '2026-08-06T09:00:00.000Z'
        created_at:
          type: string
          format: date-time
          example: '2026-08-01T00:00:00.000Z'
        updated_at:
          type: string
          format: date-time
          example: '2026-08-01T00:00:00.000Z'
    TriggerCreate:
      type: object
      required: [name, target_id, cron]
      properties:
        name:
          type: string
          example: health-check
        description:
          type: string
          example: Daily dossier + performance health check
        target_type:
          allOf:
            - $ref: '#/components/schemas/TriggerTargetType'
          default: agent
          description: >
            Which kind of resource `target_id` names. Defaults to `agent`, so a
            body written before the orchestration target existed keeps its
            meaning.
        target_id:
          type: string
          description: >
            The agent or orchestration this trigger runs, per `target_type`.
            Must belong to this project — an id from another project is `404`.
          example: agent_V1StGXR8Z5jdHi6B
        input:
          type: object
          additionalProperties: true
        cron:
          type: string
          description: 5-field cron expression, evaluated in UTC.
          example: 0 9 * * *
        active:
          type: boolean
          default: true
          example: true
    TriggerUpdate:
      type: object
      description: At least one field is required.
      properties:
        name:
          type: string
          example: health-check-v2
        description:
          type: string
          nullable: true
          example: Daily dossier + performance health check (revised)
        target_type:
          allOf:
            - $ref: '#/components/schemas/TriggerTargetType'
          description: >
            Change what kind of resource the trigger runs. Must be sent
            **together with `target_id`** — the kind and the id are one target,
            and accepting the kind alone would leave the trigger pointing at an
            id of the wrong kind (`400`). Sending `target_id` on its own is
            fine and keeps the trigger's current kind, which is the kind the new
            id is validated against.
        target_id:
          type: string
          example: agent_V1StGXR8Z5jdHi6B
        input:
          type: object
          nullable: true
          additionalProperties: true
        cron:
          type: string
          example: 0 */6 * * *
        active:
          type: boolean
          example: false
    TriggerFire:
      type: object
      properties:
        input:
          type: object
          additionalProperties: true
          description: Fire-time input, shallow-merged over the trigger's stored `input`.
    TriggerList:
      type: object
      required: [data, total, limit, offset]
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Trigger'
        total:
          type: integer
          nullable: true
        limit:
          type: integer
        offset:
          type: integer
    TriggerFiringSource:
      type: string
      description: What caused this run — the schedule itself, or a manual `…:fire` call.
      enum: [schedule, manual]
      example: schedule
    TriggerFiring:
      type: object
      description: One run of a trigger's target, and what came back.
      required:
        - id
        - trigger_id
        - project_id
        - source
        - status
        - input
        - result
        - error
        - started_at
        - completed_at
        - created_at
        - updated_at
      properties:
        id:
          type: string
          description: Public firing ID — the runtime firing id.
          example: trg_fire_V1StGXR8Z5jdHi6B
        trigger_id:
          type: string
          x-naturali-ref: trigger
          example: trg_V1StGXR8Z5jdHi6B
        project_id:
          type: string
          x-naturali-ref: project
          example: proj_V1StGXR8Z5jdHi6B
        source:
          $ref: '#/components/schemas/TriggerFiringSource'
        status:
          type: string
          enum: [pending, running, succeeded, failed]
          example: succeeded
        input:
          type: object
          nullable: true
          additionalProperties: true
        result:
          type: object
          nullable: true
          additionalProperties: true
          description: '`{ target_type, result_id, status, output }` — output truncated.'
        error:
          type: object
          nullable: true
          additionalProperties: true
          description: '`{ code, message, meta }`, present only when `status` is `failed`.'
        started_at:
          type: string
          format: date-time
          nullable: true
          example: '2026-08-06T09:00:00.100Z'
        completed_at:
          type: string
          format: date-time
          nullable: true
          example: '2026-08-06T09:00:04.512Z'
        created_at:
          type: string
          format: date-time
          example: '2026-08-06T09:00:00.000Z'
        updated_at:
          type: string
          format: date-time
          example: '2026-08-06T09:00:04.512Z'
    TriggerFiringList:
      type: object
      required: [data, total, limit, offset]
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/TriggerFiring'
        total:
          type: integer
          nullable: true
        limit:
          type: integer
        offset:
          type: integer
