npm package: @sdkwa/whatsapp-api-client
Description: Multi-messenger TypeScript SDK for WhatsApp and Telegram - Unified API for both platforms with dynamic messenger selection
Author: community
Latest version: 1.0.18

# SDKWA Multi-Messenger API Client for JavaScript

[![build](https://github.com/sdkwa/whatsapp-api-client-js/actions/workflows/build.yml/badge.svg)](https://github.com/sdkwa/whatsapp-api-client-js/actions/workflows/build.yml)
[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/sdkwa/whatsapp-api-client-js/blob/master/LICENSE)
[![npm version](https://badge.fury.io/js/@sdkwa%2Fwhatsapp-api-client.svg)](https://www.npmjs.com/package/@sdkwa/whatsapp-api-client)

A modern JavaScript/TypeScript SDK for integrating with **multiple messenger platforms** through the [SDKWA HTTP API](https://sdkwa.pro).  

**Supported Messengers:**
- 🟢 **WhatsApp** - Full support for messaging, media, groups, and webhooks
- 🔵 **Telegram** - Full support for messaging, app creation, and authentication

Switch between messengers with a single parameter. All 40+ API methods work with both platforms.

---

## Installation

```sh
npm install @sdkwa/whatsapp-api-client
```

---

## Quick Start

### 1. Link Your Account (Required First)

**⚠️ Before sending messages, scan QR code with your mobile app:**

```javascript
import SDKWA from "@sdkwa/whatsapp-api-client";

const sdkwa = new SDKWA({
  idInstance: "YOUR_ID_INSTANCE",
  apiTokenInstance: "YOUR_API_TOKEN_INSTANCE"
});

// Get QR code
const qrCode = await sdkwa.getQr();
console.log(qrCode.message); // base64 image - scan with WhatsApp/Telegram app

// Check authorization
const state = await sdkwa.getStateInstance();
console.log(state.stateInstance); // "authorized" when ready
```

📱 **WhatsApp:** Settings → Linked Devices → Link a Device  
📱 **Telegram:** Settings → Devices → Link Desktop Device

---

### 2. Send Messages

```javascript
// Send to WhatsApp (default)
await sdkwa.sendMessage({
  chatId: "712345678989@c.us",
  message: "Hello WhatsApp!"
});

// Send to Telegram (override)
await sdkwa.sendMessage({
  chatId: "@username",
  message: "Hello Telegram!"
}, 'telegram');
```

---

### 3. Receive Messages (Webhooks)

```javascript
import express from "express";

const app = express();
app.use(express.json());

sdkwa.webhookHandler.onIncomingMessageText((data) => {
  console.log("Received:", data);
});

app.post("/webhook", sdkwa.webhookHandler.webhookHandler());
app.listen(3000);
```

---

## Messenger Selection

```javascript
// Set default messenger
const sdkwa = new SDKWA({
  idInstance: "YOUR_ID",
  apiTokenInstance: "YOUR_TOKEN",
  messenger: "whatsapp" // or "telegram"
});

// Use default
await sdkwa.getSettings();

// Override per call
await sdkwa.getSettings('telegram');
await sdkwa.sendMessage(params, 'telegram');
```

**All 40+ methods support both messengers:** Account, Messaging, Files, Groups, Contacts, Notifications, etc.

---

## Telegram App Creation

```javascript
// 1. Send confirmation code
await sdkwa.sendConfirmationCode({ phoneNumber: 712345678989 });

// 2. Sign in with code
await sdkwa.signInWithConfirmationCode({ code: "received_code" });

// 3. Create app
await sdkwa.createApp({
  title: "My App",
  shortName: "myapp",
  url: "https://sdkwa.pro",
  description: "My app"
});
```

---

## Examples

- [`examples/setupAccount.ts`](./examples/setupAccount.ts) - QR code authorization
- [`examples/telegramCreateApp.ts`](./examples/telegramCreateApp.ts) - Telegram app creation
- [`examples/dynamicMessenger.ts`](./examples/dynamicMessenger.ts) - Switching messengers

---

## Links

- [Official API Documentation](https://api.sdkwa.pro/en/docs/)
- [GitHub Repository](https://github.com/sdkwa/sdkwa)
- [NPM Package](https://www.npmjs.com/package/@sdkwa/whatsapp-api-client)

---

## License

MIT