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

# OTP

> Send and verify one-time passwords by SMS, WhatsApp or email.

OTP verification takes two calls: `authentica.otp.send()` delivers a code to your user, and `authentica.otp.verify()` checks the code they enter.

## Send OTP

### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| `method` | `'sms'` \| `'whatsapp'` \| `'email'` | Yes | Delivery channel. |
| `phone` | `string` | For `sms` and `whatsapp` | Phone number in international format, for example `+9665XXXXXXXX`. |
| `email` | `string` | For `email` | Email address. |
| `templateId` | `number` | No | Integer ID of an OTP template. If omitted, the API uses template `1`. |

Pass either `phone` or `email`, matching the method. Passing both throws an `AuthenticaValidationError` and nothing is sent.

### Examples

```ts SMS theme={null}
await authentica.otp.send({
  method: 'sms',
  phone: '+9665XXXXXXXX',
});
```

```ts WhatsApp theme={null}
await authentica.otp.send({
  method: 'whatsapp',
  phone: '+9665XXXXXXXX',
});
```

```ts Email theme={null}
await authentica.otp.send({
  method: 'email',
  email: 'user@example.com',
});
```

```ts With template theme={null}
await authentica.otp.send({
  method: 'sms',
  phone: '+9665XXXXXXXX',
  templateId: 31,
});
```

> [!TIP]
>
> Template IDs and default OTP settings are managed in your application settings on the [Authentica dashboard](https://portal.authentica.sa).

### Response

```ts theme={null}
{
  success: boolean;
  message: string;
}
```

**success** `boolean`

Whether the API accepted the send request.

**message** `string`

Message returned by the API.

The response has no verification ID. To verify, pass the same phone number or email together with the code your user entered.

## Verify OTP

### Parameters

| Parameter | Type | Required | Description |
| - | - | - | - |
| `phone` | `string` | If the OTP was sent by SMS or WhatsApp | The phone number the OTP was sent to. |
| `email` | `string` | If the OTP was sent by email | The email address the OTP was sent to. |
| `otp` | `string` | Yes | The code your user entered. |

Provide exactly one of `phone` or `email`.

### Examples

```ts By phone theme={null}
const result = await authentica.otp.verify({
  phone: '+9665XXXXXXXX',
  otp: codeEnteredByUser,
});
```

```ts By email theme={null}
const result = await authentica.otp.verify({
  email: 'user@example.com',
  otp: codeEnteredByUser,
});
```

Pass `otp` as a string, exactly as the user entered it. This preserves leading zeros. Do not assume a fixed code length.

### Response

```ts theme={null}
{
  status: boolean;
  message: string;
}
```

**status** `boolean`

`true` if the OTP is valid.

**message** `string`

Message returned by the API.

### Handling a rejected code

A rejected code can come back in two ways, depending on the API response:

* a normal result with `status: false`, or
* an `AuthenticaApiError` with `status` `422`.

Handle both:

```ts theme={null}
import { AuthenticaApiError } from '@authenticasa/sdk';

async function checkCode(phone: string, otp: string) {
  try {
    const result = await authentica.otp.verify({ phone, otp });
    return result.status;
  } catch (error) {
    if (error instanceof AuthenticaApiError && error.status === 422) {
      return false;
    }
    throw error;
  }
}
```

> [!NOTE]
>
> A 422 does not always mean the code was mistyped. The code may have expired, or the request may have been rejected for another reason.

> [!WARNING]
>
> Never log OTP codes. Avoid logging phone numbers or email addresses unless you need them.

See also: [OTP Verification Workflow](https://docs.authentica.sa/guides/otp-workflow.md) and the [Send OTP](https://docs.authentica.sa/api-reference/otp-verification/send-otp.md) API Reference.


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