DEV Community

Cover image for πŸš€ Send WhatsApp Messages via API with 2Chat
Federico Roman
Federico Roman

Posted on • Edited on

πŸš€ Send WhatsApp Messages via API with 2Chat

This quick tutorial will guide you through sending messages via WhatsApp using the 2Chat API.

βœ… Requirements

  1. A 2Chat account with a connected WhatsApp number.
  2. That’s it! You’re ready to go.

πŸ“Œ Steps

  1. Create a 2Chat account and connect your WhatsApp number in the Channel section.
  2. Grab your API Key from here.
  3. Now, let’s send a message using Python!

🐍 Sending a WhatsApp Message with Python

Imagine you have a sender number (+5959123123) and want to message a recipient (+59594545). Here’s how you do it:

import requests
import json

url = "https://api.p.2chat.io/open/whatsapp/send-message"

payload = json.dumps({
  "to_number": "+59594545",
  "from_number": "+5959123123",
  "text": "Hi!",
})

headers = {
  'X-User-API-Key': 'your_api_key_here',
  'Content-Type': 'application/json'
}

response = requests.post(url, headers=headers, data=payload)

print(response.json())  # Check the response
Enter fullscreen mode Exit fullscreen mode

🎯 That’s It!

With just a few lines of code, you’ve successfully sent a WhatsApp message via API. For more details and additional API functionalities, check out the official 2Chat API Docs.

Top comments (0)