2 min read

Send WhatsApp Messages with Node.js and Python: Complete Tutorial

Sending your first WhatsApp message via API requires fewer than 20 lines of code. This tutorial shows how to do it in Node.js and Python with Chat API, including error handling and sending templates with variables.

Send WhatsApp Messages with Node.js and Python: Complete Tutorial

Initial setup and authentication

Register a Chat API account, create an API key in the control panel and save the key as an environment variable. Never embed the key directly in source code.

For Node.js, install axios or use native fetch. For Python, install requests. Both libraries allow making HTTP requests to Chat API's endpoint.

Node.js example: message and template sending

The Node.js code for sending a simple text message is a POST call to Chat API's /messages endpoint with the recipient's number, message type ('text') and text. The response includes the message ID for tracking.

For template sending with variables, add the 'template' field to the request body with the template name and variable values.

Proper error handling involves verifying the HTTP response code and logging the response body on errors for debugging.

Python example: sending with retry logic

In Python, the tenacity library or a simple for loop allows implementing retry logic with exponential backoff. This is essential for handling rate limits and transient timeouts without losing messages.

Handling events via webhook

To receive message status updates, configure a webhook endpoint in the Chat API panel. In Node.js, Express.js allows creating a POST endpoint in a few lines that receives and processes events.

Chat API's technical documentation includes complete examples for Node.js, Python, PHP and Go. The Chat API sandbox allows testing sending and receiving without real messages to end customers during development.

Chat API

Ready to integrate WhatsApp into your business?

Activate your Chat API account and start sending messages in minutes.