{
  "openapi": "3.1.0",
  "info": {
    "title": "LanguSMS API",
    "version": "1.0.0",
    "summary": "Bulk SMS and messaging API for Tanzania",
    "description": "Send transactional SMS, OTPs and bulk campaigns to Tanzanian mobile numbers. Track delivery, manage contacts and sender IDs, and receive signed webhook events.",
    "termsOfService": "https://langusms.co.tz/",
    "contact": {
      "name": "LanguSMS Support",
      "email": "support@langusms.co.tz",
      "url": "https://langusms.co.tz/docs"
    }
  },
  "jsonSchemaDialect": "https://json-schema.org/draft/2020-12/schema",
  "servers": [
    {
      "url": "https://langusms.co.tz/v1",
      "description": "Production API in Tanzania"
    }
  ],
  "externalDocs": {
    "description": "LanguSMS developer documentation",
    "url": "https://langusms.co.tz/docs"
  },
  "tags": [
    {"name": "Messages", "description": "Send and track SMS messages."},
    {"name": "Account", "description": "Inspect account credit balance."},
    {"name": "Sender IDs", "description": "List and request branded sender IDs."},
    {"name": "Contacts", "description": "Manage recipients and contact groups."},
    {"name": "Webhooks", "description": "Manage signed delivery event destinations."},
    {"name": "Reports", "description": "Retrieve aggregated messaging statistics."}
  ],
  "security": [{"bearerAuth": []}, {"apiKeyHeader": []}],
  "paths": {
    "/": {
      "get": {
        "operationId": "discoverApi",
        "summary": "Discover the API",
        "description": "Returns the API version, documentation links and endpoint directory. No authentication is required.",
        "security": [],
        "responses": {
          "200": {
            "description": "API discovery document",
            "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}
          }
        }
      }
    },
    "/messages": {
      "post": {
        "operationId": "sendMessage",
        "tags": ["Messages"],
        "summary": "Send one SMS",
        "description": "Queues one SMS to a Tanzanian mobile number. Use an Idempotency-Key when safely retrying a request.",
        "parameters": [{"$ref": "#/components/parameters/IdempotencyKey"}],
        "requestBody": {
          "required": true,
          "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SendMessageRequest"}}}
        },
        "responses": {
          "202": {"description": "Message accepted", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/MessageEnvelope"}}}},
          "400": {"$ref": "#/components/responses/BadRequest"},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "429": {"$ref": "#/components/responses/RateLimited"}
        }
      },
      "get": {
        "operationId": "listMessages",
        "tags": ["Messages"],
        "summary": "List messages",
        "parameters": [
          {"name": "status", "in": "query", "schema": {"$ref": "#/components/schemas/MessageStatus"}},
          {"name": "to", "in": "query", "description": "Filter by recipient phone number.", "schema": {"type": "string"}},
          {"name": "reference", "in": "query", "description": "Match a client reference exactly.", "schema": {"type": "string"}},
          {"name": "sender_id", "in": "query", "schema": {"type": "string"}},
          {"name": "from_date", "in": "query", "description": "Start date in YYYY-MM-DD format.", "schema": {"type": "string", "format": "date"}},
          {"name": "to_date", "in": "query", "description": "End date in YYYY-MM-DD format.", "schema": {"type": "string", "format": "date"}},
          {"name": "page", "in": "query", "schema": {"type": "integer", "minimum": 1, "default": 1}},
          {"name": "per_page", "in": "query", "schema": {"type": "integer", "minimum": 1, "maximum": 100, "default": 20}}
        ],
        "responses": {
          "200": {"description": "Paginated messages", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    },
    "/messages/bulk": {
      "post": {
        "operationId": "sendBulkMessages",
        "tags": ["Messages"],
        "summary": "Send SMS in bulk",
        "description": "Queues a bulk SMS request. The account limit is returned by the documentation and service discovery endpoint.",
        "parameters": [{"$ref": "#/components/parameters/IdempotencyKey"}],
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SendBulkRequest"}}}},
        "responses": {
          "202": {"description": "Bulk request accepted", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}},
          "400": {"$ref": "#/components/responses/BadRequest"},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    },
    "/messages/{id}": {
      "get": {
        "operationId": "getMessage",
        "tags": ["Messages"],
        "summary": "Retrieve one message",
        "parameters": [{"$ref": "#/components/parameters/MessageId"}],
        "responses": {
          "200": {"description": "Message details", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/MessageEnvelope"}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "404": {"$ref": "#/components/responses/NotFound"}
        }
      }
    },
    "/messages/{id}/events": {
      "get": {
        "operationId": "listMessageEvents",
        "tags": ["Messages"],
        "summary": "Get a message delivery timeline",
        "parameters": [{"$ref": "#/components/parameters/MessageId"}],
        "responses": {
          "200": {"description": "Chronological message events", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"},
          "404": {"$ref": "#/components/responses/NotFound"}
        }
      }
    },
    "/messages/{id}/cancel": {
      "post": {
        "operationId": "cancelMessage",
        "tags": ["Messages"],
        "summary": "Cancel a scheduled message",
        "parameters": [{"$ref": "#/components/parameters/MessageId"}],
        "responses": {
          "200": {"description": "Message cancelled", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/MessageEnvelope"}}}},
          "400": {"$ref": "#/components/responses/BadRequest"},
          "404": {"$ref": "#/components/responses/NotFound"}
        }
      }
    },
    "/balance": {
      "get": {
        "operationId": "getBalance",
        "tags": ["Account"],
        "summary": "Get SMS credit balance",
        "responses": {
          "200": {"description": "Current and reserved credits", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}},
          "401": {"$ref": "#/components/responses/Unauthorized"}
        }
      }
    },
    "/sender-ids": {
      "get": {
        "operationId": "listSenderIds",
        "tags": ["Sender IDs"],
        "summary": "List sender IDs",
        "responses": {"200": {"description": "Sender IDs and approval states", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}}
      },
      "post": {
        "operationId": "requestSenderId",
        "tags": ["Sender IDs"],
        "summary": "Request a sender ID",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["sender_id", "use_case"], "properties": {"sender_id": {"type": "string", "maxLength": 11, "examples": ["ACMELTD"]}, "use_case": {"type": "string", "minLength": 10, "maxLength": 500}, "sample_message": {"type": "string", "maxLength": 500}}}}}},
        "responses": {
          "202": {"description": "Sender ID request accepted for approval", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}},
          "400": {"$ref": "#/components/responses/BadRequest"}
        }
      }
    },
    "/contacts": {
      "get": {
        "operationId": "listContacts",
        "tags": ["Contacts"],
        "summary": "List contacts",
        "parameters": [{"name": "search", "in": "query", "schema": {"type": "string"}}, {"name": "page", "in": "query", "schema": {"type": "integer", "minimum": 1}}],
        "responses": {"200": {"description": "Paginated contacts", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}}
      },
      "post": {
        "operationId": "createContact",
        "tags": ["Contacts"],
        "summary": "Create a contact",
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ContactInput"}}}},
        "responses": {"201": {"description": "Contact created", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "400": {"$ref": "#/components/responses/BadRequest"}}
      }
    },
    "/contacts/{id}": {
      "parameters": [{"$ref": "#/components/parameters/ResourceId"}],
      "get": {"operationId": "getContact", "tags": ["Contacts"], "summary": "Retrieve a contact", "responses": {"200": {"description": "Contact", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "patch": {"operationId": "updateContact", "tags": ["Contacts"], "summary": "Update a contact", "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ContactInput"}}}}, "responses": {"200": {"description": "Contact updated", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "put": {"operationId": "replaceContact", "tags": ["Contacts"], "summary": "Replace a contact", "requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ContactInput"}}}}, "responses": {"200": {"description": "Contact updated", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "delete": {"operationId": "deleteContact", "tags": ["Contacts"], "summary": "Delete a contact", "responses": {"200": {"description": "Contact deleted", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}}
    },
    "/groups": {
      "get": {"operationId": "listGroups", "tags": ["Contacts"], "summary": "List contact groups", "responses": {"200": {"description": "Contact groups", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}}},
      "post": {"operationId": "createGroup", "tags": ["Contacts"], "summary": "Create a contact group", "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["name"], "properties": {"name": {"type": "string", "maxLength": 120}}}}}}, "responses": {"201": {"description": "Group created", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "400": {"$ref": "#/components/responses/BadRequest"}}}
    },
    "/groups/{id}/contacts": {
      "get": {
        "operationId": "listGroupContacts",
        "tags": ["Contacts"],
        "summary": "List contacts in a group",
        "parameters": [{"$ref": "#/components/parameters/ResourceId"}],
        "responses": {"200": {"description": "Paginated group contacts", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}
      },
      "post": {
        "operationId": "addContactsToGroup",
        "tags": ["Contacts"],
        "summary": "Add contacts to a group",
        "parameters": [{"$ref": "#/components/parameters/ResourceId"}],
        "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["contacts"], "properties": {"contacts": {"type": "array", "items": {"type": "string", "description": "Contact public ID or phone number"}, "minItems": 1}}}}}},
        "responses": {"200": {"description": "Contacts added", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}
      }
    },
    "/groups/{id}": {
      "parameters": [{"$ref": "#/components/parameters/ResourceId"}],
      "get": {"operationId": "getGroup", "tags": ["Contacts"], "summary": "Retrieve a contact group", "responses": {"200": {"description": "Contact group", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "delete": {"operationId": "deleteGroup", "tags": ["Contacts"], "summary": "Delete a contact group", "responses": {"200": {"description": "Group deleted", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}}
    },
    "/webhooks": {
      "get": {"operationId": "listWebhooks", "tags": ["Webhooks"], "summary": "List webhook endpoints", "responses": {"200": {"description": "Webhook endpoints", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}}},
      "post": {"operationId": "createWebhook", "tags": ["Webhooks"], "summary": "Create a signed webhook endpoint", "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "required": ["url"], "properties": {"url": {"type": "string", "format": "uri", "examples": ["https://example.co.tz/webhooks/langusms"]}, "events": {"type": "array", "items": {"type": "string"}}}}}}}, "responses": {"201": {"description": "Webhook created; the signing secret is returned once", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "400": {"$ref": "#/components/responses/BadRequest"}}}
    },
    "/webhooks/{id}": {
      "get": {"operationId": "getWebhook", "tags": ["Webhooks"], "summary": "Retrieve a webhook endpoint", "parameters": [{"$ref": "#/components/parameters/ResourceId"}], "responses": {"200": {"description": "Webhook endpoint", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "patch": {"operationId": "updateWebhook", "tags": ["Webhooks"], "summary": "Update a webhook endpoint", "parameters": [{"$ref": "#/components/parameters/ResourceId"}], "requestBody": {"required": true, "content": {"application/json": {"schema": {"type": "object", "properties": {"url": {"type": "string", "format": "uri"}, "events": {"type": "array", "items": {"type": "string"}}, "description": {"type": "string"}, "is_active": {"type": "boolean"}}}}}}, "responses": {"200": {"description": "Webhook updated", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}},
      "delete": {
        "operationId": "deleteWebhook",
        "tags": ["Webhooks"],
        "summary": "Delete a webhook endpoint",
        "parameters": [{"$ref": "#/components/parameters/ResourceId"}],
        "responses": {"200": {"description": "Webhook deleted", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "404": {"$ref": "#/components/responses/NotFound"}}
      }
    },
    "/webhooks/{id}/test": {
      "post": {"operationId": "testWebhook", "tags": ["Webhooks"], "summary": "Send a test webhook", "parameters": [{"$ref": "#/components/parameters/ResourceId"}], "responses": {"200": {"description": "Test delivered", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "502": {"description": "Test delivery failed", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ErrorEnvelope"}}}}}}
    },
    "/reports/summary": {
      "get": {
        "operationId": "getReportSummary",
        "tags": ["Reports"],
        "summary": "Get delivery summary",
        "parameters": [
          {"name": "range", "in": "query", "schema": {"type": "string", "enum": ["today", "last_7", "last_30", "this_month", "last_month", "custom"], "default": "last_30"}},
          {"name": "from", "in": "query", "schema": {"type": "string", "format": "date"}},
          {"name": "to", "in": "query", "schema": {"type": "string", "format": "date"}}
        ],
        "responses": {"200": {"description": "Totals, daily series and per-network breakdown", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "400": {"$ref": "#/components/responses/BadRequest"}}
      }
    },
    "/ping": {
      "get": {"operationId": "ping", "tags": ["Account"], "summary": "Check authenticated API connectivity", "responses": {"200": {"description": "Pong, account ID and server time", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/SuccessEnvelope"}}}}, "401": {"$ref": "#/components/responses/Unauthorized"}}}
    }
  },
  "components": {
    "securitySchemes": {
      "bearerAuth": {"type": "http", "scheme": "bearer", "bearerFormat": "LanguSMS API key", "description": "Send a server-side API key as Authorization: Bearer sk_live_..."},
      "apiKeyHeader": {"type": "apiKey", "in": "header", "name": "X-Api-Key", "description": "Alternative API key header for server-to-server clients."}
    },
    "parameters": {
      "IdempotencyKey": {"name": "Idempotency-Key", "in": "header", "required": false, "description": "Unique retry key, up to 80 characters.", "schema": {"type": "string", "maxLength": 80}},
      "MessageId": {"name": "id", "in": "path", "required": true, "description": "Public message identifier.", "schema": {"type": "string", "pattern": "^msg_[A-Za-z0-9]+$"}},
      "ResourceId": {"name": "id", "in": "path", "required": true, "description": "Public resource identifier.", "schema": {"type": "string"}}
    },
    "responses": {
      "BadRequest": {"description": "Invalid request", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ErrorEnvelope"}}}},
      "Unauthorized": {"description": "Missing or invalid API key", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ErrorEnvelope"}}}},
      "NotFound": {"description": "Resource not found", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ErrorEnvelope"}}}},
      "RateLimited": {"description": "Account rate limit exceeded", "headers": {"Retry-After": {"schema": {"type": "integer"}}}, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/ErrorEnvelope"}}}}
    },
    "schemas": {
      "SuccessEnvelope": {
        "type": "object",
        "required": ["success", "data"],
        "properties": {"success": {"type": "boolean", "const": true}, "data": {"type": ["object", "array"]}, "meta": {"type": "object"}}
      },
      "ErrorEnvelope": {
        "type": "object",
        "required": ["success", "error"],
        "properties": {"success": {"type": "boolean", "const": false}, "error": {"type": "object", "required": ["code", "message"], "properties": {"code": {"type": "string"}, "message": {"type": "string"}, "details": {}}}}
      },
      "MessageStatus": {"type": "string", "enum": ["queued", "scheduled", "submitted", "delivered", "undelivered", "rejected", "failed", "expired", "cancelled"]},
      "Message": {
        "type": "object",
        "properties": {
          "id": {"type": "string", "examples": ["msg_9f2c41a8b7d0"]},
          "to": {"type": "string", "examples": ["255712345678"]},
          "message": {"type": "string"},
          "sender_id": {"type": "string", "examples": ["ACMELTD"]},
          "status": {"$ref": "#/components/schemas/MessageStatus"},
          "parts": {"type": "integer", "minimum": 1},
          "credits_charged": {"type": "integer", "minimum": 0},
          "created_at": {"type": "string", "format": "date-time"},
          "delivered_at": {"type": ["string", "null"], "format": "date-time"}
        }
      },
      "MessageEnvelope": {
        "type": "object",
        "required": ["success", "data"],
        "properties": {"success": {"type": "boolean", "const": true}, "data": {"$ref": "#/components/schemas/Message"}}
      },
      "SendMessageRequest": {
        "type": "object",
        "required": ["to", "message"],
        "properties": {
          "to": {"type": "string", "description": "Tanzanian number in local or +255 format.", "examples": ["0712345678"]},
          "message": {"type": "string", "minLength": 1, "maxLength": 1600, "examples": ["Your verification code is 4821"]},
          "sender_id": {"type": "string", "maxLength": 20, "examples": ["ACMELTD"]},
          "scheduled_at": {"type": "string", "format": "date-time"},
          "reference": {"type": "string", "maxLength": 120, "examples": ["order-4471"]},
          "callback_url": {"type": "string", "format": "uri", "pattern": "^https://"}
        }
      },
      "SendBulkRequest": {
        "type": "object",
        "required": ["messages"],
        "properties": {
          "messages": {"type": "array", "minItems": 1, "items": {"type": "object", "required": ["to"], "properties": {"to": {"type": "string", "examples": ["0712345678"]}, "message": {"type": "string", "minLength": 1, "maxLength": 1600}, "reference": {"type": "string", "maxLength": 120}}}},
          "message": {"type": "string", "minLength": 1, "maxLength": 1600, "description": "Shared body used by entries that omit message."},
          "sender_id": {"type": "string", "maxLength": 20},
          "scheduled_at": {"type": "string", "format": "date-time"},
          "callback_url": {"type": "string", "format": "uri", "pattern": "^https://"}
        }
      },
      "ContactInput": {
        "type": "object",
        "required": ["phone"],
        "properties": {"phone": {"type": "string", "examples": ["0712345678"]}, "first_name": {"type": "string", "maxLength": 120}, "last_name": {"type": "string", "maxLength": 120}, "email": {"type": "string", "format": "email"}, "attributes": {"type": "object", "additionalProperties": true}}
      }
    }
  }
}
