{
  "openapi": "3.1.0",
  "info": {
    "title": "MailJunky API",
    "version": "1.0.0",
    "description": "The MailJunky API lets you send transactional emails, track user events, manage contacts, and build intelligent email workflows. All endpoints require API key authentication unless noted otherwise.\n\n## SDK\n\nInstall the official SDK for the easiest integration:\n\n```bash\nnpm install @mailjunky/sdk\n```\n\n```typescript\nimport { MailJunky } from '@mailjunky/sdk'\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n```\n\nFor browser-side event tracking:\n\n```typescript\nimport { initMailJunky } from '@mailjunky/sdk/browser'\nconst mj = initMailJunky({ apiKey: 'mj_pub_xxx' })\n```\n\n## Authentication\n\nAuthenticate by including your API key in the `Authorization` header:\n\n```\nAuthorization: Bearer mj_live_your_api_key\n```\n\nYou can also use the `X-API-Key` header. Public keys (`mj_pub_*`) are restricted to event tracking only.\n\n## Rate Limits\n\nAll responses include rate limit headers:\n- `X-RateLimit-Limit` - Requests allowed per window\n- `X-RateLimit-Remaining` - Requests remaining\n- `X-RateLimit-Reset` - Window reset time (unix timestamp)\n- `Retry-After` - Seconds to wait (when rate limited)\n\n## Base URL\n\n```\nhttps://www.mailjunky.ai/api/v1\n```",
    "contact": {
      "name": "MailJunky Support",
      "url": "https://www.mailjunky.ai",
      "email": "support@mailjunky.ai"
    }
  },
  "x-scalar-sdk-installation": [
    {
      "lang": "Node",
      "label": "MailJunky SDK",
      "description": "Install the official **MailJunky SDK** from npm:",
      "source": "npm install @mailjunky/sdk"
    },
    {
      "lang": "JavaScript",
      "label": "Browser SDK",
      "description": "For browser-side event tracking:",
      "source": "npm install @mailjunky/sdk"
    }
  ],
  "servers": [
    {
      "url": "https://www.mailjunky.ai/api/v1",
      "description": "Production"
    }
  ],
  "security": [
    {
      "bearerAuth": []
    }
  ],
  "tags": [
    {
      "name": "Emails",
      "description": "Send transactional emails via API"
    },
    {
      "name": "Contacts",
      "description": "Manage your contact lists and subscriber data"
    },
    {
      "name": "Events",
      "description": "Track user behavior and custom events"
    },
    {
      "name": "Analytics",
      "description": "Email and event statistics"
    },
    {
      "name": "Auth",
      "description": "API key validation"
    },
    {
      "name": "Health",
      "description": "Service health checks"
    }
  ],
  "paths": {
    "/health": {
      "get": {
        "operationId": "getHealth",
        "summary": "Health check",
        "description": "Check the health of the API and its dependencies. No authentication required.",
        "tags": ["Health"],
        "security": [],
        "responses": {
          "200": {
            "description": "Service health status",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "string",
                      "enum": ["healthy", "degraded"]
                    },
                    "timestamp": {
                      "type": "string",
                      "format": "date-time"
                    },
                    "version": {
                      "type": "string"
                    },
                    "uptime": {
                      "type": "number",
                      "description": "Uptime in seconds"
                    },
                    "checks": {
                      "type": "object",
                      "properties": {
                        "database": {
                          "type": "object",
                          "properties": {
                            "status": {
                              "type": "string",
                              "enum": ["ok", "error"]
                            },
                            "latency_ms": {
                              "type": "number"
                            }
                          }
                        }
                      }
                    },
                    "latency_ms": {
                      "type": "number"
                    }
                  }
                },
                "example": {
                  "status": "healthy",
                  "timestamp": "2026-02-16T12:00:00.000Z",
                  "version": "1.0.0",
                  "uptime": 86400,
                  "checks": {
                    "database": {
                      "status": "ok",
                      "latency_ms": 2
                    }
                  },
                  "latency_ms": 3
                }
              }
            }
          }
        }
      }
    },
    "/auth/validate": {
      "get": {
        "operationId": "validateApiKey",
        "summary": "Validate API key",
        "description": "Validate your API key and retrieve the associated team ID. Useful for verifying that your key is correctly configured.",
        "tags": ["Auth"],
        "responses": {
          "200": {
            "description": "API key is valid",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "valid": {
                      "type": "boolean",
                      "const": true
                    },
                    "team_id": {
                      "type": "string"
                    }
                  }
                },
                "example": {
                  "valid": true,
                  "team_id": "team_abc123"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/emails/send": {
      "post": {
        "operationId": "sendEmail",
        "summary": "Send an email",
        "description": "Send a single transactional email. The email is queued for async delivery and returns immediately with a `queued` status.\n\n**Rate limit:** 60 requests/minute\n\n**Permissions required:** `email:send`\n\n**Note:** Free plan can only send to team members. Domain must be verified.",
        "tags": ["Emails"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst result = await mailjunky.emails.send({\n  from: 'hello@yourapp.com',\n  to: 'user@example.com',\n  subject: 'Welcome aboard!',\n  html: '<h1>Welcome</h1><p>Thanks for signing up.</p>',\n  tags: [{ name: 'category', value: 'onboarding' }]\n})\n\nconsole.log(result.id, result.message_id)"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SendEmailRequest"
              },
              "example": {
                "from": "hello@yourapp.com",
                "to": "user@example.com",
                "subject": "Welcome aboard!",
                "html": "<h1>Welcome</h1><p>Thanks for signing up.</p>",
                "tags": [
                  {
                    "name": "category",
                    "value": "onboarding"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Email queued for delivery",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "id": {
                      "type": "string",
                      "description": "Email record ID"
                    },
                    "message_id": {
                      "type": "string",
                      "description": "Unique message ID for tracking"
                    },
                    "status": {
                      "type": "string",
                      "const": "queued"
                    }
                  }
                },
                "example": {
                  "id": "cm1abc123",
                  "message_id": "<1708099200.abc123@yourapp.com>",
                  "status": "queued"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/emails/batch": {
      "post": {
        "operationId": "sendBatchEmails",
        "summary": "Send batch emails",
        "description": "Send up to 100 personalized emails in a single request. Each email can have a different recipient, subject, and body. Returns per-email results.\n\n**Rate limit:** 10 requests/minute\n\n**Permissions required:** `email:send`",
        "tags": ["Emails"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst result = await mailjunky.emails.sendBatch([\n  {\n    from: 'hello@yourapp.com',\n    to: 'alice@example.com',\n    subject: 'Your order shipped!',\n    html: '<h1>Order #001</h1>'\n  },\n  {\n    from: 'hello@yourapp.com',\n    to: 'bob@example.com',\n    subject: 'Your order shipped!',\n    html: '<h1>Order #002</h1>'\n  }\n])\n\nconsole.log(result.results)"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BatchEmailRequest"
              },
              "example": {
                "emails": [
                  {
                    "from": "hello@yourapp.com",
                    "to": "alice@example.com",
                    "subject": "Your order shipped!",
                    "html": "<h1>Order #001</h1><p>Your package is on its way.</p>"
                  },
                  {
                    "from": "hello@yourapp.com",
                    "to": "bob@example.com",
                    "subject": "Your order shipped!",
                    "html": "<h1>Order #002</h1><p>Your package is on its way.</p>"
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "results": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "email": {
                            "type": "string",
                            "description": "Recipient email address"
                          },
                          "id": {
                            "type": "string",
                            "description": "Email record ID (if sent)"
                          },
                          "message_id": {
                            "type": "string",
                            "description": "Message ID (if sent)"
                          },
                          "status": {
                            "type": "string",
                            "enum": ["sent", "failed"]
                          },
                          "error": {
                            "type": "string",
                            "description": "Error message (if failed)"
                          }
                        }
                      }
                    }
                  }
                },
                "example": {
                  "results": [
                    {
                      "email": "alice@example.com",
                      "id": "cm1abc123",
                      "message_id": "<1708099200.abc123@yourapp.com>",
                      "status": "sent"
                    },
                    {
                      "email": "bob@example.com",
                      "id": "cm1abc124",
                      "message_id": "<1708099200.abc124@yourapp.com>",
                      "status": "sent"
                    }
                  ]
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/contacts": {
      "get": {
        "operationId": "listContacts",
        "summary": "List contacts",
        "description": "Retrieve a paginated list of contacts. Supports filtering by email, tag, and status.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `analytics:read`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst { data, pagination } = await mailjunky.contacts.list({\n  page: 1,\n  limit: 25,\n  status: 'active',\n  tag: 'customer'\n})\n\nconsole.log(`${pagination.total} contacts found`)\ndata.forEach(c => console.log(c.email))"
          }
        ],
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "default": 1
            },
            "description": "Page number"
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 100,
              "default": 25
            },
            "description": "Results per page (max 100)"
          },
          {
            "name": "email",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Filter by email (case-insensitive search)"
          },
          {
            "name": "tag",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "Filter by tag (exact match)"
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": ["ACTIVE", "UNSUBSCRIBED", "BOUNCED", "COMPLAINED"]
            },
            "description": "Filter by contact status"
          }
        ],
        "responses": {
          "200": {
            "description": "Paginated list of contacts",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Contact"
                      }
                    },
                    "pagination": {
                      "$ref": "#/components/schemas/Pagination"
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      },
      "post": {
        "operationId": "createContact",
        "summary": "Create or update a contact",
        "description": "Create a new contact or update an existing one (upsert by email). If a contact with the same email already exists, it will be updated.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `analytics:read`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst contact = await mailjunky.contacts.create({\n  email: 'user@example.com',\n  first_name: 'Alice',\n  last_name: 'Smith',\n  tags: ['customer', 'premium'],\n  properties: { plan: 'pro', company: 'Acme Inc' }\n})\n\nconsole.log(contact.id)"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateContactRequest"
              },
              "example": {
                "email": "user@example.com",
                "first_name": "Alice",
                "last_name": "Smith",
                "tags": ["customer", "premium"],
                "properties": {
                  "plan": "pro",
                  "company": "Acme Inc"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Contact created or updated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ContactBasic"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/contacts/{id}": {
      "get": {
        "operationId": "getContact",
        "summary": "Get a contact",
        "description": "Retrieve a single contact by ID with full engagement statistics.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `analytics:read`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst contact = await mailjunky.contacts.get('contact_id')\n\nconsole.log(contact.email, contact.status)\nconsole.log(`Sent: ${contact.emails_sent}, Opened: ${contact.emails_opened}`)"
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Contact ID"
          }
        ],
        "responses": {
          "200": {
            "description": "Contact details",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Contact"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "patch": {
        "operationId": "updateContact",
        "summary": "Update a contact",
        "description": "Update an existing contact's fields. Only provided fields will be updated.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `contacts:write`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst updated = await mailjunky.contacts.update('contact_id', {\n  first_name: 'Alice',\n  tags: ['customer', 'vip'],\n  properties: { plan: 'enterprise' }\n})\n\nconsole.log(updated.email, updated.tags)"
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Contact ID"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/UpdateContactRequest"
              },
              "example": {
                "first_name": "Alice",
                "tags": ["customer", "vip"],
                "properties": {
                  "plan": "enterprise"
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Contact updated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ContactBasic"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "description": "A contact with this email already exists",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      },
      "delete": {
        "operationId": "deleteContact",
        "summary": "Delete a contact",
        "description": "Permanently delete a contact by ID.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `contacts:write`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nawait mailjunky.contacts.delete('contact_id')\nconsole.log('Contact deleted')"
          }
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            },
            "description": "Contact ID"
          }
        ],
        "responses": {
          "200": {
            "description": "Contact deleted",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "deleted": {
                      "type": "boolean",
                      "const": true
                    }
                  }
                },
                "example": {
                  "deleted": true
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/contacts/batch": {
      "post": {
        "operationId": "batchImportContacts",
        "summary": "Batch import contacts",
        "description": "Import up to 1,000 contacts in a single request. Contacts are upserted by email -- existing contacts will be updated, new ones created.\n\n**Rate limit:** 100 requests/minute\n\n**Permissions required:** `analytics:read`",
        "tags": ["Contacts"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nconst result = await mailjunky.contacts.batch({\n  contacts: [\n    { email: 'alice@example.com', first_name: 'Alice', tags: ['customer'] },\n    { email: 'bob@example.com', first_name: 'Bob', tags: ['lead'] }\n  ]\n})\n\nconsole.log(`Created: ${result.created}, Updated: ${result.updated}`)"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BatchContactRequest"
              },
              "example": {
                "contacts": [
                  {
                    "email": "alice@example.com",
                    "first_name": "Alice",
                    "tags": ["customer"]
                  },
                  {
                    "email": "bob@example.com",
                    "first_name": "Bob",
                    "tags": ["lead"]
                  }
                ]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Batch import results",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "created": {
                      "type": "integer",
                      "description": "Number of new contacts created"
                    },
                    "updated": {
                      "type": "integer",
                      "description": "Number of existing contacts updated"
                    },
                    "failed": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "email": {
                            "type": "string"
                          },
                          "error": {
                            "type": "string"
                          }
                        }
                      },
                      "description": "Contacts that failed to import"
                    }
                  }
                },
                "example": {
                  "created": 1,
                  "updated": 1,
                  "failed": []
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/events/track": {
      "post": {
        "operationId": "trackEvent",
        "summary": "Track an event",
        "description": "Track a custom user event. Events are queued for async processing. Supports both private (`mj_live_*`) and public (`mj_pub_*`) API keys.\n\nPublic keys can also be passed via `X-API-Key` header or `?key=` query param (for `sendBeacon`).\n\nCORS is enabled for browser-based tracking.\n\n**Permissions required:** `events:track`\n\n**Monthly quotas:** Free: 2,000 | Hobby: 5,000 | Starter: 50,000 | Pro: 250,000",
        "tags": ["Events"],
        "x-codeSamples": [
          {
            "lang": "JavaScript",
            "label": "MailJunky SDK",
            "source": "import { MailJunky } from '@mailjunky/sdk'\n\nconst mailjunky = new MailJunky({ apiKey: 'mj_live_xxx' })\n\nawait mailjunky.events.track({\n  event: 'purchase_completed',\n  user: 'user@example.com',\n  properties: {\n    orderId: 'order_456',\n    total: 99.99,\n    items: 3\n  }\n})"
          },
          {
            "lang": "JavaScript",
            "label": "Browser SDK",
            "source": "import { initMailJunky } from '@mailjunky/sdk/browser'\n\nconst mj = initMailJunky({\n  apiKey: 'mj_pub_your_public_key',\n  autoTrack: { pageViews: true, buttonClicks: true }\n})\n\nmj.identify('user@example.com')\n\nmj.track({\n  event: 'purchase_completed',\n  properties: { orderId: 'order_456', total: 99.99 }\n})"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/TrackEventRequest"
              },
              "example": {
                "event": "purchase_completed",
                "user": "user@example.com",
                "properties": {
                  "orderId": "order_456",
                  "total": 99.99,
                  "items": 3
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Event queued for processing",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "success": {
                      "type": "boolean",
                      "const": true
                    },
                    "queued": {
                      "type": "boolean",
                      "const": true
                    }
                  }
                },
                "example": {
                  "success": true,
                  "queued": true
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/ValidationError"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/events/stats": {
      "get": {
        "operationId": "getEventStats",
        "summary": "Get event statistics",
        "description": "Retrieve aggregate statistics about your tracked events including totals, unique users, and event type breakdowns.\n\n**Rate limit:** 100 requests/minute",
        "tags": ["Events"],
        "responses": {
          "200": {
            "description": "Event statistics",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "total": {
                      "type": "integer",
                      "description": "Total events tracked"
                    },
                    "last_24_hours": {
                      "type": "integer",
                      "description": "Events in the last 24 hours"
                    },
                    "last_7_days": {
                      "type": "integer",
                      "description": "Events in the last 7 days"
                    },
                    "unique_users": {
                      "type": "integer",
                      "description": "Number of unique users tracked"
                    },
                    "event_types": {
                      "type": "array",
                      "items": {
                        "type": "object",
                        "properties": {
                          "name": {
                            "type": "string"
                          },
                          "count": {
                            "type": "integer"
                          }
                        }
                      },
                      "description": "Breakdown by event type"
                    }
                  }
                },
                "example": {
                  "total": 15420,
                  "last_24_hours": 523,
                  "last_7_days": 3847,
                  "unique_users": 892,
                  "event_types": [
                    { "name": "page_view", "count": 8230 },
                    { "name": "purchase_completed", "count": 1245 },
                    { "name": "cart_added", "count": 3102 }
                  ]
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/stats": {
      "get": {
        "operationId": "getEmailStats",
        "summary": "Get email statistics",
        "description": "Retrieve email delivery statistics including rates, health status, and current period usage.\n\n**Rate limit:** 100 requests/minute",
        "tags": ["Analytics"],
        "parameters": [
          {
            "name": "range",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": ["24h", "7d", "30d", "90d"],
              "default": "7d"
            },
            "description": "Time range for statistics"
          }
        ],
        "responses": {
          "200": {
            "description": "Email statistics",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "range": {
                      "type": "string"
                    },
                    "current_period": {
                      "type": "object",
                      "properties": {
                        "period": {
                          "type": "string",
                          "description": "Current billing period (YYYY-MM)"
                        },
                        "emails_sent": {
                          "type": "integer"
                        },
                        "limit": {
                          "type": "integer"
                        },
                        "percentage_used": {
                          "type": "number"
                        }
                      }
                    },
                    "last_30_days": {
                      "type": "object",
                      "properties": {
                        "total": { "type": "integer" },
                        "sent": { "type": "integer" },
                        "delivered": { "type": "integer" },
                        "bounced": { "type": "integer" },
                        "complained": { "type": "integer" },
                        "failed": { "type": "integer" }
                      }
                    },
                    "rates": {
                      "type": "object",
                      "properties": {
                        "delivery": { "type": "string", "description": "Delivery rate as decimal" },
                        "bounce": { "type": "string", "description": "Bounce rate as decimal" },
                        "complaint": { "type": "string", "description": "Complaint rate as decimal" }
                      }
                    },
                    "health": {
                      "type": "object",
                      "properties": {
                        "bounce_status": {
                          "type": "string",
                          "enum": ["healthy", "warning", "critical"],
                          "description": "healthy (<3%), warning (3-5%), critical (>=5%)"
                        },
                        "complaint_status": {
                          "type": "string",
                          "enum": ["healthy", "warning", "critical"],
                          "description": "healthy (<0.05%), warning (0.05-0.1%), critical (>=0.1%)"
                        }
                      }
                    }
                  }
                },
                "example": {
                  "range": "7d",
                  "current_period": {
                    "period": "2026-02",
                    "emails_sent": 4523,
                    "limit": 10000,
                    "percentage_used": 45.23
                  },
                  "last_30_days": {
                    "total": 12450,
                    "sent": 12300,
                    "delivered": 12250,
                    "bounced": 45,
                    "complained": 5,
                    "failed": 150
                  },
                  "rates": {
                    "delivery": "0.996",
                    "bounce": "0.004",
                    "complaint": "0.0004"
                  },
                  "health": {
                    "bounce_status": "healthy",
                    "complaint_status": "healthy"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {
        "type": "http",
        "scheme": "bearer",
        "description": "API key authentication. Use your `mj_live_*` or `mj_pub_*` key as the bearer token."
      },
      "apiKeyHeader": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Key",
        "description": "Alternative: pass your API key via the X-API-Key header."
      }
    },
    "schemas": {
      "SendEmailRequest": {
        "type": "object",
        "required": ["from", "to", "subject"],
        "properties": {
          "from": {
            "oneOf": [
              { "type": "string", "format": "email" },
              {
                "type": "object",
                "required": ["email"],
                "properties": {
                  "email": { "type": "string", "format": "email" },
                  "name": { "type": "string" }
                }
              }
            ],
            "description": "Sender email address or object with email and display name"
          },
          "to": {
            "oneOf": [
              { "type": "string", "format": "email" },
              { "type": "array", "items": { "type": "string", "format": "email" } }
            ],
            "description": "Recipient email address(es)"
          },
          "cc": {
            "oneOf": [
              { "type": "string", "format": "email" },
              { "type": "array", "items": { "type": "string", "format": "email" } }
            ],
            "description": "CC recipient(s)"
          },
          "bcc": {
            "oneOf": [
              { "type": "string", "format": "email" },
              { "type": "array", "items": { "type": "string", "format": "email" } }
            ],
            "description": "BCC recipient(s)"
          },
          "subject": {
            "type": "string",
            "minLength": 1,
            "maxLength": 998,
            "description": "Email subject line"
          },
          "html": {
            "type": "string",
            "description": "HTML body. Either `html` or `text` is required."
          },
          "text": {
            "type": "string",
            "description": "Plain text body. Either `html` or `text` is required."
          },
          "reply_to": {
            "oneOf": [
              { "type": "string", "format": "email" },
              { "type": "array", "items": { "type": "string", "format": "email" } }
            ],
            "description": "Reply-to address(es)"
          },
          "tags": {
            "type": "array",
            "items": {
              "type": "object",
              "required": ["name", "value"],
              "properties": {
                "name": { "type": "string" },
                "value": { "type": "string" }
              }
            },
            "description": "Tags for categorization and filtering"
          },
          "metadata": {
            "type": "object",
            "additionalProperties": { "type": "string" },
            "description": "Custom key-value metadata"
          },
          "category": {
            "type": "string",
            "enum": ["TRANSACTIONAL", "WELCOME", "MARKETING", "NOTIFICATION", "NEWSLETTER", "CUSTOM"],
            "description": "Email category for analytics"
          }
        }
      },
      "BatchEmailRequest": {
        "type": "object",
        "required": ["emails"],
        "properties": {
          "emails": {
            "type": "array",
            "minItems": 1,
            "maxItems": 100,
            "items": {
              "type": "object",
              "required": ["from", "to", "subject"],
              "properties": {
                "from": { "type": "string", "format": "email" },
                "to": {
                  "oneOf": [
                    { "type": "string", "format": "email" },
                    { "type": "array", "items": { "type": "string", "format": "email" } }
                  ]
                },
                "cc": {
                  "oneOf": [
                    { "type": "string", "format": "email" },
                    { "type": "array", "items": { "type": "string", "format": "email" } }
                  ]
                },
                "bcc": {
                  "oneOf": [
                    { "type": "string", "format": "email" },
                    { "type": "array", "items": { "type": "string", "format": "email" } }
                  ]
                },
                "subject": { "type": "string", "minLength": 1, "maxLength": 998 },
                "html": { "type": "string" },
                "text": { "type": "string" },
                "reply_to": {
                  "oneOf": [
                    { "type": "string", "format": "email" },
                    { "type": "array", "items": { "type": "string", "format": "email" } }
                  ]
                },
                "tags": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "name": { "type": "string" },
                      "value": { "type": "string" }
                    }
                  }
                },
                "metadata": {
                  "type": "object",
                  "additionalProperties": { "type": "string" }
                }
              }
            },
            "description": "Array of 1-100 emails to send"
          }
        }
      },
      "CreateContactRequest": {
        "type": "object",
        "required": ["email"],
        "properties": {
          "email": { "type": "string", "format": "email" },
          "first_name": { "type": "string" },
          "last_name": { "type": "string" },
          "phone": { "type": "string" },
          "properties": {
            "type": "object",
            "additionalProperties": true,
            "description": "Custom properties"
          },
          "tags": {
            "type": "array",
            "items": { "type": "string" },
            "description": "Tags for segmentation"
          }
        }
      },
      "UpdateContactRequest": {
        "type": "object",
        "properties": {
          "email": { "type": "string", "format": "email" },
          "first_name": { "type": "string" },
          "last_name": { "type": "string" },
          "phone": { "type": "string" },
          "properties": {
            "type": "object",
            "additionalProperties": true
          },
          "tags": {
            "type": "array",
            "items": { "type": "string" }
          },
          "status": {
            "type": "string",
            "enum": ["active", "unsubscribed"]
          }
        }
      },
      "BatchContactRequest": {
        "type": "object",
        "required": ["contacts"],
        "properties": {
          "contacts": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1000,
            "items": {
              "$ref": "#/components/schemas/CreateContactRequest"
            },
            "description": "Array of 1-1,000 contacts to import"
          }
        }
      },
      "TrackEventRequest": {
        "type": "object",
        "required": ["event"],
        "properties": {
          "event": {
            "type": "string",
            "minLength": 1,
            "maxLength": 255,
            "description": "Event name (e.g. 'purchase_completed', 'page_viewed')"
          },
          "user": {
            "type": "string",
            "description": "User identifier (email, user ID, etc.)"
          },
          "properties": {
            "type": "object",
            "additionalProperties": true,
            "description": "Custom event properties"
          },
          "session_id": {
            "type": "string",
            "description": "Session identifier for grouping events"
          },
          "timestamp": {
            "type": "string",
            "format": "date-time",
            "description": "Event timestamp (defaults to now)"
          },
          "context": {
            "type": "object",
            "additionalProperties": true,
            "description": "Browser/device context (url, user_agent, etc.)"
          }
        }
      },
      "Contact": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "email": { "type": "string", "format": "email" },
          "first_name": { "type": "string", "nullable": true },
          "last_name": { "type": "string", "nullable": true },
          "phone": { "type": "string", "nullable": true },
          "properties": { "type": "object", "additionalProperties": true },
          "tags": { "type": "array", "items": { "type": "string" } },
          "status": { "type": "string", "enum": ["active", "unsubscribed", "bounced", "complained"] },
          "emails_sent": { "type": "integer" },
          "emails_opened": { "type": "integer" },
          "emails_clicked": { "type": "integer" },
          "last_email_sent_at": { "type": "string", "format": "date-time", "nullable": true },
          "last_opened_at": { "type": "string", "format": "date-time", "nullable": true },
          "unsubscribed_at": { "type": "string", "format": "date-time", "nullable": true },
          "created_at": { "type": "string", "format": "date-time" },
          "updated_at": { "type": "string", "format": "date-time" }
        }
      },
      "ContactBasic": {
        "type": "object",
        "properties": {
          "id": { "type": "string" },
          "email": { "type": "string", "format": "email" },
          "first_name": { "type": "string", "nullable": true },
          "last_name": { "type": "string", "nullable": true },
          "phone": { "type": "string", "nullable": true },
          "properties": { "type": "object", "additionalProperties": true },
          "tags": { "type": "array", "items": { "type": "string" } },
          "status": { "type": "string" },
          "created_at": { "type": "string", "format": "date-time" }
        }
      },
      "Pagination": {
        "type": "object",
        "properties": {
          "page": { "type": "integer" },
          "limit": { "type": "integer" },
          "total": { "type": "integer" },
          "has_more": { "type": "boolean" }
        }
      },
      "Error": {
        "type": "object",
        "properties": {
          "statusCode": { "type": "integer" },
          "statusMessage": { "type": "string" },
          "message": { "type": "string" },
          "data": {
            "type": "object",
            "description": "Additional error details (e.g. validation errors)",
            "properties": {
              "fieldErrors": {
                "type": "object",
                "additionalProperties": {
                  "type": "array",
                  "items": { "type": "string" }
                }
              },
              "formErrors": {
                "type": "array",
                "items": { "type": "string" }
              }
            }
          }
        }
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Invalid or missing API key",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "statusCode": 401,
              "statusMessage": "Unauthorized",
              "message": "Invalid or missing API key"
            }
          }
        }
      },
      "Forbidden": {
        "description": "Insufficient permissions",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "statusCode": 403,
              "statusMessage": "Forbidden",
              "message": "This API key does not have the required permission"
            }
          }
        }
      },
      "NotFound": {
        "description": "Resource not found",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "statusCode": 404,
              "statusMessage": "Not Found",
              "message": "Resource not found"
            }
          }
        }
      },
      "ValidationError": {
        "description": "Request validation failed",
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "statusCode": 400,
              "message": "Validation failed",
              "data": {
                "fieldErrors": {
                  "to": ["Invalid email"]
                },
                "formErrors": []
              }
            }
          }
        }
      },
      "RateLimited": {
        "description": "Rate limit or quota exceeded",
        "headers": {
          "Retry-After": {
            "schema": { "type": "integer" },
            "description": "Seconds to wait before retrying"
          },
          "X-RateLimit-Limit": {
            "schema": { "type": "integer" }
          },
          "X-RateLimit-Remaining": {
            "schema": { "type": "integer" }
          },
          "X-RateLimit-Reset": {
            "schema": { "type": "integer" }
          }
        },
        "content": {
          "application/json": {
            "schema": { "$ref": "#/components/schemas/Error" },
            "example": {
              "statusCode": 429,
              "message": "Rate limit exceeded. Please slow down."
            }
          }
        }
      }
    }
  }
}
