{
  "openapi": "3.1.0",
  "info": {
    "title": "Meediar API",
    "version": "1.0.0",
    "summary": "Messaging relay and post scheduling for Instagram, Facebook Messenger and YouTube.",
    "description": "Meediar forwards each incoming Instagram or Messenger message and comment as signed JSON to your webhook. You reply and schedule posts with this API. Authenticate with a Bearer API key (pk_live_...) created in the panel. Errors are JSON: { \"error\": \"message\", \"code\": \"optional_code\" }.",
    "contact": { "name": "Meediar", "url": "https://meediar.com" }
  },
  "servers": [{ "url": "https://meediar.com/api/v1" }],
  "security": [{ "bearerKey": [] }],
  "tags": [
    { "name": "Messages", "description": "Reply in a conversation." },
    { "name": "Comments", "description": "Reply to comments, in public or by private message." },
    { "name": "Posts", "description": "Schedule, list, cancel and retry posts on Instagram, Facebook and YouTube." }
  ],
  "paths": {
    "/messages": {
      "post": {
        "tags": ["Messages"],
        "operationId": "sendMessage",
        "summary": "Reply in a conversation",
        "description": "Meta only accepts replies within 24 hours of the customer's last message. Send an Idempotency-Key header so retries never send twice.",
        "parameters": [{ "$ref": "#/components/parameters/IdempotencyKey" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object", "required": ["conversationId", "text"],
            "properties": {
              "conversationId": { "type": "string", "format": "uuid", "description": "conversation.id of the message.received event." },
              "text": { "type": "string", "maxLength": 2000 }
            }
          } } }
        },
        "responses": {
          "200": { "description": "Sent.", "content": { "application/json": { "schema": { "type": "object", "properties": { "message": { "type": "object", "properties": { "id": { "type": "string", "format": "uuid" } } }, "meediar": { "$ref": "#/components/schemas/SendStatus" } } } } } },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/NoPlan" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "422": { "description": "Meta rejected the message (for example the 24 hour window has passed)." },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "502": { "description": "Meta could not be reached." }
        }
      }
    },
    "/comments/private-reply": {
      "post": {
        "tags": ["Comments"],
        "operationId": "privateReply",
        "summary": "Answer a comment by direct message",
        "description": "One private reply per comment, within 7 days. A second attempt returns 409.",
        "parameters": [{ "$ref": "#/components/parameters/IdempotencyKey" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object", "required": ["connectionId", "postId", "commentId", "text"],
            "properties": {
              "connectionId": { "type": "string", "format": "uuid", "description": "page.id from the webhook event." },
              "postId": { "type": "string", "description": "comment.postId from the webhook event." },
              "commentId": { "type": "string", "description": "comment.id from the webhook event." },
              "text": { "type": "string" }
            }
          } } }
        },
        "responses": {
          "200": { "description": "Sent.", "content": { "application/json": { "schema": { "type": "object", "properties": { "meediar": { "$ref": "#/components/schemas/SendStatus" } } } } } },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/NoPlan" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "409": { "description": "A private reply was already sent to that comment." },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "502": { "description": "Meta could not be reached." }
        }
      }
    },
    "/comments/reply": {
      "post": {
        "tags": ["Comments"],
        "operationId": "replyToComment",
        "summary": "Reply publicly to a comment",
        "parameters": [{ "$ref": "#/components/parameters/IdempotencyKey" }],
        "requestBody": {
          "required": true,
          "content": { "application/json": { "schema": {
            "type": "object", "required": ["connectionId", "postId", "text"],
            "properties": {
              "connectionId": { "type": "string", "format": "uuid" },
              "postId": { "type": "string" },
              "commentId": { "type": "string", "description": "Reply to a specific comment. Omit to comment on the post." },
              "text": { "type": "string" }
            }
          } } }
        },
        "responses": {
          "200": { "description": "Sent.", "content": { "application/json": { "schema": { "type": "object", "properties": { "meediar": { "$ref": "#/components/schemas/SendStatus" } } } } } },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/NoPlan" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "502": { "description": "Meta could not be reached." }
        }
      }
    },
    "/posts": {
      "get": {
        "tags": ["Posts"],
        "operationId": "listPosts",
        "summary": "List posts",
        "description": "Returns up to 500 posts, newest first. Posts whose time has passed are synced with the platform before the list is returned.",
        "parameters": [
          { "name": "from", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "First day (YYYY-MM-DD)." },
          { "name": "to", "in": "query", "schema": { "type": "string", "format": "date" }, "description": "Last day, inclusive (YYYY-MM-DD)." }
        ],
        "responses": {
          "200": { "description": "Posts.", "content": { "application/json": { "schema": { "type": "object", "properties": { "posts": { "type": "array", "items": { "$ref": "#/components/schemas/Post" } } } } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "429": { "$ref": "#/components/responses/TooManyRequests" }
        }
      },
      "post": {
        "tags": ["Posts"],
        "operationId": "createPost",
        "summary": "Schedule or publish a post",
        "description": "Send JSON with already-hosted media (https URLs in mediaItems) or multipart/form-data with the files in the repeated `file` field. Use scheduledFor for a future date, or publishNow=true to publish right away. Limits: 300 MB per video, 20 MB per image, 10 files, caption up to 2,200 characters on Instagram. YouTube needs exactly one video and a title of up to 100 characters.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": { "schema": { "$ref": "#/components/schemas/PostInput" } },
            "multipart/form-data": { "schema": {
              "type": "object", "required": ["connectionId"],
              "properties": {
                "connectionId": { "type": "string", "format": "uuid" },
                "kind": { "type": "string", "enum": ["post", "reel", "story", "video"] },
                "title": { "type": "string", "maxLength": 100 },
                "content": { "type": "string" },
                "scheduledFor": { "type": "string", "format": "date-time" },
                "publishNow": { "type": "boolean" },
                "visibility": { "type": "string", "enum": ["public", "unlisted", "private"] },
                "madeForKids": { "type": "boolean" },
                "firstComment": { "type": "string" },
                "file": { "type": "array", "items": { "type": "string", "format": "binary" } }
              }
            } }
          }
        },
        "responses": {
          "201": { "description": "Created.", "content": { "application/json": { "schema": { "type": "object", "properties": { "post": { "$ref": "#/components/schemas/Post" } } } } } },
          "400": { "$ref": "#/components/responses/BadRequest" },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "402": { "$ref": "#/components/responses/NoPlan" },
          "403": { "description": "Email not confirmed, or the platform refused the post." },
          "404": { "$ref": "#/components/responses/NotFound" },
          "413": { "description": "A file exceeds the size limit." },
          "429": { "$ref": "#/components/responses/TooManyRequests" },
          "502": { "description": "The platform did not accept the post. The post is stored as failed and can be retried." },
          "503": { "description": "Publishing is temporarily unavailable." }
        }
      }
    },
    "/posts/{id}": {
      "delete": {
        "tags": ["Posts"],
        "operationId": "cancelPost",
        "summary": "Cancel a scheduled post",
        "description": "Removes the post from the platform and marks it cancelled. The history is kept.",
        "parameters": [{ "$ref": "#/components/parameters/PostId" }],
        "responses": {
          "200": { "description": "Cancelled.", "content": { "application/json": { "schema": { "type": "object", "properties": { "ok": { "type": "boolean" } } } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "502": { "description": "Could not cancel on the platform; try again." }
        }
      }
    },
    "/posts/{id}/retry": {
      "post": {
        "tags": ["Posts"],
        "operationId": "retryPost",
        "summary": "Retry a failed post",
        "description": "Only posts in the failed state. If the original time has passed the post is published right away.",
        "parameters": [{ "$ref": "#/components/parameters/PostId" }],
        "responses": {
          "200": { "description": "Sent again.", "content": { "application/json": { "schema": { "type": "object", "properties": { "post": { "$ref": "#/components/schemas/Post" } } } } } },
          "401": { "$ref": "#/components/responses/Unauthorized" },
          "404": { "$ref": "#/components/responses/NotFound" },
          "409": { "description": "The post is not failed, or the connection is no longer active." }
        }
      }
    }
  },
  "webhooks": {
    "message.received": {
      "post": {
        "summary": "A customer sent a message",
        "description": "Sent to the webhook URL of the connection. Answer 2xx within 5 seconds; otherwise it is retried at 5 s, 30 s, 2 min, 5 min and 15 min. Headers: meediar-event-id, meediar-timestamp and meediar-signature (v1=HMAC-SHA256 of `{event-id}.{timestamp}.{raw body}` with your whsec_ secret). Reject events older than 300 seconds.",
        "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/MessageEvent" } } } },
        "responses": { "200": { "description": "Return any 2xx status." } }
      }
    },
    "comment.received": {
      "post": {
        "summary": "Someone commented on a post",
        "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CommentEvent" } } } },
        "responses": { "200": { "description": "Return any 2xx status." } }
      }
    },
    "post.published": {
      "post": {
        "summary": "A scheduled post was published",
        "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PostEvent" } } } },
        "responses": { "200": { "description": "Return any 2xx status." } }
      }
    },
    "post.failed": {
      "post": {
        "summary": "A scheduled post failed",
        "requestBody": { "content": { "application/json": { "schema": { "$ref": "#/components/schemas/PostEvent" } } } },
        "responses": { "200": { "description": "Return any 2xx status." } }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerKey": { "type": "http", "scheme": "bearer", "description": "API key created in the panel (pk_live_...). Limit: 120 requests per minute." }
    },
    "parameters": {
      "IdempotencyKey": { "name": "Idempotency-Key", "in": "header", "required": false, "schema": { "type": "string" }, "description": "Repeating the request with the same key returns the first response and sends nothing twice. Use the event id." },
      "PostId": { "name": "id", "in": "path", "required": true, "schema": { "type": "string", "format": "uuid" } }
    },
    "responses": {
      "BadRequest": { "description": "Missing or invalid fields.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "Unauthorized": { "description": "Missing, invalid or revoked API key.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "NoPlan": { "description": "No active plan (code no_plan).", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "NotFound": { "description": "Not found.", "content": { "application/json": { "schema": { "$ref": "#/components/schemas/Error" } } } },
      "TooManyRequests": { "description": "Too many requests. Retry after the Retry-After header.", "headers": { "Retry-After": { "schema": { "type": "integer" } } } }
    },
    "schemas": {
      "Error": { "type": "object", "properties": { "error": { "type": "string" }, "code": { "type": "string" } } },
      "SendStatus": { "type": "object", "properties": { "status": { "type": "string", "enum": ["sent", "failed"] }, "idempotentReplay": { "type": "boolean" } } },
      "PostInput": {
        "type": "object", "required": ["connectionId"],
        "properties": {
          "connectionId": { "type": "string", "format": "uuid", "description": "page.id of an active Instagram, Facebook or YouTube connection." },
          "kind": { "type": "string", "enum": ["post", "reel", "story", "video"], "description": "Instagram and Facebook: post, reel or story. YouTube: video. Default: post (video for YouTube)." },
          "title": { "type": "string", "maxLength": 100, "description": "Required for YouTube. Optional title of a Facebook reel." },
          "content": { "type": "string", "description": "Caption or description." },
          "mediaItems": { "type": "array", "maxItems": 10, "items": { "type": "object", "required": ["type", "url"], "properties": { "type": { "type": "string", "enum": ["image", "video"] }, "url": { "type": "string", "format": "uri", "description": "Public https URL." } } } },
          "scheduledFor": { "type": "string", "format": "date-time", "description": "Future date and time (ISO 8601). Required unless publishNow is true." },
          "publishNow": { "type": "boolean", "default": false },
          "timezone": { "type": "string", "default": "America/Guayaquil" },
          "visibility": { "type": "string", "enum": ["public", "unlisted", "private"], "default": "public", "description": "YouTube only." },
          "madeForKids": { "type": "boolean", "description": "YouTube only." },
          "firstComment": { "type": "string", "description": "Posted as the first comment (not on stories)." }
        }
      },
      "Post": {
        "type": "object",
        "properties": {
          "id": { "type": "string", "format": "uuid" },
          "connectionId": { "type": "string", "format": "uuid" },
          "channel": { "type": "string", "enum": ["instagram", "facebook", "youtube"] },
          "kind": { "type": "string", "enum": ["post", "reel", "story", "video"] },
          "title": { "type": ["string", "null"] },
          "content": { "type": "string" },
          "mediaItems": { "type": "array", "items": { "type": "object", "properties": { "type": { "type": "string" }, "url": { "type": "string" } } } },
          "scheduledFor": { "type": "string", "format": "date-time" },
          "timezone": { "type": "string" },
          "visibility": { "type": "string" },
          "status": { "type": "string", "enum": ["scheduled", "published", "failed", "cancelled"] },
          "error": { "type": ["string", "null"] },
          "createdAt": { "type": "string", "format": "date-time" }
        }
      },
      "Page": {
        "type": "object",
        "properties": { "id": { "type": "string", "format": "uuid", "description": "Use as connectionId." }, "channel": { "type": "string" }, "name": { "type": "string" }, "username": { "type": ["string", "null"] } }
      },
      "MessageEvent": {
        "type": "object",
        "properties": {
          "type": { "const": "message.received" },
          "test": { "type": "boolean", "description": "True for test events sent from the panel. Do not answer them." },
          "tenant": { "type": "object", "properties": { "id": { "type": "string" } } },
          "page": { "$ref": "#/components/schemas/Page" },
          "conversation": { "type": "object", "properties": { "id": { "type": "string", "format": "uuid", "description": "Use as conversationId." }, "contactId": { "type": "string" } } },
          "message": { "type": "object", "properties": { "id": { "type": "string" }, "direction": { "const": "inbound" }, "status": { "type": "string" }, "text": { "type": ["string", "null"] }, "attachment": {}, "createdAt": { "type": "string", "format": "date-time" } } }
        }
      },
      "CommentEvent": {
        "type": "object",
        "properties": {
          "type": { "const": "comment.received" },
          "tenant": { "type": "object", "properties": { "id": { "type": "string" } } },
          "page": { "$ref": "#/components/schemas/Page" },
          "comment": { "type": "object", "properties": { "id": { "type": "string" }, "postId": { "type": ["string", "null"] }, "platformPostId": { "type": "string" }, "text": { "type": "string" }, "author": { "type": "object", "properties": { "id": { "type": "string" }, "username": { "type": "string" }, "name": { "type": "string" } } }, "createdAt": { "type": "string", "format": "date-time" } } }
        }
      },
      "PostEvent": {
        "type": "object",
        "properties": {
          "type": { "type": "string", "enum": ["post.published", "post.failed"] },
          "tenant": { "type": "object", "properties": { "id": { "type": "string" } } },
          "page": { "$ref": "#/components/schemas/Page" },
          "post": { "type": "object", "properties": { "id": { "type": "string", "format": "uuid" }, "channel": { "type": "string" }, "kind": { "type": "string" }, "title": { "type": ["string", "null"] }, "content": { "type": "string" }, "mediaUrls": { "type": "array", "items": { "type": "string" } }, "scheduledFor": { "type": "string", "format": "date-time" }, "status": { "type": "string", "enum": ["published", "failed"] }, "error": { "type": ["string", "null"] }, "url": { "type": ["string", "null"], "description": "Link to the published post, when available." } } }
        }
      }
    }
  }
}
