How to Send WhatsApp Message Using API: A Step-by-Step Developer's Guide

·8 min read
How to Send WhatsApp Message Using API: A Step-by-Step Developer's Guide

Automating customer communication is no longer a luxury. It is a necessity for growing businesses. With billions of active users, WhatsApp is the most effective channel to reach your audience. However, manually sending messages from a phone is impossible to scale.

To automate notifications, alerts, and customer support, you must use the WhatsApp Business API. This guide shows you exactly how to send WhatsApp messages programmatically using the official Meta Cloud API, from setting up your developer account to executing your first API request.

What it means / how it works

Sending a WhatsApp message via API means you are sending an HTTP POST request from your application to Meta's servers. Meta then processes this request and delivers the message directly to the recipient's WhatsApp application on their mobile device or desktop.

Unlike personal WhatsApp accounts or the standard WhatsApp Business app, the WhatsApp Business API does not have a physical interface. It is a backend service designed to integrate directly with your CRM, website, or custom software. To prevent spam, Meta categorizes API messages into two distinct types: Session Messages and Template Messages.

Message TypeTrigger ConditionPre-Approval Required?Cost Structure
Session MessagesSent in response to a user-initiated message within a 24-hour window.No. You can send free-form text and media.Lower cost (Utility/Service conversation rates).
Template MessagesSent outside the 24-hour window, initiated by the business.Yes. Meta must approve the template text beforehand.Higher cost (Marketing, Utility, or Authentication rates).

To send any business-initiated message (like an order confirmation or an OTP), you must use a pre-approved template. For ongoing conversations started by the customer, you can send free-form text messages dynamically.

Practical use cases

  • Two-Factor Authentication (2FA) and OTPs: Deliver secure, instant one-time passwords directly to a user's WhatsApp account to verify logins or transactions.
  • Order and Shipping Updates: Send automated confirmation messages, real-time tracking links, and delivery notifications immediately after a purchase.
  • Appointment Reminders: Reduce no-shows by sending automated booking confirmations and reminder alerts with interactive "Confirm" or "Reschedule" buttons.
  • Abandoned Cart Recovery: Re-engage online shoppers who left items in their cart by offering a direct link back to checkout, along with a personalized discount.
  • Customer Support Ticketing: Automatically route incoming customer queries to your support helpdesk and send automated receipt confirmations with ticket numbers.
  • Payment and Billing Alerts: Notify clients when their monthly invoices are ready, or send friendly, automated reminders for overdue balances.
  • Event Registrations: Send digital tickets, QR codes, and venue directions to attendees right after they register for a webinar or in-person event.
  • Real-Time System Alerts: Send critical server downtime or system status alerts directly to your engineering team's WhatsApp group or individual devices.

Step-by-step guide

Follow these steps to set up your Meta developer account and send your first test message using the official WhatsApp Cloud API.

Step 1: Create a Meta Developer Account

Navigate to the Meta for Developers portal. Log in with your personal Facebook credentials. Click on "My Apps" in the top right corner, and then click "Create App." Select "Other" as your use case, click "Next," and choose "Business" as your app type. Give your app a name, associate it with your Business Manager account, and click "Create App."

Step 2: Add WhatsApp to Your App

Once inside your App Dashboard, scroll down to find the "WhatsApp" product. Click the "Set Up" button. This action links the WhatsApp Cloud API to your Meta developer application. Meta will automatically generate a temporary access token and a test phone number for you to use during development.

Step 3: Register Your Recipient Phone Number

Before sending messages with a test number, you must whitelist your recipient phone number. In the WhatsApp "Getting Started" panel, look for the "To" phone number dropdown menu. Select "Manage phone number list" and add your personal WhatsApp phone number. Meta will send a verification code via WhatsApp to your device. Enter this code on your screen to authorize the number.

Step 4: Locate Your API Credentials

To write your API request, you need three key pieces of information from your dashboard: the Temporary Access Token, the Phone Number ID, and the WhatsApp Business Account ID. Keep these values secure, as they grant access to your messaging channel.

Step 5: Send a Test Template Message Using cURL

The easiest way to test your API integration is by using a standard cURL command in your terminal. Meta provides a default template called "hello_world" for testing. Replace the placeholder values in the command below with your actual Phone Number ID, Access Token, and whitelisted recipient phone number.

curl -X POST \
"https://graph.facebook.com/v17.0/YOUR_PHONE_NUMBER_ID/messages" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"messaging_product": "whatsapp",
"to": "RECIPIENT_PHONE_NUMBER",
"type": "template",
"template": {
"name": "hello_world",
"language": {
"code": "en_US"
}
}
}'

Run this command in your terminal. You will receive an instant push notification on your whitelisted phone number containing the pre-approved "hello_world" message from Meta.

Step 6: Handle the API Response

A successful API call returns a JSON payload containing a unique message ID and contact details. A typical response looks like this:

{
"messaging_product": "whatsapp",
"contacts": [ { "input": "RECIPIENT_PHONE_NUMBER", "wa_id": "RECIPIENT_PHONE_NUMBER" } ],
"messages": [ { "id": "wamid.HBgLMTU1NTg2NzUzMDQVAgARGBI4RDN..." } ]
}

If you receive an error code instead, double-check that your access token has not expired. Temporary tokens are only valid for 24 hours. You must generate a permanent access token in your Meta Business Suite to run production applications.

Safety, privacy and policy notes

Meta enforces strict rules to maintain user trust and prevent spam on WhatsApp. Before you begin messaging real customers, you must understand their safety and privacy frameworks.

First, you must collect explicit opt-in consent from your users before sending them proactive messages. This consent cannot be assumed. You must obtain it through a checkbox on a signup form, an email confirmation, or an interactive chat menu. If users block or report your number for spam, Meta will dynamically lower your quality rating, which can result in temporary messaging limits or permanent account suspension.

Second, all automated business-initiated messages must use templates. Meta reviews these templates to ensure they do not violate their Commerce Policy. For instance, templates promoting firearms, adult content, or gambling are strictly prohibited. Templates are categorized into Utility (updates on transactions), Authentication (OTPs), and Marketing (promotional offers). Each category has a different per-message pricing model.

Finally, WhatsApp messages sent via the Cloud API are encrypted in transit. Meta hosts the Cloud API infrastructure, ensuring compliance with global data protection standards like GDPR and CCPA. However, you are still responsible for protecting the API tokens and customer phone numbers stored within your own databases.

Best tools and alternatives

Depending on your technical resources and business needs, you have several options for connecting to the WhatsApp API.

The official Meta Cloud API is the most direct and cost-effective route. It is free to set up, and you only pay Meta's wholesale conversation rates. However, it requires software development experience to build a user interface, manage webhooks, and store conversation histories.

For non-developers or teams looking for a simple, ready-to-use alternative, you can use a Business Solution Provider (BSP). Companies like Twilio, Vonage, or Infobip offer managed APIs. They provide SDKs, customer support, and advanced routing engines, but they charge an extra markup fee on top of Meta's standard messaging rates.

If you do not need automated backend integrations and simply want a quick way for customers to start a chat with you, a simple link generator is an excellent alternative. You can use WA Link (create.wa.link) to generate custom click-to-chat links. This tool allows users to click a URL and instantly open a pre-filled chat with your business on their phone, completely bypassing the need for complex API setups or coding.

Frequently Asked Questions

Is the WhatsApp Business API free?

No, the WhatsApp Business API operates on a conversation-based pricing model. Meta charges per 24-hour conversation window, and rates vary based on the recipient's country code and the conversation category (Marketing, Utility, Authentication, or Service). However, Meta offers 1,000 free Service (user-initiated) conversations every month per WhatsApp Business Account.

Can I send promotional messages using the WhatsApp API?

Yes, you can send promotional and marketing messages using the API. However, you must submit these messages to Meta as "Marketing" templates for approval before sending them. You must also ensure that you have obtained explicit opt-in consent from the recipients to avoid spam complaints.

What is the difference between Cloud API and On-Premises API?

The Cloud API is hosted directly on Meta's servers, making it faster to set up and easier to maintain since Meta handles all scaling and updates automatically. The On-Premises API requires you or a Business Solution Provider (BSP) to host the API client on your own servers, which demands more technical maintenance and infrastructure costs.

Do I need a Facebook Business Manager account to use the API?

Yes, you need a verified Meta Business Manager account to move your WhatsApp API integration out of development mode and into production. Verification requires you to upload official business registration documents to prove your company's identity.

Can I use my existing personal WhatsApp phone number for the API?

You can use your existing number, but you must first delete the personal or business WhatsApp application account associated with that number. A phone number cannot be active on the physical WhatsApp mobile app and the WhatsApp Business API at the same time.

How long does it take for a message template to get approved?

Meta uses automated systems to review message templates. Most template submissions are approved or rejected within a few minutes, though some complex templates may take up to 24 hours for manual review.

What happens if a user replies to an API-sent message?

When a user replies, Meta sends a real-time event payload to your configured webhook URL. Your backend application must listen to this webhook to capture the reply text, media, or button clicks, allowing your system or support agents to respond.

Can I send media files like PDFs or images through the API?

Yes, the API supports rich media. You can send images, PDFs, audio files, videos, and interactive buttons. For template messages, you must include media placeholders in your template design during the Meta approval process.

What is the 24-hour customer service window?

The customer service window is a 24-hour timer that starts the moment a user sends a message to your business. Within this window, you can send free-form "Session Messages" without template restrictions. Once the 24 hours expire, you can only message the user again using an approved Template Message.

Conclusion

Sending WhatsApp messages using the API allows your business to automate customer touchpoints at scale. By leveraging Meta's Cloud API, you can easily integrate secure, reliable messaging into your existing software stack. Whether you build directly on Meta's infrastructure or use simplified alternatives like WA Link for basic customer routing, establishing a programmatic presence on WhatsApp is key to modernizing your customer communications.