{
  "openapi": "3.1.0",
  "info": {
    "title": "Cosmic agent, AI, and webhook API",
    "version": "1.0.0",
    "summary": "Agent signup, AI generation stored in a Bucket, and the outbound webhook payload.",
    "description": "These operations are not served by https://api.cosmicjs.com. Content reads and writes stay in https://www.cosmicjs.com/openapi.json.\n\nAgent signup, verify, and status are on https://dapi.cosmicjs.com. AI generation is on https://workers.cosmicjs.com and requires the Bucket write key. Generated images, video, and audio are stored in the Bucket media library.\n\nWebhooks are configured in the dashboard (Bucket Settings > Webhooks), not created through this API. Cosmic then POSTs the payload described under webhooks to the URL you set.",
    "contact": {
      "name": "Cosmic Support",
      "email": "support@cosmicjs.com",
      "url": "https://www.cosmicjs.com/contact"
    }
  },
  "externalDocs": {
    "description": "AI onboarding",
    "url": "https://www.cosmicjs.com/docs/ai-onboarding"
  },
  "servers": [
    {
      "url": "https://dapi.cosmicjs.com/v3",
      "description": "Dashboard API. Agent signup, verify, and status."
    }
  ],
  "tags": [
    {
      "name": "Agent signup",
      "description": "Provision a project with no existing Cosmic account."
    },
    {
      "name": "AI",
      "description": "Generate text, images, video, and audio into a Bucket. Served by workers.cosmicjs.com."
    }
  ],
  "paths": {
    "/agents/sign-up": {
      "post": {
        "operationId": "agentSignup",
        "tags": ["Agent signup"],
        "summary": "Create an unclaimed project and bucket",
        "description": "No credentials. Creates a free-tier project tied to a human email and emails that person a one-time code. The response includes the bucket read and write keys and an agent_key for verify and status.",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/AgentSignupRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Project created. A one-time code was emailed to human_email.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AgentSignupResponse" }
              }
            }
          }
        }
      }
    },
    "/agents/verify": {
      "post": {
        "operationId": "agentVerify",
        "tags": ["Agent signup"],
        "summary": "Verify the emailed code",
        "description": "Lifts restricted-mode limits on the unclaimed project. Authenticate with the agent_key from signup: `Authorization: Bearer agk_...`.",
        "security": [{ "agentKey": [] }],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "additionalProperties": false,
                "required": ["code"],
                "properties": {
                  "code": {
                    "type": "string",
                    "minLength": 4,
                    "maxLength": 10,
                    "description": "One-time code emailed to the human owner."
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Verified. Restricted-mode limits lifted.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AgentVerifyResponse" }
              }
            }
          }
        }
      }
    },
    "/agents/status": {
      "get": {
        "operationId": "agentStatus",
        "tags": ["Agent signup"],
        "summary": "Read signup status and recover keys",
        "description": "Returns claim status, plan limits, a fresh access token, and the bucket keys. Authenticate with `Authorization: Bearer agk_...`.",
        "security": [{ "agentKey": [] }],
        "responses": {
          "200": {
            "description": "Current agent session.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/AgentStatusResponse" }
              }
            }
          }
        }
      }
    },
    "/buckets/{slug}/ai/text": {
      "servers": [{ "url": "https://workers.cosmicjs.com/v3" }],
      "post": {
        "operationId": "generateText",
        "tags": ["AI"],
        "summary": "Generate text",
        "description": "Requires the Bucket write key. Provide `prompt` or `messages`. Full parameter list: https://www.cosmicjs.com/docs/api/ai",
        "security": [{ "bucketWriteKey": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/BucketSlug" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/GenerateTextRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Generated text.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GenerateTextResponse" }
              }
            }
          }
        }
      }
    },
    "/buckets/{slug}/ai/image": {
      "servers": [{ "url": "https://workers.cosmicjs.com/v3" }],
      "post": {
        "operationId": "generateImage",
        "tags": ["AI"],
        "summary": "Generate an image into the media library",
        "description": "Requires the Bucket write key. The file is stored as Media. `format` is `png` (default) or `svg`.",
        "security": [{ "bucketWriteKey": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/BucketSlug" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/GenerateImageRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Image stored in the Bucket.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GenerateMediaResponse" }
              }
            }
          }
        }
      }
    },
    "/buckets/{slug}/ai/video": {
      "servers": [{ "url": "https://workers.cosmicjs.com/v3" }],
      "post": {
        "operationId": "generateVideo",
        "tags": ["AI"],
        "summary": "Generate a video into the media library",
        "description": "Requires the Bucket write key. Asynchronous, typically 30-90 seconds. The file is stored as Media.",
        "security": [{ "bucketWriteKey": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/BucketSlug" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/GenerateVideoRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Video stored in the Bucket.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GenerateMediaResponse" }
              }
            }
          }
        }
      }
    },
    "/buckets/{slug}/ai/audio": {
      "servers": [{ "url": "https://workers.cosmicjs.com/v3" }],
      "post": {
        "operationId": "generateAudio",
        "tags": ["AI"],
        "summary": "Generate speech into the media library",
        "description": "Requires the Bucket write key. Text is converted to an MP3 and stored as Media.",
        "security": [{ "bucketWriteKey": [] }],
        "parameters": [
          { "$ref": "#/components/parameters/BucketSlug" }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/GenerateAudioRequest" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Audio stored in the Bucket.",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/GenerateMediaResponse" }
              }
            }
          }
        }
      }
    }
  },
  "webhooks": {
    "contentEvent": {
      "post": {
        "summary": "Content or media changed",
        "description": "Cosmic POSTs this body to the URL configured on the webhook. One webhook listens to one resource and can include several events. `data` contains only the props selected in the dashboard.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "$ref": "#/components/schemas/WebhookPayload" }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Your server accepted the event."
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "agentKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "Agent key returned by signup. It starts with `agk_`."
      },
      "bucketWriteKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "Bucket write key. Send `Authorization: Bearer <write_key>`."
      }
    },
    "parameters": {
      "BucketSlug": {
        "name": "slug",
        "in": "path",
        "required": true,
        "schema": { "type": "string" },
        "description": "Bucket slug."
      }
    },
    "schemas": {
      "AgentSignupRequest": {
        "type": "object",
        "additionalProperties": false,
        "required": ["human_email", "project_name", "agent_id"],
        "properties": {
          "human_email": { "type": "string", "format": "email", "maxLength": 254 },
          "project_name": { "type": "string", "minLength": 1, "maxLength": 100 },
          "agent_id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 200,
            "description": "Stable id for the calling agent, used for rate limiting."
          },
          "client": { "type": "string", "maxLength": 200 },
          "prompt_hint": { "type": "string", "maxLength": 1000 }
        }
      },
      "BucketKeys": {
        "type": "object",
        "properties": {
          "slug": { "type": "string" },
          "read_key": { "type": "string" },
          "write_key": { "type": "string" }
        }
      },
      "AgentSignupResponse": {
        "type": "object",
        "properties": {
          "message": { "type": "string" },
          "auth_type": { "type": "string", "enum": ["unclaimed"] },
          "agent_key": { "type": "string" },
          "access_token": { "type": "string" },
          "project": {
            "type": "object",
            "properties": {
              "id": { "type": "string" },
              "name": { "type": "string" }
            }
          },
          "bucket": { "$ref": "#/components/schemas/BucketKeys" },
          "claim_url": { "type": "string" }
        }
      },
      "AgentVerifyResponse": {
        "type": "object",
        "properties": {
          "message": { "type": "string" },
          "auth_type": { "type": "string", "enum": ["verified"] },
          "claim_status": { "type": "string" },
          "access_token": { "type": "string" }
        }
      },
      "AgentStatusResponse": {
        "type": "object",
        "properties": {
          "access_token": { "type": "string" },
          "project": {
            "type": "object",
            "nullable": true,
            "properties": {
              "id": { "type": "string" },
              "name": { "type": "string" }
            }
          },
          "bucket": {
            "anyOf": [
              { "$ref": "#/components/schemas/BucketKeys" },
              { "type": "null" }
            ]
          },
          "agent_id": { "type": ["string", "null"] },
          "human_email": { "type": "string" }
        }
      },
      "GenerateTextRequest": {
        "type": "object",
        "properties": {
          "prompt": { "type": "string" },
          "messages": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["role", "content"],
              "properties": {
                "role": { "type": "string", "enum": ["user", "assistant"] },
                "content": { "type": "string" }
              }
            }
          },
          "model": { "type": "string" },
          "max_tokens": { "type": "number" },
          "media_url": { "type": "string" },
          "stream": { "type": "boolean" }
        }
      },
      "GenerateTextResponse": {
        "type": "object",
        "properties": {
          "text": { "type": "string" },
          "usage": { "type": "object" }
        }
      },
      "GenerateImageRequest": {
        "type": "object",
        "required": ["prompt"],
        "properties": {
          "prompt": { "type": "string" },
          "format": { "type": "string", "enum": ["png", "svg"] },
          "aspect_ratio": { "type": "string" },
          "model": { "type": "string" },
          "folder": { "type": "string" },
          "alt_text": { "type": "string" }
        }
      },
      "GenerateVideoRequest": {
        "type": "object",
        "required": ["prompt"],
        "properties": {
          "prompt": { "type": "string" },
          "duration": { "type": "number", "enum": [4, 6, 8] },
          "resolution": { "type": "string", "enum": ["720p", "1080p"] },
          "folder": { "type": "string" }
        }
      },
      "GenerateAudioRequest": {
        "type": "object",
        "required": ["prompt"],
        "properties": {
          "prompt": { "type": "string" },
          "voice": { "type": "string" },
          "model": { "type": "string", "enum": ["tts-1", "tts-1-hd"] },
          "folder": { "type": "string" }
        }
      },
      "GenerateMediaResponse": {
        "type": "object",
        "properties": {
          "media": {
            "type": "object",
            "properties": {
              "url": { "type": "string" },
              "imgix_url": { "type": "string" }
            }
          }
        }
      },
      "WebhookPayload": {
        "type": "object",
        "required": ["resource", "event", "triggered_at"],
        "properties": {
          "resource": { "type": "string", "enum": ["objects", "media"] },
          "event": { "type": "string", "enum": ["created", "edited", "deleted"] },
          "triggered_at": {
            "type": "number",
            "description": "UTC milliseconds."
          },
          "data": {
            "type": "object",
            "description": "The props selected when the webhook was created."
          }
        }
      }
    }
  }
}
