> ## 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.

# Verify an OTP Code with Python

> Verify the OTP code a user typed from Python with the Hypersender OTP API. Handle wrong, used, and expired codes for WhatsApp and SMS OTP verification flows.

Check whether an OTP code is correct, unexpired, and unused using the Python SDK. Use this after the user submits the code they received via WhatsApp.

## Parameters

| Field | Type | Required | Description |
| - | - | - | - |
| chat\_id | str | yes | The chat ID or, for SMS, the phone number that received the code. |
| code | str | yes | The OTP code entered by the user. |

## Validate the code

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

hs = Hypersender()

try:
    result = hs.otp.validate_code("+201001234567", "439713")

    if result["data"]["status"] == "validated":
        pass  # proceed with login or protected action
except HypersenderError as error:
    # Validation failed (error.status_code is 422); do not treat the user as verified
    pass
```

The method returns the parsed response on success and raises a `HypersenderError` on any `4XX` or `5XX` response.

### Common failure reasons

* Code does not match the active OTP
* Code expired based on `expires` value
* Code already used (single-use)
* No active OTP found for the `chat_id`

> [!WARNING]
>
> Implement rate limits on validation attempts (e.g., max 3-5 tries) to block brute-force guessing.

## Next

For passwordless login flows, try [Generate OTP Link](https://docs.hypersender.com/v2/packages-and-sdks/python/otp/generate-whatsapp-otp-link-with-python.md) to avoid manual code entry.


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