{
    "openapi": "3.1.0",
    "info": {
        "title": "Aqwa Whats API",
        "version": "1.0.0",
        "summary": "Send WhatsApp OTP codes and notifications from your own numbers.",
        "description": "Every response uses the same envelope: `{\"success\": true, \"data\": …}` or `{\"success\": false, \"error\": {\"code\", \"message\"}, \"request_id\"}`.\n\nError messages follow `Accept-Language` (`ar` by default, `en`). Every response carries `X-Request-Id`.\n\nCurrent limits: 60 requests/minute per key; OTP: 30s cooldown per recipient, 10 per 5 min, 50/day, expires after 300s in the queue; text: 20/hour per recipient."
    },
    "servers": [
        {
            "url": "https://whats.aqwasoft.com/api/v1"
        }
    ],
    "security": [
        {
            "bearerAuth": []
        }
    ],
    "paths": {
        "/messages": {
            "post": {
                "operationId": "sendMessage",
                "summary": "Send a message (queued)",
                "description": "Queues a WhatsApp message and returns immediately with `status: queued`. Track it with `GET /messages/{id}` or webhooks. `POST /send` is a legacy alias.",
                "parameters": [
                    {
                        "$ref": "#/components/parameters/IdempotencyKey"
                    },
                    {
                        "$ref": "#/components/parameters/RequestId"
                    }
                ],
                "requestBody": {
                    "required": true,
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/SendMessage"
                            },
                            "examples": {
                                "otp": {
                                    "summary": "OTP code",
                                    "value": {
                                        "to": "9665XXXXXXXX",
                                        "body": "رمز التحقق: 482913",
                                        "type": "otp"
                                    }
                                },
                                "text": {
                                    "summary": "Notification",
                                    "value": {
                                        "to": "9665XXXXXXXX",
                                        "body": "تم تأكيد طلبك رقم 1048",
                                        "type": "text"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "202": {
                        "description": "Accepted and queued. With a replayed Idempotency-Key the original response is returned with `Idempotent-Replayed: true`.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            },
                            "Idempotent-Replayed": {
                                "description": "Present (`true`) when this is a stored response to a repeated request.",
                                "schema": {
                                    "type": "string",
                                    "enum": [
                                        "true"
                                    ]
                                }
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/MessageResponse"
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Missing or invalid API key.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "missing_api_key",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "403": {
                        "description": "The account behind this key is suspended or not activated.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "account_inactive",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "409": {
                        "description": "Another request with the same Idempotency-Key is in progress.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "idempotency_in_progress",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "422": {
                        "description": "Validation failed (`error.fields`), or the Idempotency-Key was used for a different request.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "validation_error",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "429": {
                        "description": "A rate limit was hit. `error.limit` names it; wait `Retry-After` seconds.",
                        "headers": {
                            "Retry-After": {
                                "schema": {
                                    "type": "integer"
                                },
                                "description": "Seconds to wait."
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "rate_limited",
                                        "limit": "otp_cooldown",
                                        "message": "…",
                                        "retry_after": 42
                                    },
                                    "request_id": "req_…"
                                }
                            }
                        }
                    },
                    "500": {
                        "description": "Unexpected error. Retry; if it persists send us the request_id.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "server_error",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/account": {
            "get": {
                "operationId": "getAccount",
                "summary": "Account & connection check (no message sent)",
                "description": "Validates the key and returns the account, connected WhatsApp numbers and current limits. Use it for health probes: `ready: false` means no connected number, so sends will queue and may fail.",
                "responses": {
                    "200": {
                        "description": "Account status.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/AccountResponse"
                                },
                                "example": {
                                    "success": true,
                                    "data": {
                                        "account": {
                                            "type": "subscriber",
                                            "name": "متجر نخلة",
                                            "status": "active"
                                        },
                                        "key": {
                                            "name": "Production server",
                                            "prefix": "aqw_live_Ab12Cd",
                                            "last_used_at": "2026-09-27T10:15:01+03:00"
                                        },
                                        "numbers": {
                                            "connected": 1,
                                            "total": 1
                                        },
                                        "ready": true,
                                        "limits": {
                                            "requests_per_minute": 60,
                                            "otp_cooldown_seconds": 30
                                        },
                                        "api_version": "v1"
                                    }
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Missing or invalid API key.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "missing_api_key",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "403": {
                        "description": "The account behind this key is suspended or not activated.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "account_inactive",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    }
                }
            }
        },
        "/messages/{id}": {
            "get": {
                "operationId": "getMessage",
                "summary": "Get a message status",
                "parameters": [
                    {
                        "name": "id",
                        "in": "path",
                        "required": true,
                        "schema": {
                            "type": "string",
                            "pattern": "^msg_[0-9a-z]{26}$"
                        },
                        "example": "msg_01j9zq4k2r5t8y1u3i6o9p2a4s"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "The message.",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/MessageResponse"
                                }
                            }
                        }
                    },
                    "401": {
                        "description": "Missing or invalid API key.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "missing_api_key",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    },
                    "404": {
                        "description": "No such message for this key owner.",
                        "headers": {
                            "X-Request-Id": {
                                "$ref": "#/components/headers/RequestId"
                            }
                        },
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Error"
                                },
                                "example": {
                                    "success": false,
                                    "error": {
                                        "code": "not_found",
                                        "message": "…"
                                    },
                                    "request_id": "req_01j9zq4m8h2k7x3v5b6n1c0d9e"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "webhooks": {
        "messageStatus": {
            "post": {
                "summary": "Message status changed",
                "description": "Sent to your webhook URL (panel → Webhooks) for the events you selected: message.sent, message.delivered, message.read, message.failed, message.expired. Verify `X-Aqwa-Signature: t=<unix>,v1=<hex>` where v1 = HMAC-SHA256(secret, \"<t>.<raw body>\"), and reject timestamps older than 5 minutes. Return any 2xx within 10 s; otherwise we retry after 1m, 5m, 30m, 2h and 6h. The same event may arrive more than once — deduplicate by `id`.",
                "parameters": [
                    {
                        "name": "X-Aqwa-Event",
                        "in": "header",
                        "schema": {
                            "type": "string",
                            "enum": [
                                "message.sent",
                                "message.delivered",
                                "message.read",
                                "message.failed",
                                "message.expired",
                                "webhook.test"
                            ]
                        }
                    },
                    {
                        "name": "X-Aqwa-Delivery",
                        "in": "header",
                        "schema": {
                            "type": "string"
                        },
                        "example": "evt_01j9zq4m8h2k7x3v5b6n1c0d9e"
                    },
                    {
                        "name": "X-Aqwa-Signature",
                        "in": "header",
                        "schema": {
                            "type": "string"
                        },
                        "example": "t=1790493304,v1=5f2b…"
                    }
                ],
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/WebhookEvent"
                            }
                        }
                    }
                },
                "responses": {
                    "2XX": {
                        "description": "Received."
                    }
                }
            }
        }
    },
    "components": {
        "securitySchemes": {
            "bearerAuth": {
                "type": "http",
                "scheme": "bearer",
                "description": "API key from the panel (aqw_live_…). `X-Api-Key` header is also accepted."
            }
        },
        "parameters": {
            "IdempotencyKey": {
                "name": "Idempotency-Key",
                "in": "header",
                "required": false,
                "description": "Optional (recommended for OTP). Repeating the same request with the same key within 24h returns the first response without sending again.",
                "schema": {
                    "type": "string",
                    "maxLength": 255
                },
                "example": "order-1048-otp"
            },
            "RequestId": {
                "name": "X-Request-Id",
                "in": "header",
                "required": false,
                "description": "Optional trace id (8–64 chars `[A-Za-z0-9._-]`); echoed back, otherwise generated.",
                "schema": {
                    "type": "string"
                }
            }
        },
        "headers": {
            "RequestId": {
                "description": "Request trace id (quote it to support).",
                "schema": {
                    "type": "string"
                }
            }
        },
        "schemas": {
            "SendMessage": {
                "type": "object",
                "required": [
                    "to",
                    "body"
                ],
                "properties": {
                    "to": {
                        "type": "string",
                        "description": "Recipient in international format (country code, with or without +).",
                        "example": "9665XXXXXXXX"
                    },
                    "body": {
                        "type": "string",
                        "maxLength": 4096,
                        "description": "Message text. `message` is accepted as an alias."
                    },
                    "type": {
                        "type": "string",
                        "enum": [
                            "text",
                            "otp"
                        ],
                        "default": "text",
                        "description": "OTP messages are prioritized and expire if not sent in time."
                    }
                }
            },
            "Message": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string",
                        "example": "msg_01j9zq4k2r5t8y1u3i6o9p2a4s"
                    },
                    "to": {
                        "type": "string"
                    },
                    "type": {
                        "type": "string",
                        "enum": [
                            "text",
                            "otp"
                        ]
                    },
                    "status": {
                        "type": "string",
                        "enum": [
                            "queued",
                            "processing",
                            "sent",
                            "delivered",
                            "read",
                            "failed",
                            "expired"
                        ],
                        "description": "queued → processing → sent (✓) → delivered (✓✓) → read (blue ✓✓, only if the recipient allows read receipts); or failed / expired."
                    },
                    "error": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "description": "Failure reason (e.g. not_on_whatsapp, no_connected_session, otp_expired)."
                    },
                    "created_at": {
                        "type": "string",
                        "format": "date-time"
                    },
                    "sent_at": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "format": "date-time"
                    },
                    "delivered_at": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "format": "date-time"
                    },
                    "read_at": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "format": "date-time"
                    },
                    "expires_at": {
                        "type": [
                            "string",
                            "null"
                        ],
                        "format": "date-time"
                    }
                }
            },
            "AccountResponse": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "const": true
                    },
                    "data": {
                        "type": "object",
                        "properties": {
                            "account": {
                                "type": "object",
                                "properties": {
                                    "type": {
                                        "type": "string",
                                        "enum": [
                                            "subscriber",
                                            "platform"
                                        ]
                                    },
                                    "name": {
                                        "type": "string"
                                    },
                                    "status": {
                                        "type": "string"
                                    }
                                }
                            },
                            "key": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "prefix": {
                                        "type": "string"
                                    },
                                    "last_used_at": {
                                        "type": [
                                            "string",
                                            "null"
                                        ],
                                        "format": "date-time"
                                    }
                                }
                            },
                            "numbers": {
                                "type": "object",
                                "properties": {
                                    "connected": {
                                        "type": "integer"
                                    },
                                    "total": {
                                        "type": "integer"
                                    }
                                }
                            },
                            "ready": {
                                "type": "boolean",
                                "description": "At least one connected WhatsApp number."
                            },
                            "limits": {
                                "type": "object",
                                "additionalProperties": {
                                    "type": "integer"
                                }
                            },
                            "api_version": {
                                "type": "string"
                            }
                        }
                    }
                }
            },
            "MessageResponse": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "const": true
                    },
                    "data": {
                        "$ref": "#/components/schemas/Message"
                    }
                }
            },
            "Error": {
                "type": "object",
                "properties": {
                    "success": {
                        "type": "boolean",
                        "const": false
                    },
                    "error": {
                        "type": "object",
                        "required": [
                            "code",
                            "message"
                        ],
                        "properties": {
                            "code": {
                                "type": "string",
                                "enum": [
                                    "missing_api_key",
                                    "invalid_api_key",
                                    "account_inactive",
                                    "validation_error",
                                    "not_found",
                                    "method_not_allowed",
                                    "bad_request",
                                    "rate_limited",
                                    "idempotency_conflict",
                                    "idempotency_in_progress",
                                    "server_error"
                                ]
                            },
                            "message": {
                                "type": "string"
                            },
                            "fields": {
                                "type": "object",
                                "additionalProperties": {
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                }
                            },
                            "limit": {
                                "type": "string",
                                "enum": [
                                    "api_key",
                                    "auth_ip",
                                    "otp_cooldown",
                                    "otp_recipient",
                                    "otp_recipient_daily",
                                    "recipient"
                                ]
                            },
                            "retry_after": {
                                "type": "integer"
                            }
                        }
                    },
                    "request_id": {
                        "type": "string"
                    }
                }
            },
            "WebhookEvent": {
                "type": "object",
                "properties": {
                    "id": {
                        "type": "string",
                        "example": "evt_01j9zq4m8h2k7x3v5b6n1c0d9e"
                    },
                    "event": {
                        "type": "string",
                        "enum": [
                            "message.sent",
                            "message.delivered",
                            "message.read",
                            "message.failed",
                            "message.expired",
                            "webhook.test"
                        ]
                    },
                    "created_at": {
                        "type": "string",
                        "format": "date-time"
                    },
                    "data": {
                        "$ref": "#/components/schemas/Message"
                    }
                }
            }
        }
    }
}