Skip to content

Node.js / TypeScript SDK

Coming Soon

The @sendapi/node package is currently under development and has not yet been published to npm. The examples below show the planned SDK interface. In the meantime, use any HTTP client (e.g., fetch, axios) with the REST API endpoints directly.

The official SendAPI Node.js SDK will provide a typed client for all API endpoints with automatic retries, error handling, and webhook signature verification.

Installation

bash
npm install @sendapi/node
# or
yarn add @sendapi/node

Setup

typescript
import { SendAPI } from '@sendapi/node';

const client = new SendAPI('sk_live_YOUR_API_KEY');

Sending a WhatsApp Message

typescript
const message = await client.whatsapp.send({
  session_id: 'sess_01H8BKF...Z2T',
  to: '+447700900000',
  type: 'text',
  text: { body: 'Hello from SendAPI!' }
});

console.log(message.id); // msg_01H8...QP

Sending an SMS

typescript
const sms = await client.sms.send({
  to: '+447700900000',
  content: 'Your verification code is 847291'
});

Sending an Email

typescript
const email = await client.email.send({
  to: 'alice@example.com',
  from: 'noreply@myapp.com',
  subject: 'Welcome!',
  html: '<h1>Welcome to MyApp!</h1>'
});

Sending an OTP

typescript
const verification = await client.verify.send({
  to: '+447700900000',
  channel: 'auto',
  brand_name: 'MyApp'
});

// Later, verify the code the user submitted:
const result = await client.verify.check({
  id: verification.id,
  code: userEnteredCode
});

if (result.valid) {
  // Authenticate the user
}

Webhook Signature Verification

typescript
import { verifyWebhookSignature } from '@sendapi/node';

app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
  const signature = req.headers['x-sendapi-signature'] as string;

  if (!verifyWebhookSignature(req.body, signature, process.env.WEBHOOK_SECRET!)) {
    return res.status(401).json({ error: 'Invalid signature' });
  }

  const event = JSON.parse(req.body.toString());
  console.log('Received event:', event.event);

  res.json({ received: true });
});

Error Handling

All SDK methods throw a SendAPIError on API errors:

typescript
import { SendAPIError } from '@sendapi/node';

try {
  await client.whatsapp.send({ ... });
} catch (err) {
  if (err instanceof SendAPIError) {
    console.log(err.status);     // 429
    console.log(err.code);       // "rate_limit_exceeded"
    console.log(err.message);    // "Too many requests"
  }
}

TypeScript Support

The SDK ships with full TypeScript definitions. All request and response types are exported:

typescript
import type { WhatsAppMessage, SMSMessage, Verification } from '@sendapi/node';

Released under the MIT License.