# ⚡ TechSwiftTrix — M-Pesa STK Push
A clean, fully functional M-Pesa STK Push payment application built with Node.js + Express and Safaricom's Daraja API.
---
## 🚀 Quick Start
### 1. Install dependencies
```bash
npm install
```
### 2. Configure credentials
```bash
cp .env.example .env
```
Open `.env` and fill in your M-Pesa Daraja API credentials:
- **MPESA_CONSUMER_KEY** — from
developer.safaricom.co.ke
- **MPESA_CONSUMER_SECRET** — from the same portal
- **MPESA_SHORTCODE** — your Paybill or Till number
- **MPESA_PASSKEY** — your Lipa Na M-Pesa Online Passkey
- **MPESA_CALLBACK_URL** — a public HTTPS URL (see below)
### 3. Set up a callback URL (for local testing)
Install ngrok and run:
```bash
ngrok http 3000
```
Copy the HTTPS URL (e.g. `
abc123.ngrok-free.app`) and set:
```
MPESA_CALLBACK_URL=
abc123.ngrok-free.app
```
### 4. Start the server
```bash
npm start
```
Or for development with auto-reload:
```bash
npm run dev
```
### 5. Open in browser
```
localhost
```
---
## 📁 Project Structure
```
mpesa-stk-push/
├── server.js # Express server entry point
├── package.json
├── .env.example # Environment variables template
├── config/
│ └── mpesa.js # M-Pesa config (reads from .env)
├── routes/
│ └── mpesa.js # STK push, callback, query routes
└── public/
└── index.html # Frontend payment page
```
---
## 🔌 API Endpoints
| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/api/mpesa/stkpush` | Initiate STK push |
| `POST` | `/api/mpesa/callback` | Safaricom callback (auto-called) |
| `POST` | `/api/mpesa/query` | Query transaction status |
### STK Push payload
```json
{
"phone": "0712345678",
"amount": 100
}
```
---
## 🧪 Sandbox Testing
Use Safaricom's test credentials from the Daraja portal.
Test phone numbers accepted in sandbox: **254708374149**
---
## 🔐 Environment Variables
| Variable | Description |
|-- …