Django REST Framework backend for WhatsApp Business messaging platform with team collaboration features.
# Create virtual environment
python -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Configure environment
cp .env.example .env
# Run migrations
python manage.py migrate
# Create superuser
python manage.py createsuperuser
# Run server
python manage.py runserverServer runs at http://localhost:8000
backend/
├── config/ # Django project settings
│ ├── __init__.py
│ ├── settings.py # Main settings
│ ├── urls.py # URL routing
│ ├── wsgi.py # WSGI config
│ └── asgi.py # ASGI config
├── api/ # REST API application
│ ├── migrations/ # Database migrations
│ ├── models.py # Data models
│ ├── views.py # API views
│ ├── serializers.py # DRF serializers
│ ├── urls.py # API routes
│ ├── admin.py # Django admin config
│ └── apps.py # App configuration
├── manage.py # Django management
├── requirements.txt # Dependencies
├── .env.example # Environment template
└── .gitignore
- name: CharField
- description: TextField (optional)
- created_at: DateTimeField
- updated_at: DateTimeField
- team: ForeignKey(Team)
- user: ForeignKey(User)
- role: Choice(admin, member)
- joined_at: DateTimeField
- team: ForeignKey(Team)
- whatsapp_id: CharField (unique)
- contact_name: CharField
- contact_phone: CharField
- last_message: TextField
- last_message_at: DateTimeField
- status: Choice(active, resolved, archived)
- created_at: DateTimeField
- updated_at: DateTimeField
- conversation: ForeignKey(Conversation)
- direction: Choice(inbound, outbound)
- message_type: CharField (text, image, document, etc.)
- content: TextField
- sender_name: CharField
- created_at: DateTimeField
- is_read: BooleanField
- conversation: ForeignKey(Conversation)
- assigned_to: ForeignKey(User, null=True)
- tag_name: CharField
- tag_color: CharField (Tailwind color classes)
- note: TextField
- expiry_type: Choice(1h, 5h, end_of_day, custom)
- expires_at: DateTimeField
- created_at: DateTimeField
- created_by: ForeignKey(User)
- is_active: BooleanField
POST /api-auth/
Body: { "username": "user", "password": "pass" }
Response: { "token": "xxxxx" }
GET /api/teams/ # List teams
POST /api/teams/ # Create team
GET /api/teams/{id}/ # Get team detail
POST /api/teams/{id}/add_member/ # Add member
POST /api/teams/{id}/remove_member/ # Remove member
GET /api/conversations/ # List all conversations
GET /api/conversations/active_conversations/ # List active
GET /api/conversations/{id}/ # Get detail
POST /api/conversations/{id}/add_tag/ # Add tag
POST /api/conversations/{id}/remove_tag/ # Remove tag
GET /api/conversations/{id}/messages/ # Get messages
POST /api/conversations/{id}/messages/ # Send message
GET /api/users/current_user/ # Get logged-in user
GET /api/users/team_users/ # Get team members
GET /api/stickers/ # List your uploaded sticker images
POST /api/stickers/ # Upload a sticker image (multipart/form-data)
GET /api/stickers/{id}/ # Get sticker details
DELETE /api/stickers/{id}/ # Remove a sticker image
curl -X POST http://localhost:8000/api/conversations/1/add_tag/ \
-H "Authorization: Token YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"tag_name": "Billing Issue",
"assigned_to_id": 2,
"expiry_type": "1h",
"tag_color": "red",
"note": "Waiting for invoice"
}'1h: Expires after 1 hour5h: Expires after 5 hoursend_of_day: Expires at 23:59:59 UTCcustom: Usecustom_expiry_minutesfield
Tags are not deleted, they're marked as inactive:
curl -X POST http://localhost:8000/api/conversations/1/remove_tag/ \
-H "Authorization: Token YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"tag_id": 5}'# Django
SECRET_KEY=your-secret-key
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
# Database (SQLite for dev, PostgreSQL for prod)
DB_ENGINE=django.db.backends.sqlite3
DB_NAME=db.sqlite3
# PostgreSQL (production)
# DB_ENGINE=django.db.backends.postgresql
# DB_NAME=domi_messager
# DB_USER=postgres
# DB_PASSWORD=password
# DB_HOST=localhost
# DB_PORT=5432
# CORS
CORS_ALLOWED_ORIGINS=http://localhost:5173
# Media uploads
MEDIA_URL=/media/
MEDIA_ROOT=media
# WhatsApp API (when ready)
WHATSAPP_BUSINESS_ACCOUNT_ID=
WHATSAPP_API_TOKEN=
WHATSAPP_PHONE_NUMBER=# Show migrations
python manage.py showmigrations
# Create migration
python manage.py makemigrations
# Apply migration
python manage.py migrate
# Migrate specific app
python manage.py migrate apiAccess at http://localhost:8000/admin
Log in with your superuser credentials created during setup.
python manage.py shell
from django.contrib.auth.models import User
from api.models import Team, TeamMember, Conversation
# Create users
user1 = User.objects.create_user('agent1', 'agent1@test.com', 'pass123')
user2 = User.objects.create_user('agent2', 'agent2@test.com', 'pass123')
# Create team
team = Team.objects.create(name='Support Team')
TeamMember.objects.create(team=team, user=user1, role='admin')
TeamMember.objects.create(team=team, user=user2)
# Create conversation
Conversation.objects.create(
team=team,
whatsapp_id='1234567890',
contact_name='John Doe',
contact_phone='+1234567890'
)python manage.py test
# Test specific app
python manage.py test api
# Verbose output
python manage.py test -v 2- Set
DEBUG=Falsein.env - Generate strong
SECRET_KEY - Set up PostgreSQL database
- Configure
ALLOWED_HOSTS - Set
CORS_ALLOWED_ORIGINSto your frontend domain - Use environment variables for all sensitive data
- Set up HTTPS/SSL
- Configure static files serving
- Set up error logging
pip install gunicorn
gunicorn config.wsgi:applicationCreate Dockerfile:
FROM python:3.10
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]# Reset database (development only)
rm db.sqlite3
python manage.py migrate
python manage.py createsuperuserpython manage.py makemigrations --mergepython manage.py clear_cache{
"id": 1,
"name": "Team Name",
...
}{
"error": "Error message"
}All endpoints require token authentication:
curl -H "Authorization: Token YOUR_TOKEN" http://localhost:8000/api/teams/Get token by logging in:
curl -X POST http://localhost:8000/api-auth/ \
-d "username=user&password=pass"