{
  "openapi": "3.1.0",
  "info": {
    "title": "CronSynth API",
    "description": "Time-driven coordination for autonomous AI agents",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://cronsynth.xyz",
      "description": "Production"
    }
  ],
  "security": [
    {
      "x402Session": []
    }
  ],
  "paths": {
    "/api/schedule": {
      "post": {
        "operationId": "createSchedule",
        "summary": "Create a new schedule",
        "description": "Register a webhook URL with a cron expression. CronSynth will trigger the webhook according to the schedule.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateScheduleRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Schedule created",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CreateScheduleResponse"
                }
              }
            }
          },
          "400": {
            "description": "Invalid request"
          },
          "402": {
            "description": "Payment required or insufficient balance"
          }
        }
      }
    },
    "/api/unschedule": {
      "post": {
        "operationId": "deleteSchedule",
        "summary": "Delete a schedule",
        "description": "Remove an existing schedule. Must be the schedule owner.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DeleteScheduleRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Schedule deleted",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SuccessResponse"
                }
              }
            }
          },
          "404": {
            "description": "Schedule not found"
          }
        }
      }
    },
    "/api/schedules": {
      "get": {
        "operationId": "listSchedules",
        "summary": "List schedules",
        "description": "List all schedules owned by the authenticated agent.",
        "responses": {
          "200": {
            "description": "List of schedules",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ListSchedulesResponse"
                }
              }
            }
          }
        }
      }
    },
    "/api/health": {
      "get": {
        "operationId": "healthCheck",
        "summary": "Health check",
        "description": "Check service status and view aggregate metrics.",
        "security": [],
        "responses": {
          "200": {
            "description": "Service healthy",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HealthResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "x402Session": {
        "type": "apiKey",
        "in": "header",
        "name": "X-402-Session",
        "description": "x402 payment session token"
      }
    },
    "schemas": {
      "CreateScheduleRequest": {
        "type": "object",
        "required": ["webhookUrl", "cron"],
        "properties": {
          "webhookUrl": {
            "type": "string",
            "format": "uri",
            "description": "HTTPS URL to receive triggers",
            "example": "https://agent.example/trigger"
          },
          "cron": {
            "type": "string",
            "description": "Standard 5-field cron expression",
            "example": "*/5 * * * *"
          },
          "label": {
            "type": "string",
            "description": "Optional human-readable label",
            "example": "rebalance-bot"
          }
        }
      },
      "CreateScheduleResponse": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string",
            "example": "sch_a1b2c3d4e5f6g7h8"
          },
          "nextRun": {
            "type": "integer",
            "description": "Unix timestamp of next trigger",
            "example": 1701433200
          },
          "webhookSecret": {
            "type": "string",
            "description": "Secret for verifying webhook signatures"
          }
        }
      },
      "DeleteScheduleRequest": {
        "type": "object",
        "required": ["id"],
        "properties": {
          "id": {
            "type": "string",
            "example": "sch_a1b2c3d4e5f6g7h8"
          }
        }
      },
      "SuccessResponse": {
        "type": "object",
        "properties": {
          "ok": {
            "type": "boolean",
            "example": true
          }
        }
      },
      "ListSchedulesResponse": {
        "type": "object",
        "properties": {
          "schedules": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Schedule"
            }
          }
        }
      },
      "Schedule": {
        "type": "object",
        "properties": {
          "id": {
            "type": "string"
          },
          "webhookUrl": {
            "type": "string"
          },
          "cron": {
            "type": "string"
          },
          "label": {
            "type": "string",
            "nullable": true
          },
          "nextRun": {
            "type": "integer"
          },
          "lastRunAt": {
            "type": "integer",
            "nullable": true
          },
          "runCount": {
            "type": "integer"
          },
          "active": {
            "type": "boolean"
          },
          "consecutiveFailures": {
            "type": "integer"
          }
        }
      },
      "HealthResponse": {
        "type": "object",
        "properties": {
          "status": {
            "type": "string",
            "enum": ["ok", "degraded", "down"]
          },
          "lastCronRun": {
            "type": "integer"
          },
          "metrics": {
            "type": "object",
            "properties": {
              "totalTriggers": {
                "type": "integer"
              },
              "totalFailures": {
                "type": "integer"
              },
              "activeSchedules": {
                "type": "integer"
              }
            }
          }
        }
      },
      "WebhookPayload": {
        "type": "object",
        "description": "Payload sent to agent webhooks",
        "properties": {
          "scheduleId": {
            "type": "string"
          },
          "timestamp": {
            "type": "integer",
            "description": "Unix timestamp of trigger"
          },
          "runNumber": {
            "type": "integer",
            "description": "Incrementing trigger count"
          },
          "cron": {
            "type": "string"
          }
        }
      }
    }
  }
}

