{
  "openapi": "3.1.0",
  "info": {
    "title": "X402 Music Gateway",
    "version": "2.0.0",
    "description": "Pre-cleared music catalog, vector discovery, commercial licensing with signed receipts, stem streaming, rendering and credit billing for apps and AI agents."
  },
  "servers": [{ "url": "https://levievans.cloud" }],
  "security": [{ "bearerAuth": [] }, { "apiKeyAuth": [] }],
  "components": {
    "securitySchemes": {
      "bearerAuth": { "type": "http", "scheme": "bearer", "bearerFormat": "JWT" },
      "apiKeyAuth": { "type": "apiKey", "in": "header", "name": "X-API-Key" }
    },
    "schemas": {
      "Error": {
        "type": "object",
        "properties": {
          "error": {
            "type": "object",
            "properties": {
              "code": { "type": "string" },
              "message": { "type": "string" },
              "details": {},
              "request_id": { "type": "string" }
            },
            "required": ["code", "message"]
          }
        }
      },
      "Track": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "title": { "type": "string" },
          "artist": { "type": ["string", "null"] },
          "genre": { "type": ["string", "null"] },
          "bpm": { "type": "number" },
          "key": { "type": "string" },
          "scale": { "type": "string" },
          "key_scale": { "type": "string" },
          "sentiment_tags": { "type": "array", "items": { "type": "string" } },
          "sentiment_vector": { "type": "array", "items": { "type": "number" } },
          "micro_license": { "type": "object" },
          "stems": { "type": "array", "items": { "type": "object" } }
        }
      },
      "Receipt": {
        "type": "object",
        "required": ["licenseId", "client", "trackIds", "usage", "issuedAt", "expiresAt", "feeCents", "signature"],
        "properties": {
          "licenseId": { "type": "string" },
          "client": { "type": "string" },
          "trackIds": { "type": "array", "items": { "type": "string" } },
          "usage": { "type": "string", "enum": ["commercial", "internal"] },
          "issuedAt": { "type": "string", "format": "date-time" },
          "expiresAt": { "type": "string", "format": "date-time" },
          "feeCents": { "type": "integer" },
          "signature": { "type": "string" }
        }
      }
    }
  },
  "paths": {
    "/api/v1/auth/register": {
      "post": {
        "summary": "Create a client and mint an API key + JWT",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["name"],
                "properties": {
                  "name": { "type": "string" },
                  "email": { "type": "string", "format": "email" },
                  "wallet": { "type": "string" }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "Client created" }, "400": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/auth/token": {
      "post": {
        "summary": "Exchange an API key for a JWT",
        "security": [],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": { "type": "object", "properties": { "api_key": { "type": "string" } } }
            }
          }
        },
        "responses": { "200": { "description": "JWT issued" }, "401": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/catalog": {
      "get": {
        "summary": "Paginated catalog with filters",
        "parameters": [
          { "name": "genre", "in": "query", "schema": { "type": "string" } },
          { "name": "mood", "in": "query", "schema": { "type": "string" } },
          { "name": "key", "in": "query", "schema": { "type": "string" } },
          { "name": "min_bpm", "in": "query", "schema": { "type": "number" } },
          { "name": "max_bpm", "in": "query", "schema": { "type": "number" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 100, "maximum": 300 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "Catalog page" }, "401": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/catalog/{trackId}": {
      "get": {
        "summary": "Single track metadata",
        "parameters": [{ "name": "trackId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "Track" }, "404": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/search": {
      "get": {
        "summary": "Metadata + vector search",
        "parameters": [
          { "name": "min_bpm", "in": "query", "schema": { "type": "number" } },
          { "name": "max_bpm", "in": "query", "schema": { "type": "number" } },
          { "name": "key", "in": "query", "schema": { "type": "string" } },
          { "name": "tags", "in": "query", "schema": { "type": "string" } },
          { "name": "vibe_vector", "in": "query", "schema": { "type": "string" }, "description": "8 floats, or true to include vectors" },
          { "name": "q", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 20 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "Ranked results" }, "400": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/stats": { "get": { "summary": "Public catalog statistics", "security": [], "responses": { "200": { "description": "Stats" } } } },
    "/api/v1/genres": { "get": { "summary": "Public genre index", "security": [], "responses": { "200": { "description": "Genres" } } } },
    "/api/v1/pricing": { "get": { "summary": "Public credit packs and license tiers", "security": [], "responses": { "200": { "description": "Pricing" } } } },
    "/api/v1/license": {
      "post": {
        "summary": "Lock tracks and issue a signed receipt",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["track_ids"],
                "properties": {
                  "client_wallet": { "type": "string" },
                  "account_token": { "type": "string" },
                  "track_ids": { "type": "array", "items": { "type": "string" } },
                  "usage": { "type": "string", "enum": ["commercial", "internal"] },
                  "duration_days": { "type": "integer" },
                  "exclusive": { "type": "boolean" }
                }
              }
            }
          }
        },
        "responses": { "201": { "description": "License + receipt" }, "404": { "$ref": "#/components/schemas/Error" }, "409": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/license/{licenseId}": {
      "get": {
        "summary": "Retrieve a license",
        "parameters": [{ "name": "licenseId", "in": "path", "required": true, "schema": { "type": "string" } }],
        "responses": { "200": { "description": "License" }, "404": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/license/verify": {
      "post": {
        "summary": "Verify a receipt signature",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": { "type": "object", "properties": { "receipt": { "$ref": "#/components/schemas/Receipt" } } }
            }
          }
        },
        "responses": { "200": { "description": "Validity" }, "400": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/stems": {
      "get": {
        "summary": "List stems",
        "parameters": [
          { "name": "track_id", "in": "query", "schema": { "type": "string" } },
          { "name": "limit", "in": "query", "schema": { "type": "integer", "default": 200 } },
          { "name": "offset", "in": "query", "schema": { "type": "integer", "default": 0 } }
        ],
        "responses": { "200": { "description": "Stems" } }
      }
    },
    "/api/v1/stems/{trackId}": {
      "get": {
        "summary": "Stem manifest or raw WAV stream",
        "parameters": [
          { "name": "trackId", "in": "path", "required": true, "schema": { "type": "string" } },
          { "name": "stem", "in": "query", "schema": { "type": "string", "enum": ["drums", "bass", "melody", "vocals", "fx"] } }
        ],
        "responses": {
          "200": { "description": "Manifest (JSON) or chunked WAV", "content": { "audio/wav": { "schema": { "type": "string", "format": "binary" } } } },
          "404": { "$ref": "#/components/schemas/Error" }
        }
      }
    },
    "/api/v1/render": {
      "post": {
        "summary": "Render MIDI + stems",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["track_ids"],
                "properties": {
                  "track_ids": { "type": "array", "items": { "type": "string" } },
                  "stems": { "type": "array", "items": { "type": "string" } },
                  "duration_ms": { "type": "integer" },
                  "bpm": { "type": "number" },
                  "format": { "type": "string", "enum": ["wav", "mp3"] },
                  "mode": { "type": "string", "enum": ["stream", "json"] }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "WAV stream or render manifest" } }
      }
    },
    "/api/v1/billing/checkout": {
      "post": {
        "summary": "Create a Stripe Checkout session for credits",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "required": ["pack_id"],
                "properties": {
                  "pack_id": { "type": "string", "enum": ["starter", "studio", "label"] },
                  "email": { "type": "string", "format": "email" }
                }
              }
            }
          }
        },
        "responses": { "200": { "description": "Checkout URL" }, "503": { "$ref": "#/components/schemas/Error" } }
      }
    },
    "/api/v1/billing/webhooks": {
      "post": {
        "summary": "Stripe webhook",
        "security": [],
        "responses": { "200": { "description": "Received" }, "400": { "$ref": "#/components/schemas/Error" } }
      }
    }
  }
}
