> For the complete documentation index, see [llms.txt](https://docs.lipachat.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lipachat.com/crm-workflow.md).

# CRM Workflow

This tutorial guides you through integrating a CRM system with Lipachat to allow operators to respond to WhatsApp messages in real time. It includes logic for handling both 24-hour session replies and template-based messages.

### 🪝 Step 1: Set Up Webhook

Set your webhook endpoint in the Lipachat Dashboard → **Settings**. This allows Lipachat to notify your CRM when a customer sends a WhatsApp message.

**Example:**

```
https://yourcrm.com/api/whatsapp-webhook
```

When a message is received, a POST request will be made to this URL with the message details.

### 📥 Step 2: Receive and Display Incoming Messages

**Sample webhook payload:**

```json
{
     "messageId": "c94ced08-5f40-46b7-a88d-7d4fbe113fe9",
     "from": "CUSTOMER_PHONE_NUMBER",
     "to": "WHATSAPP_NUMBER",
     "profileName": "CUSTOMER_NAME",
     "type": "TEXT",
     "text": "Text Content"
}
```

Display the message in your CRM’s chat interface and associate it with the appropriate customer profile.

### 🧠 Step 3: Determine Session Status

Check whether the 24-hour session window is still valid. If the current timestamp is **before** `session_expires_at`, respond with a free-form message. Otherwise, use a pre-approved template.

```
const isSessionActive = Date.now() < session_expires_at;
```

{% hint style="info" %}
Session expires at should hold the timestamp of the last message time + 24 hours.\
E.g., if a customer's last message is 27th May 2025 9.00 am, the 'session\_expires\_at' will be 28th May 2025 9.00 am
{% endhint %}

### 📝 Step 4A: Respond Within 24-Hour Session (Free Form)

```sh
curl --location 'https://gateway.lipachat.com/api/v1/whatsapp/message/text' \
--header 'apiKey: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
    "message": "Hello, how can I help you today?",
    "messageId": "e66fc7b8-680f-4887-8dc8-ee062d65b54f",
    "to": "254XXXXXXX",
    "from": "254110090747"
}'
```

### 🧾 Step 4B: Respond Outside Session (Template Message)

```sh
curl --location 'https://gateway.lipachat.com/api/v1/whatsapp/template' \
--header 'apiKey: YOUR_API_KEY' \
--header 'Content-Type: application/json' \
--data '{
	"messageId": "5c7be224-be61-4aa5-97aa-98906f1f6d15",
	"to": "254XXXXXXX",
	"from": "254110090747",
	"template": {
		"name": "declined_transfert",
		"languageCode": "en",
		"components": {
			"header": {
				"type": "IMAGE", 
				"mediaUrl": "https://picsum.photos/id/237/200/300" 
			},
			"body": {
				"placeholders": [
					"John",
					"Airtime",
					"KES 100",
					"Insufficient funds",
					"funding your account"
				]
			}
		}
	}
}'
```

### End-to-End Flow Summary

1. Customer sends message → webhook sends to CRM
2. CRM operator sees message and session info
3. If the session is active → send a reply
4. If the session expired → send the approved template

With this setup, your CRM team can interact with customers compliantly and in real time via WhatsApp.

Need help creating message templates? Visit the [Message Templates](/api/templates.md) Guide.
