{
  "openapi": "3.1.0",
  "info": {
    "title": "Maddy Developers WhatsApp Service",
    "version": "1.0.0",
    "summary": "Send WhatsApp messages from your own phone numbers.",
    "description": "Link a WhatsApp number by scanning a QR code, then send messages over HTTP.\n\nYour numbers, messages and delivery logs are private to your account and reachable only with your own token.\n\nAuthenticate with the `X-API-Token` header on every request. Your token is shown once when issued and cannot be recovered - ask support to rotate a new one if lost.",
    "contact": {
      "name": "Maddy Developers Support",
      "email": "maddydevelops6@gmail.com",
      "url": "https://md.wepure.site"
    }
  },
  "servers": [
    {
      "url": "https://whatsapp-service.wepure.site",
      "description": "Production"
    }
  ],
  "security": [
    {
      "ApiToken": []
    }
  ],
  "tags": [
    {
      "name": "Account",
      "description": "Who you are and what you may do"
    },
    {
      "name": "Numbers",
      "description": "Linking WhatsApp numbers (sessions)"
    },
    {
      "name": "Messages",
      "description": "Sending and history"
    }
  ],
  "paths": {
    "/api/v1/health": {
      "get": {
        "tags": [
          "Account"
        ],
        "summary": "Liveness probe",
        "description": "No authentication required.",
        "security": [],
        "responses": {
          "200": {
            "description": "Service is up",
            "content": {
              "application/json": {
                "example": {
                  "message": "ok",
                  "service": "whatsapp-service",
                  "time": "2026-11-01T09:00:00.000Z"
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/me": {
      "get": {
        "tags": [
          "Account"
        ],
        "summary": "Confirm credentials and read your allowance",
        "description": "Call this first when wiring up an integration. It proves the token works and reports whether you may create more numbers.\n\nCheck `allowance.can_create` before offering an 'add number' action in your own UI, rather than discovering the limit from a 409.",
        "responses": {
          "200": {
            "description": "Authenticated",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Me"
                },
                "example": {
                  "message": "Authenticated",
                  "account": {
                    "id": "4cbc...",
                    "username": "your-project"
                  },
                  "sessions": {
                    "total": 2,
                    "connected": 2
                  },
                  "allowance": {
                    "mode": "MULTI",
                    "used": 2,
                    "limit": 5,
                    "can_create": true
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    },
    "/api/v1/sessions": {
      "get": {
        "tags": [
          "Numbers"
        ],
        "summary": "List your WhatsApp numbers",
        "description": "Costs one upstream call regardless of how many numbers you have.",
        "responses": {
          "200": {
            "description": "Your numbers",
            "content": {
              "application/json": {
                "example": {
                  "data": [
                    {
                      "id": "7c9e...",
                      "label": "Sales line",
                      "state": "WORKING",
                      "connected": true,
                      "phone_number": "923001234567",
                      "daily_limit": 500
                    }
                  ],
                  "count": 3,
                  "connected": 2
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      },
      "post": {
        "tags": [
          "Numbers"
        ],
        "summary": "Create a number slot",
        "description": "Creates the slot only. Nothing starts until you request the QR, so an unused slot costs nothing.\n\nFails with 409 if your account is SINGLE mode and already has a number, or if you have reached your MULTI limit.",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateSession"
              },
              "example": {
                "label": "Sales line",
                "warmup": true
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "example": {
                  "session": {
                    "id": "7c9e...",
                    "label": "Sales line",
                    "state": "PENDING"
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "409": {
            "description": "Session limit reached for your account",
            "content": {
              "application/json": {
                "example": {
                  "message": "This account is on single-session mode and already has a session."
                }
              }
            }
          }
        }
      }
    },
    "/api/v1/sessions/{id}": {
      "parameters": [
        {
          "$ref": "#/components/parameters/SessionId"
        }
      ],
      "get": {
        "tags": [
          "Numbers"
        ],
        "summary": "One number, with recent lifecycle events",
        "responses": {
          "200": {
            "description": "Session detail"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      },
      "delete": {
        "tags": [
          "Numbers"
        ],
        "summary": "Remove the number entirely",
        "responses": {
          "200": {
            "description": "Deleted"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/v1/sessions/{id}/qr": {
      "parameters": [
        {
          "$ref": "#/components/parameters/SessionId"
        }
      ],
      "get": {
        "tags": [
          "Numbers"
        ],
        "summary": "Get the QR code to link a phone",
        "description": "Self-starting: calling this on a never-started session brings it up, so one endpoint takes you from 'new' to 'scannable'.\n\nIMPORTANT - poll this every 2 seconds and always render the newest `qr` value. WhatsApp rotates the code roughly every 20 seconds, and showing a cached image makes the phone report 'Unable to link device'. Stop polling when `session.state` becomes `WORKING`.\n\n`qr` is a PNG data URL usable directly as an <img> src.",
        "responses": {
          "200": {
            "description": "Current QR, or null once connected",
            "content": {
              "application/json": {
                "example": {
                  "message": "Scan the QR code to link this number",
                  "session": {
                    "id": "7c9e...",
                    "state": "SCAN_QR",
                    "connected": false
                  },
                  "qr": "data:image/png;base64,iVBORw0KGgo...",
                  "qr_expires_in_sec": 14
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/v1/sessions/{id}/start": {
      "parameters": [
        {
          "$ref": "#/components/parameters/SessionId"
        }
      ],
      "post": {
        "tags": [
          "Numbers"
        ],
        "summary": "Start or restart a number",
        "description": "`force: true` restarts a FAILED session. It deliberately will not restart one that is mid-scan, since that invalidates the user's pairing attempt.",
        "requestBody": {
          "content": {
            "application/json": {
              "example": {
                "force": false
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Started"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/v1/sessions/{id}/logout": {
      "parameters": [
        {
          "$ref": "#/components/parameters/SessionId"
        }
      ],
      "post": {
        "tags": [
          "Numbers"
        ],
        "summary": "Unlink the phone",
        "description": "The slot survives, so the same id can be linked again later.",
        "responses": {
          "200": {
            "description": "Signed out"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/api/v1/messages/send": {
      "post": {
        "tags": [
          "Messages"
        ],
        "summary": "Send a text message",
        "description": "Sends from the number named by `session_id`.\n\nAlways set the `Idempotency-Key` header for OTPs. A repeat of the same key returns the original result and sends nothing - without it, a client retry after a timeout delivers a second code.\n\nReturns 409 immediately when the number is not connected; it never blocks waiting for a scan.",
        "parameters": [
          {
            "name": "Idempotency-Key",
            "in": "header",
            "required": false,
            "schema": {
              "type": "string",
              "maxLength": 190
            },
            "description": "Unique per send attempt, e.g. otp-<user-id>-<timestamp>. Strongly recommended for OTPs.",
            "example": "otp-user-8821-1730450000"
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SendMessage"
              },
              "example": {
                "session_id": "7c9e...",
                "to": "03001234567",
                "message": "Your code is 418290"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Sent, or replayed from an earlier identical idempotency key",
            "content": {
              "application/json": {
                "example": {
                  "message": "Message sent successfully",
                  "replayed": false,
                  "to": "923001234567",
                  "message_id": "3EB0...",
                  "log_id": "a1b2...",
                  "status": "SENT",
                  "usage": {
                    "minute_used": 3,
                    "minute_limit": 20,
                    "day_used": 47,
                    "day_limit": 215
                  }
                }
              }
            }
          },
          "400": {
            "description": "Bad input, or the number is not registered on WhatsApp"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "409": {
            "description": "That number is not connected right now - fetch its QR and have it scanned",
            "content": {
              "application/json": {
                "example": {
                  "message": "Session is waiting for a QR scan. Link the device before sending."
                }
              }
            }
          },
          "429": {
            "description": "Rate limit reached. Honour the Retry-After header.",
            "headers": {
              "Retry-After": {
                "schema": {
                  "type": "integer"
                },
                "description": "Seconds to wait"
              }
            }
          }
        }
      }
    },
    "/api/v1/messages": {
      "get": {
        "tags": [
          "Messages"
        ],
        "summary": "Message history",
        "description": "Status advances PENDING -> SENT -> DELIVERED -> READ as receipts arrive, or FAILED.",
        "parameters": [
          {
            "name": "session_id",
            "in": "query",
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "PENDING",
                "SENT",
                "DELIVERED",
                "READ",
                "FAILED"
              ]
            }
          },
          {
            "name": "search",
            "in": "query",
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "limit",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 25,
              "maximum": 100
            }
          },
          {
            "name": "page",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 1
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Paged history"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "ApiToken": {
        "type": "apiKey",
        "in": "header",
        "name": "X-API-Token",
        "description": "Your API token, issued by Maddy Developers. `Authorization: Bearer <token>` is also accepted."
      }
    },
    "parameters": {
      "SessionId": {
        "name": "id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string",
          "format": "uuid"
        },
        "description": "The session id returned when you created the number."
      }
    },
    "responses": {
      "Unauthorized": {
        "description": "Token missing, malformed, revoked or expired",
        "content": {
          "application/json": {
            "example": {
              "message": "Unauthorized. Provide a valid API token."
            }
          }
        }
      },
      "NotFound": {
        "description": "No session with that id.",
        "content": {
          "application/json": {
            "example": {
              "message": "Session not found"
            }
          }
        }
      }
    },
    "schemas": {
      "Me": {
        "type": "object",
        "properties": {
          "account": {
            "type": "object",
            "properties": {
              "id": {
                "type": "string"
              },
              "username": {
                "type": "string"
              }
            }
          },
          "sessions": {
            "type": "object",
            "properties": {
              "total": {
                "type": "integer"
              },
              "connected": {
                "type": "integer"
              }
            }
          },
          "allowance": {
            "type": "object",
            "description": "How many numbers you may link.",
            "properties": {
              "mode": {
                "type": "string",
                "enum": [
                  "SINGLE",
                  "MULTI"
                ]
              },
              "used": {
                "type": "integer"
              },
              "limit": {
                "type": [
                  "integer",
                  "null"
                ],
                "description": "null means no explicit cap"
              },
              "can_create": {
                "type": "boolean"
              }
            }
          }
        }
      },
      "CreateSession": {
        "type": "object",
        "required": [
          "label"
        ],
        "properties": {
          "label": {
            "type": "string",
            "maxLength": 120,
            "description": "Your own name for this number, e.g. 'Sales line'."
          },
          "warmup": {
            "type": "boolean",
            "default": true,
            "description": "Ramp the daily cap over ~14 days. Leave on for any newly linked number - sending at full volume from a fresh number is the most reliable way to get it banned."
          },
          "per_minute_limit": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1,
            "maximum": 120
          },
          "daily_limit": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1,
            "maximum": 20000
          }
        }
      },
      "SendMessage": {
        "type": "object",
        "required": [
          "session_id",
          "to",
          "message"
        ],
        "properties": {
          "session_id": {
            "type": "string",
            "format": "uuid",
            "description": "Which of your numbers to send from."
          },
          "to": {
            "type": "string",
            "description": "Recipient. Non-digits are stripped and a leading 0 is replaced with the country code (92 by default), so 0300-1234567, 03001234567 and +923001234567 are equivalent."
          },
          "message": {
            "type": "string",
            "maxLength": 4096
          }
        }
      },
      "SessionState": {
        "type": "string",
        "enum": [
          "PENDING",
          "STARTING",
          "SCAN_QR",
          "WORKING",
          "STOPPED",
          "FAILED",
          "LOGGED_OUT"
        ],
        "description": "PENDING: created, not started. STARTING: coming up. SCAN_QR: waiting for a scan. WORKING: linked and ready to send. STOPPED: stopped. FAILED: QRs went unscanned, request the QR again. LOGGED_OUT: unlinked from the phone."
      }
    }
  }
}
