How to use Whatsapp API to Send Messages?

How to use Whatsapp API to Send Messages?

The WhatsApp Business API enables businesses to send messages to their customers in a scalable and automated manner. Whether for customer support, notifications, or marketing, using the WhatsApp API can streamline communication. In this guide, we’ll explore how to set up and use the WhatsApp API to send messages, including authentication, message formats, and best practices.


Understanding WhatsApp API

WhatsApp offers two primary API solutions:

  1. WhatsApp Business API – Designed for medium and large businesses, requiring approval from Meta.
  2. WhatsApp Cloud API – Hosted by Meta, offering a simpler setup and free hosting.

Unlike the WhatsApp Business App, these APIs do not provide a user interface but integrate with backend systems to automate messaging.


Step 1: Setting Up WhatsApp Business API

1.1 Registering for WhatsApp Business API

To use the WhatsApp Business API, follow these steps:

  • Create a Meta Business Manager account.
  • Verify your business phone number.
  • Apply for WhatsApp Business API access through an official provider (e.g., Twilio, Vonage, 360dialog).
  • Complete business verification on Meta.

For WhatsApp Cloud API, registration is simplified, and businesses can get started via the Meta Developer Console.


Step 2: Getting API Credentials

Once approved, obtain the following credentials:

  • Access Token – Required for API authentication.
  • Phone Number ID – Identifies the registered number.
  • Business Account ID – Links to your Meta Business Manager account.

Use the Meta Graph API Explorer to test API endpoints before integrating them into your system.


Step 3: Sending Messages Using WhatsApp API

WhatsApp API supports two main messaging formats:

3.1 Sending Text Messages

Use the following POST request to send a text message:

API Endpoint:

https://graph.facebook.com/v13.0/{phone-number-id}/messages

Request Headers:

{
    "Authorization": "Bearer YOUR_ACCESS_TOKEN",
    "Content-Type": "application/json"
}

Request Body:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "RECIPIENT_PHONE_NUMBER",
    "type": "text",
    "text": {"body": "Hello! This is a test message from WhatsApp API."}
}

3.2 Sending Media Messages

WhatsApp API allows sending images, videos, documents, and audio.

Example for Sending an Image:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "RECIPIENT_PHONE_NUMBER",
    "type": "image",
    "image": {"link": "https://yourimageurl.com/image.jpg"}
}

3.3 Sending Interactive Messages

Interactive messages improve user engagement. WhatsApp supports quick replies and call-to-action buttons.

Example for a Quick Reply Message:

{
    "messaging_product": "whatsapp",
    "recipient_type": "individual",
    "to": "RECIPIENT_PHONE_NUMBER",
    "type": "interactive",
    "interactive": {
        "type": "button",
        "body": {"text": "Would you like more info?"},
        "action": {
            "buttons": [
                {"type": "reply", "reply": {"id": "yes", "title": "Yes"}},
                {"type": "reply", "reply": {"id": "no", "title": "No"}}
            ]
        }
    }
}

Step 4: Managing Message Templates

To send business-initiated messages, pre-approved templates must be used.

Example of a Template Message:

{
    "messaging_product": "whatsapp",
    "to": "RECIPIENT_PHONE_NUMBER",
    "type": "template",
    "template": {
        "name": "order_confirmation",
        "language": {"code": "en_US"},
        "components": [{"type": "body", "parameters": [{"type": "text", "text": "John"}]}]
    }
}

Templates must be submitted for approval via the Meta Business Manager.


Step 5: Receiving Messages from Users

To receive messages, webhooks must be set up. Configure a webhook URL in the Meta Developer Console to capture incoming messages.

Webhook Payload for an Incoming Message:

{
    "object": "whatsapp_business_account",
    "entry": [{
        "id": "WHATSAPP_BUSINESS_ACCOUNT_ID",
        "changes": [{
            "value": {
                "messages": [{
                    "from": "USER_PHONE_NUMBER",
                    "text": {"body": "Hello!"}
                }]
            }
        }]
    }]
}

Step 6: Best Practices for WhatsApp API Messaging

🔹 Use Approved Message Templates – Business-initiated messages require templates.
🔹 Respect User Consent – Only send messages to users who have opted in.
🔹 Monitor API Limits – Ensure compliance with WhatsApp’s rate limits.
🔹 Use Webhooks for Automation – Set up real-time message tracking and event handling.
🔹 Secure API Credentials – Store tokens securely to prevent unauthorized access.


Conclusion

WhatsApp API is a powerful tool for automating customer communication, enabling businesses to send messages, images, and interactive content seamlessly. By following this guide, developers can successfully integrate WhatsApp messaging into their applications for enhanced customer engagement and support.

Facebook
Twitter
Telegram
WhatsApp

Subscribe Our Newsletter

Contact Us

File a form and let us know more about you and your project.

Let's Talk About Your Project

sdlccorp-logo
Trust badges
Contact Us
For Sales Enquiry email us a
For Job email us at
USA Flag

USA:

5214f Diamond Heights Blvd,
San Francisco, California, United States. 94131
UK Flag

United Kingdom:

30 Charter Avenue, Coventry
 CV4 8GE Post code: CV4 8GF United Kingdom
Dubai Flag

Dubai:

Unit No: 729, DMCC Business Centre Level No 1, Jewellery & Gemplex 3 Dubai, United Arab Emirates
Dubai Flag

Australia:

7 Banjolina Circuit Craigieburn, Victoria VIC Southeastern Australia. 3064
Dubai Flag

India:

715, Astralis, Supernova, Sector 94 Noida, Delhi NCR India. 201301
Dubai Flag

India:

Connect Enterprises, T-7, MIDC, Chhatrapati Sambhajinagar, Maharashtra, India. 411021
Dubai Flag

Qatar:

B-ring road zone 25, Bin Dirham Plaza building 113, Street 220, 5th floor office 510 Doha, Qatar

© COPYRIGHT 2024 - SDLC Corp - Transform Digital DMCC