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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up