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

# OTP Code

> Secure one-time password authentication via WhatsApp

## Overview

The OTP Code feature allows you to implement secure one-time password authentication through WhatsApp. This provides a familiar and secure way for users to verify their identity during login, registration, or sensitive operations.

## How It Works

1. Your application requests an OTP code to be sent to a user's WhatsApp
2. CrunchzApp generates a random code and delivers it via WhatsApp
3. The user enters this code in your application
4. Your application validates the code through the CrunchzApp API

## Available Methods

### Request Code

Send a one-time password to a WhatsApp contact.

<Tabs>
  <Tab title="Basic Usage">
    ```php theme={null}
    use CrunchzApp\CrunchzApp;

    CrunchzApp::otp('code')
      ->contact('xxx@c.us')
      ->send();
    ```
  </Tab>

  <Tab title="Parameters">
    | Parameter | Type   | Required | Description                                                         |
    | --------- | ------ | -------- | ------------------------------------------------------------------- |
    | contact   | string | Yes      | The WhatsApp contact ID in the format '[xxx@c.us](mailto:xxx@c.us)' |
  </Tab>
</Tabs>

<Note>
  By default, OTP codes expire after 5 minutes. The user will receive a message containing the generated code.
</Note>

### Validate Code

Verify an OTP code entered by the user.

<Tabs>
  <Tab title="Basic Usage">
    ```php theme={null}
    use CrunchzApp\CrunchzApp;

    CrunchzApp::otp('code')
      ->contact('xxx@c.us')
      ->validate('M9TQ');
    ```
  </Tab>

  <Tab title="Parameters">
    | Parameter | Type   | Required | Description                                                         |
    | --------- | ------ | -------- | ------------------------------------------------------------------- |
    | contact   | string | Yes      | The WhatsApp contact ID in the format '[xxx@c.us](mailto:xxx@c.us)' |
    | code      | string | Yes      | The OTP code entered by the user                                    |
  </Tab>
</Tabs>

<Warning>
  OTP codes can only be validated once. After successful validation or expiration, a new code must be requested.
</Warning>

## Common Use Cases

<AccordionGroup>
  <Accordion title="User Registration">
    Verify a user's WhatsApp number during the registration process to ensure they have access to the number they're registering with.
  </Accordion>

  <Accordion title="Secure Login">
    Add an extra layer of security by requiring OTP verification during login, especially for high-value accounts or after suspicious activity.
  </Accordion>

  <Accordion title="Transaction Verification">
    Confirm sensitive operations like payments, transfers, or account changes with an OTP code sent via WhatsApp.
  </Accordion>
</AccordionGroup>

## Error Handling

<Tabs>
  <Tab title="Invalid Code">
    ```json theme={null}
    {
      "success": false,
      "message": "Invalid OTP code",
      "error_code": "OTP_INVALID"
    }
    ```
  </Tab>

  <Tab title="Expired Code">
    ```json theme={null}
    {
      "success": false,
      "message": "OTP code has expired",
      "error_code": "OTP_EXPIRED"
    }
    ```
  </Tab>

  <Tab title="Too Many Attempts">
    ```json theme={null}
    {
      "success": false,
      "message": "Too many failed attempts",
      "error_code": "OTP_MAX_ATTEMPTS"
    }
    ```
  </Tab>
</Tabs>
