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.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay