> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hypersender.com/llms.txt
> Use this file to discover all available pages before exploring further.

# WhatsApp and SMS OTP in Python

> Send and verify OTP codes over WhatsApp or SMS from Python, and create WhatsApp OTP links, with the Hypersender OTP API. Expiry and validation checks.

Use the Hypersender OTP service from Python to send verification codes over WhatsApp or SMS, validate the codes your users enter, and generate passwordless WhatsApp links.

## What you can do

* **Request OTP codes**: Generate a one-time code and send it from a WhatsApp or SMS instance.
* **Validate codes**: Check user-entered codes against the active OTP.
* **Generate OTP links (WhatsApp only)**: Create WhatsApp links that complete verification without typing. SMS instances can't generate links.

## Prerequisites

* Python app with the `hypersender` package installed ([Installation](https://docs.hypersender.com/v2/packages-and-sdks/python/installation.md)).
* Hypersender **API token** and **Instance UUID** from the dashboard, set as `HYPERSENDER_OTP_API_KEY` and `HYPERSENDER_OTP_INSTANCE_ID`.
* For SMS, an SMS instance with the Hypersender SMS app running on the linked Android phone. See [SMS quick start](https://docs.hypersender.com/v2/hypersender/overview/sms/sms-quick-start.md).

## Base URL and auth

```bash theme={null}
Base URL: https://app.hypersender.com/api/otp/v2/{instance}
Auth: Authorization: Bearer YOUR_API_TOKEN
```

The SDK adds the instance UUID to the URL and the token to the header for you.

## Quick start (request and validate)

```python theme={null}
from hypersender import Hypersender, HypersenderError

hs = Hypersender()

code_from_user = "482913"  # the code the user typed

# 1) Send a code
result = hs.otp.request_code(
    "201001234567@c.us",  # WhatsApp chat ID
    length=6,
    use_number=True,
    use_letter=False,
    all_capital=False,
    name="YourApp",
    expires=300,
)

chat_id = result["data"]["chat_id"]

# 2) Validate the code the user typed
is_valid = False

try:
    validation = hs.otp.validate_code(chat_id, code_from_user)
    is_valid = validation["success"] is True
except HypersenderError as error:
    # Wrong, used, or expired code: error.status_code is 422
    pass
```

Both methods return the parsed JSON response body as a `dict`, with the OTP details under `data`. Any `4XX` or `5XX` response raises a `HypersenderError`; a wrong, used, or expired code raises with `status_code` `422`.

## Send the code over SMS

With an SMS instance, pass the phone number in international format as `chat_id`. Spaces, dashes, dots, and parentheses are removed, and `00` at the start is read as `+`. WhatsApp chat IDs are rejected for SMS.

```python theme={null}
from hypersender import Hypersender

hs = Hypersender()

result = hs.otp.request_code(
    "+201001234567",
    length=6,
    use_number=True,
    use_letter=False,
    all_capital=False,
    name="YourApp",
    expires=300,
)
```

The user receives a short plain-text SMS, such as:

```text theme={null}
YourApp code: 482913
```

The wording rotates between six short versions, so carriers are less likely to drop it. Validate it with the same phone number. If you want your own message text, send the code yourself with the SMS API. See the [SMS OTP guide](https://docs.hypersender.com/v2/hypersender/overview/sms/sms-otp-guide.md).

> [!WARNING]
>
> Keep tokens and OTP codes out of logs. Use HTTPS for all calls.

## Next steps

* [Request OTP Code](https://docs.hypersender.com/v2/packages-and-sdks/python/otp/request-otp-code-python-sdk.md)
* [Validate OTP Code](https://docs.hypersender.com/v2/packages-and-sdks/python/otp/validate-otp-code-python-sdk.md)
* [Generate OTP Link](https://docs.hypersender.com/v2/packages-and-sdks/python/otp/generate-whatsapp-otp-link-with-python.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.