# π± M-Pesa STK Push Demo β Daraja API Integration
> A production-ready M-Pesa STK Push integration using Safaricom's Daraja API. Enter a phone number and amount β an M-Pesa prompt fires on the customer's phone instantly.
## β¨ Features
- **Real STK Push** β Triggers actual M-Pesa prompts via Daraja sandbox
- **OAuth2 token generation** β Auto-fetches and uses Daraja access tokens
- **Secure password hashing** β `Base64(shortcode + passkey + timestamp)`
- **Callback URL handler** β Receives and processes Safaricom payment confirmations
- **Status polling** β Frontend polls every 3s until confirmed or failed
- **Phone number validation** β Accepts 07XX, 01XX, +254, 254 formats
- **Amount presets** β Quick KES 50 / 100 / 500 / 1K / 2.5K / 5K buttons
- **90s timeout** β Graceful expiry with retry option
- **Go-live ready** β Swap 4 env vars, zero code changes
## π Quick Start
### 1. Get Daraja credentials (free)
1. Go to developer.safaricom.co.ke
2. Create an account β Create App β Select **Lipa Na M-Pesa Sandbox**
3. Copy your **Consumer Key** and **Consumer Secret**
### 2. Clone & install
```bash
git clone
github.com
cd mpesa-demo
npm install
```
### 3. Configure environment
```bash
cp .env.local.example .env.local
```
Edit `.env.local`:
```env
MPESA_CONSUMER_KEY=your_consumer_key
MPESA_CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=174379
MPESA_PASSKEY=bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919
NEXT_PUBLIC_BASE_URL=
your-app.vercel.app
```
### 4. Deploy to Vercel first (callback URL needs to be public)
```bash
git init && git add . && git commit -m "feat: M-Pesa STK Push integration"
git remote add origin
github.com
git push -u origin main
```
Then: **vercel.com β New Project β Add env vars β Deploy**
Update `NEXT_PUBLIC_BASE_URL` to your Vercel URL.
## ποΈ Project Structure
```
mpesa-demo/
βββ app/
β βββ api/mpesa/
β β βββ token/route.ts β¦