WhatsApp Business API For sending messages
To get an API access token for sending WhatsApp messages, you typically need to use the WhatsApp Business API. Here’s a step-by-step guide on how to obtain the token:
Steps to Obtain WhatsApp API Access Token
Create a Facebook Developer Account
- If you don’t already have one, create a Facebook Developer account at Facebook for Developers.
Set Up a WhatsApp Business Account
Go to the WhatsApp Business API page.
You will need to create a WhatsApp Business account, which can be done through the Facebook Business Manager.
Register Your Phone Number
- Register your business phone number in the WhatsApp Business Manager. This number will be used to send messages through the API.
Create an App in Facebook Developer Console
- In your Facebook Developer account, create a new app. This app will be linked to your WhatsApp Business account.
Get the API Credentials
- After setting up your app, you’ll have access to your App ID and App Secret.
Generate an Access Token
Go to the Tools section of the Facebook Developer dashboard.
Navigate to Graph API Explorer.
Select your app from the dropdown.
In the User or Page dropdown, select Get User Access Token.
Select the permissions you need (for sending messages, you might need
whatsapp_business_management
, etc.).Click the Generate Access Token button.
Get Your WhatsApp Business API Access Token (for testing)
If you’re testing or want to receive a long-lived token:
Use the API endpoint:
POST https://graph.facebook.com/v16.0/{YOUR_PHONE_NUMBER_ID}/message_templates
Replace
{YOUR_PHONE_NUMBER_ID}
with your WhatsApp Business API phone number ID. You’ll need to include the generated token in the header as:Authorization: Bearer {ACCESS_TOKEN}
Notes:
Sandbox Environment: Facebook often provides a sandbox environment that allows you to test the API without sending real messages. Make sure to check out their guidelines.
Token Expiration: User access tokens generally expire. For production systems, consider implementing a mechanism to refresh tokens or obtain long-lived tokens.
API Limits: Be aware of the API usage limits and the policies regarding message sending to avoid being flagged for spam.
Costs: WhatsApp Business API may have associated costs, especially when sending messages to users outside your existing customer base.
Example of Using an Access Token
Once you have the access token, you can send messages using a cURL command or through a PHP script as such:
$access_token = 'YOUR_ACCESS_TOKEN';
$to = 'RECIPIENT_PHONE_NUMBER';
$message = 'Hello, this is a test message!';
$url = "https://graph.facebook.com/v16.0/YOUR_PHONE_NUMBER_ID/messages";
$data = [
'messaging_product' => 'whatsapp',
'to' => $to,
'text' => [
'body' => $message
]
];
$options = [
'http' => [
'header' => "Content-type: application/json\r\n" .
"Authorization: Bearer $access_token\r\n",
'method' => 'POST',
'content' => json_encode($data),
],
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {
die('Error occurred');
}
echo $result;
Feel free to ask if you need further information about any of these steps!