convoup Docs
Docs / Guides / Sending OTPs

Sending OTPs

Use sendOtp() for login codes, verification codes, and one-time passwords.

Basic OTP

TypeScript
await client.sendOtp({
  to: '+918851479441',
  template: 'test_welcome_template',
  code: '123456',
});

The SDK places code as the first body_params value, then appends any extraParams. The server maps these to the template's {{1}}, {{2}} placeholders.

With Extra Parameters

If your template has more placeholders beyond the OTP code:

TypeScript
await client.sendOtp({
  to: '+918851479441',
  template: 'test_welcome_template',
  code: '654321',
  extraParams: ['Acme Corp'], // fills {{2}} in the template
});

cURL Equivalent

bash
curl -X POST https://api.convoup.com/api/v1/templates \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -d '{
    "to": "+918851479441",
    "template_name": "test_welcome_template",
    "language": "en_US",
    "body_params": [{"type": "text", "text": "123456"}]
  }'

How It Works Internally

  1. The SDK takes your code and places it as body_params[0]
  2. Any extraParams are appended as body_params[1], body_params[2], etc.
  3. The server maps these to the template's {{1}}, {{2}} placeholders
  4. The final payload is sent to the Meta WhatsApp Cloud API

Response

JSON
{
  "messageId": "meta-msg-abc123",
  "waId": "918851479441"
}

Error Handling

TypeScript
import { Convoup, ConvoupError } from 'convoup';

try {
  await client.sendOtp({
    to: '+918851479441',
    template: 'test_welcome_template',
    code: '123456',
  });
} catch (err) {
  if (err instanceof ConvoupError) {
    if (err.code === 'TEMPLATE_NOT_FOUND') {
      console.error('Template not found. Create it in the Convoup dashboard.');
    } else if (err.code === 'TEMPLATE_PARAM_MISMATCH') {
      console.error('Param mismatch:', err.message);
    }
  }
}

Next Steps