A lightweight, zero-dependency Go boilerplate for Safaricom Daraja API integration. Features automated STK push triggers, dynamic password generation, and a plug-and-play HTTP callback handler configurable entirely via environment variables.
A production-ready, highly reusable M-Pesa (Daraja) API gateway service built in Go. Designed around Clean Architecture, SRE best practices, and high-performance, dependency-minimal principles.
This service acts as a secure, centralized payment gateway. It shields your Daraja credentials from client-side frontends, maintains transaction state within PostgreSQL, handles asynchronous webhooks, and serves a secure polling endpoint for client UIs.
sync.RWMutex to eliminate outbound authentication bottlenecks and protect against concurrent refresh race conditions.log/slog which automatically aligns with Google Cloud Logging when deployed on Cloud Run.golang.org/x/time/rate with background garbage collection of inactive IPs to prevent memory leaks..env file loader and self-bootstrapping database migration scripts leveraging Go’s native go:embed.SIGINT/SIGTERM to safely drain connection pools and active operations.mpesa-service-go/
├── cmd/
│ └── api/
│ └── main.go # Application entrypoint & graceful shutdown setup
├── config/
│ └── config.go # Low-overhead configuration & .env loader
├── internal/
│ ├── domain/
│ │ ├── repository.go # Storage interface contracts
│ │ └── transaction.go # Entities and webhook JSON payload mappings
│ ├── delivery/
│ │ └── http/
│ │ ├── handlers.go # HTTP JSON bindings & response writers
│ │ ├── middleware.go # CORS, Logging, Rate Limiter & API Key Auth
│ │ └── router.go # Go 1.22+ method-matching multiplexer
│ ├── usecase/
│ │ └── mpesa_usecase.go # Transaction orchestrator & ResultCode translator
│ └── infrastructure/
│ ├── daraja/
│ │ └── client.go # Low-level token manager & Safaricom client
│ └── repository/
│ ├── postgres.go # Connection pooling & auto-schema executor
│ └── schema.sql # Embedded PostgreSQL 17 relational schema
├── .env # Local configurations (git-ignored)
├── Dockerfile # Multi-stage secure build
└── go.mod
Clone the repository:
git clone https://github.com/lxmwaniky/mpesa-service-go.git
cd mpesa-service-go
Configure Environment Variables:
Create a .env file at the root:
DB_USER=mpesa_secure_user
DB_PASSWORD=mpesa_secure_password
DB_NAME=mpesa_db
DB_PORT=5432
DB_HOST=localhost
PORT=8080
MPESA_ENV=sandbox
MPESA_CONSUMER_KEY=your_daraja_key
MPESA_CONSUMER_SECRET=your_daraja_secret
MPESA_PASSKEY=your_daraja_passkey
MPESA_SHORTCODE=your_shortcode
MPESA_CALLBACK_URL=https://xxxx.ngrok-free.app/api/v1/mpesa/callbacks/stk
API_KEY=your_pre_shared_secret_api_key
Start PostgreSQL 17:
docker compose up -d
Run the Go Application:
go run cmd/api/main.go
Triggers a payment prompt to a user's phone.
POST /api/v1/mpesa/stk-pushX-API-Key: your_pre_shared_secret_api_keyContent-Type: application/json{
"external_reference": "invoice-1024",
"phone_number": "254701343452",
"amount": 1.00,
"description": "Payment for invoice 1024"
}
202 Accepted):
{
"id": 1,
"external_reference": "invoice-1024",
"merchant_request_id": "4413-468c-b949-a0f46ce2524393104",
"checkout_request_id": "ws_CO_03062026074701134701343452",
"phone_number": "2547XXXXXXXX",
"amount": 1,
"status": "PENDING",
"result_code": 0,
"transaction_type": "STK_PUSH",
"created_at": "2026-06-03T07:46:59Z",
"updated_at": "2026-06-03T07:46:59Z"
}
Checks the status of an ongoing transaction. Translates cryptic M-Pesa error codes into actionable user-facing messages.
GET /api/v1/mpesa/status/{extRef}X-API-Key: your_pre_shared_secret_api_key200 OK - Success):
{
"id": 1,
"external_reference": "invoice-1024",
"status": "SUCCESS",
"mpesa_receipt_number": "UF3N06DVFC",
"user_message": "Payment completed successfully.",
"amount": 1
}
200 OK - Wrong PIN failure):
{
"id": 2,
"external_reference": "invoice-1025",
"status": "FAILED",
"user_message": "Incorrect M-Pesa PIN entered. Please try again.",
"amount": 1
}
Registers the validation and confirmation webhooks dynamically with Safaricom G2 platform.
POST /api/v1/mpesa/c2b/registerX-API-Key: your_pre_shared_secret_api_keyContent-Type: application/json{
"validation_url": "https://your-domain.com/api/v1/mpesa/callbacks/c2b/validation",
"confirmation_url": "https://your-domain.com/api/v1/mpesa/callbacks/c2b/confirmation"
}
200 OK):
{
"message": "C2B URLs registered successfully"
}
Public endpoint for SRE observability, readiness, and liveness probes. Checks database ping connectivity under the hood.
GET /health200 OK):
{
"status": "UP"
}
503 Service Unavailable):
{
"status": "DOWN",
"error": "sql: database is closed"
}
| Variable | Description | Required | Default |
|---|---|---|---|
PORT | The port the Go web server binds to | No | 8080 |
DB_USER | PostgreSQL user | Yes | mpesa_user |
DB_PASSWORD | PostgreSQL password | Yes | mpesa_password |
DB_NAME | PostgreSQL database name | Yes | mpesa_db |
DB_PORT | PostgreSQL internal mapping port | No | 5432 |
DB_HOST | Database host | Yes | localhost |
MPESA_ENV | Target environment (sandbox or production) | Yes | sandbox |
MPESA_CONSUMER_KEY | Safaricom developer Portal Consumer Key | Yes | — |
MPESA_CONSUMER_SECRET | Safaricom developer Portal Consumer Secret | Yes | — |
MPESA_PASSKEY | Lipa Na Mpesa Online Passkey | Yes | — |
MPESA_SHORTCODE | Paybill / Buy Goods shortcode | Yes | — |
MPESA_CALLBACK_URL | Public secure HTTPS endpoint for webhooks | Yes | — |
API_KEY | Pre-shared API Key for securing client access | Yes | — |
os.Interrupt and syscall.SIGTERM. On receipt, stops processing new requests and initiates a 15-second draining window allowing active database connections and transactions to close securely.UPDATE transactions
SET mpesa_receipt_number = $1, status = $2, result_code = $3, result_desc = $4, updated_at = $5
WHERE checkout_request_id = $6 AND status = 'PENDING'
PENDING and returns an immediate 200 OK response to silence Safaricom’s retry queue, protecting application throughput.timestamp, level, message, transaction_id, etc.) to facilitate seamless integration with log aggregation platforms and enable powerful querying and alerting capabilities./health endpoint performs a lightweight database ping to ensure the service is not only running but also has connectivity to its critical dependencies, providing accurate signals for Kubernetes liveness and readiness probes.A lightweight, zero-dependency Go boilerplate for Safaricom Daraja API integration. Features automated STK push triggers, dynamic password generation, and a plug-and-play HTTP callback handler configurable entirely via environment variables.