# WhatsApp API

> WhatsApp Business API setup, configuration, and messaging endpoints

Source: https://edesy.in/docs/waflow/api/whatsapp

Configure your WhatsApp Business API integration and send messages programmatically.

## Setup Guide

Before using the WhatsApp endpoints, you need to obtain credentials from Facebook/Meta.

### Step 1: Create Facebook Developer Account

1. Go to [developers.facebook.com](https://developers.facebook.com/)
2. Click 'Get Started' and create an account
3. Verify your email and phone number
4. Complete developer verification if prompted

### Step 2: Create Facebook App

1. Go to 'My Apps' > 'Create App'
2. Select 'Business' as the app type
3. Fill in app name and contact email
4. Add WhatsApp product to your app
5. Note down your **App ID** and **App Secret**

### Step 3: Set Up WhatsApp Business API

1. Go to WhatsApp > API Setup in your app
2. Add a phone number (or use test number)
3. Generate temporary access token
4. Note down your **WhatsApp Business Account ID**
5. Copy the **temporary access token**

### Step 4: Generate System User Token

1. Go to Business Settings > System Users
2. Create a new system user
3. Assign WhatsApp Business Management permission
4. Generate a **permanent access token**
5. Save this token securely - it doesn't expire

### Step 5: Configure Webhook (Optional)

1. Go to WhatsApp > Configuration
2. Add webhook URL: `https://your-domain.com/webhook/whatsapp`
3. Use a verify token (any random string)
4. Subscribe to `messages` and `messaging_postbacks` events
5. Verify webhook endpoint

### Required Credentials

| Credential | Description |
|------------|-------------|
| Facebook App ID | Your Facebook App ID from step 2 |
| Facebook App Secret | Your Facebook App Secret from step 2 |
| WhatsApp Business Account ID | From WhatsApp API Setup |
| System User Access Token | Long-lived token from step 4 |
| Phone Number ID | From WhatsApp API Setup |
| Webhook Verify Token | Custom token for webhook verification |

### Troubleshooting

| Issue | Solution |
|-------|----------|
| Can't find WhatsApp product | Ensure your developer account is verified and you've selected 'Business' app type |
| Access token expired | Use System User tokens instead of temporary tokens for production |
| Phone number not verified | Verify your phone number in WhatsApp Business API settings |
| Webhook verification failed | Ensure your webhook endpoint returns the challenge parameter |

---

## Configure WhatsApp Integration

Set up WhatsApp Business API credentials in your workspace.

```http
POST /api/whatsapp/configure
```

**Request Body:**
```json
{
  "facebookAppId": "your_facebook_app_id",
  "facebookAppSecret": "your_facebook_app_secret",
  "whatsappBusinessAccountId": "your_whatsapp_business_account_id",
  "accessToken": "your_system_user_access_token",
  "phoneNumberId": "your_phone_number_id",
  "webhookVerifyToken": "your_webhook_verify_token"
}
```

**Response:**
```json
{
  "success": true,
  "message": "WhatsApp configuration saved successfully",
  "configuration": {
    "id": 1,
    "isConfigured": true,
    "phoneNumber": "+1234567890",
    "businessAccountId": "your_whatsapp_business_account_id",
    "tokenExpiresAt": "2025-12-31T23:59:59.000Z",
    "createdAt": "2025-01-01T00:00:00.000Z"
  }
}
```

---

## Check WhatsApp Status

Check the status of your WhatsApp Business API integration.

```http
GET /api/whatsapp/status
```

**Response:**
```json
{
  "isConfigured": true,
  "isActive": true,
  "phoneNumber": "+1234567890",
  "businessAccountId": "123456789",
  "tokenStatus": "valid",
  "tokenExpiresAt": "2025-12-31T23:59:59.000Z",
  "lastSyncAt": "2025-01-01T12:00:00.000Z",
  "templatesCount": 5,
  "capabilities": {
    "sendMessages": true,
    "receiveMessages": true,
    "manageTemplates": true,
    "accessInsights": true
  }
}
```

---

## Sync WhatsApp Templates

Import approved message templates from WhatsApp Business API.

```http
POST /api/whatsapp/templates/sync
```

**Response:**
```json
{
  "success": true,
  "message": "Templates synced successfully",
  "imported": 5,
  "updated": 2,
  "skipped": 1,
  "templates": [
    {
      "id": 1,
      "name": "welcome_message",
      "status": "APPROVED",
      "language": "en",
      "category": "MARKETING",
      "components": [
        {
          "type": "BODY",
          "text": "Hello {{1}}, welcome to {{2}}!"
        }
      ]
    }
  ]
}
```

---

## Send WhatsApp Message

Send a message using WhatsApp Business API.

```http
POST /api/whatsapp/send
```

**Request Body:**
```json
{
  "to": "+1234567890",
  "type": "template",
  "template": {
    "name": "welcome_message",
    "language": {
      "code": "en"
    },
    "components": [
      {
        "type": "body",
        "parameters": [
          {
            "type": "text",
            "text": "John"
          },
          {
            "type": "text",
            "text": "Acme Corp"
          }
        ]
      }
    ]
  }
}
```

**Response:**
```json
{
  "success": true,
  "message": "Message sent successfully",
  "messageId": "wamid.ABC123...",
  "status": "sent",
  "timestamp": "2025-01-01T12:00:00.000Z"
}
```

**cURL Example:**
```bash
curl -X POST "https://waflow.edesy.in/api/whatsapp/send" \
  -H "Content-Type: application/json" \
  -b cookies.txt \
  -d '{
    "to": "+1234567890",
    "type": "template",
    "template": {
      "name": "welcome_message",
      "language": {"code": "en"},
      "components": [
        {
          "type": "body",
          "parameters": [
            {"type": "text", "text": "John"},
            {"type": "text", "text": "Acme Corp"}
          ]
        }
      ]
    }
  }'
```

---

## List WhatsApp Templates

Get all approved WhatsApp message templates.

```http
GET /api/whatsapp/templates
```

**Response:**
```json
{
  "templates": [
    {
      "id": 1,
      "name": "welcome_message",
      "status": "APPROVED",
      "language": "en",
      "category": "MARKETING",
      "components": [
        {
          "type": "HEADER",
          "format": "TEXT",
          "text": "Welcome!"
        },
        {
          "type": "BODY",
          "text": "Hello {{1}}, welcome to {{2}}! We're excited to have you on board."
        },
        {
          "type": "FOOTER",
          "text": "Best regards, Team"
        }
      ]
    }
  ],
  "total": 1
}
```

---

## Testing Your Setup

After completing the setup:

1. Go to Settings > WhatsApp Configuration in WAFlow
2. Enter your credentials and save
3. Check that the status indicator turns green
4. Try syncing templates from WhatsApp
5. Send a test message to verify
