# Zero-Knowledge OTP Verification (v2)
URL: /docs/recipes/zero-knowledge-otp

> Issue 128-bit single-use challenge nonces, derive SHA-256 codeProof digests, verify OTPs without transmitting raw PINs, and handle failover.

Difficulty: Advanced | Duration: 10 min | Language: TypeScript

## Step 1: Issue Challenge & Capture challengeNonce (`src/services/otp-v2.ts`)

{
  const res = await fetch(\`\${process.env.SMSPRO_BASE_URL}/api/v2/public/otp/send\`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Api-Key': process.env.SMSPRO_API_KEY!,
    },
    body: JSON.stringify({
      phoneNumber,
      senderId: 'AUTH',
    }),
  });

  if (!res.ok) throw new Error(\`OTP send failed: \${res.status}\`);
  const json = (await res.json()) as { data: OtpSendV2Response };
  return json.data;
}`}
  >
    Call `POST /api/v2/public/otp/send`. SMS Pro delivers the numeric PIN out-of-band to the subscriber&apos;s handset and returns a single-use 128-bit hex `challengeNonce` plus `otpId`.

## Step 2: Derive Canonical SHA-256 codeProof (`src/lib/zk-proof.ts`)

{
  const canonical = \`\${challengeNonce.trim().toLowerCase()}:\${userCode.trim().toLowerCase()}\`;
  const data = new TextEncoder().encode(canonical);
  const digest = await crypto.subtle.digest('SHA-256', data);
  return Array.from(new Uint8Array(digest))
    .map((byte) => byte.toString(16).padStart(2, '0'))
    .join('');
}`}
  >
    Normalize both `challengeNonce` and the user-entered PIN with `.trim().toLowerCase()`, join with `:`, and compute the 64-character lowercase hexadecimal SHA-256 digest.

## Step 3: Verify Proof Without Sending Plaintext PIN (`src/services/otp-v2.ts`)

{
  const codeProof = computeCodeProof(params.challengeNonce, params.userEnteredCode);

  const res = await fetch(\`\${process.env.SMSPRO_BASE_URL}/api/v2/public/otp/verify\`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Api-Key': process.env.SMSPRO_API_KEY!,
    },
    body: JSON.stringify({
      phoneNumber: params.phoneNumber,
      otpId: params.otpId,
      codeProof,
    }),
  });

  const json = (await res.json()) as { data: OtpVerifyV2Response };
  return json.data;
}`}
  >
    Submit `phoneNumber`, `otpId`, and `codeProof` to `POST /api/v2/public/otp/verify`. The proof is checked once and cannot be reused.

## Step 4: Trigger Automatic SMS Failover (`src/services/otp-v2.ts`)

If the initial WhatsApp challenge goes undelivered or the user requests an alternate channel, invoke `POST /api/v2/public/otp/failover` to fall back to carrier SMS (`SMS_FAILOVER`) while preserving the challenge flow.

```typescript
export async function triggerOtpSmsFailover(otpId: string, phoneNumber: string) {
  const res = await fetch(`${process.env.SMSPRO_BASE_URL}/api/v2/public/otp/failover`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-Api-Key': process.env.SMSPRO_API_KEY!,
    },
    body: JSON.stringify({
      otpId,
      phoneNumber,
    }),
  });

  if (!res.ok) {
    throw new Error(`OTP failover failed with status ${res.status}`);
  }

  return res.json();
}
```