M-Pesa API client implementations in Java and Python
# M-Pesa API Client
This repository contains client implementations for the M-Pesa API in both Java and Python.
## Features
- **Bearer Token Generation**: Encrypts API key using RSA public key encryption
- **Customer Name Query**: Makes API requests to query customer information
- **Error Handling**: Comprehensive error handling and debugging information
- **Secure Configuration**: Uses environment variables for API credentials
## Files
- `MpesaClient.java` - Java implementation with HTTP client
- `mpesa_client.py` - Python implementation with requests library
- `requirements.txt` - Python dependencies
- `commons-codec-1.19.0/` - Apache Commons Codec library for Java
- `.env.example` - Example environment configuration file
## Setup
### 1. Configuration
Copy the example environment file and add your credentials:
```bash
cp .env.example .env
```
Edit `.env` file and add your M-Pesa API credentials:
```bash
MPESA_API_KEY=your_actual_api_key
MPESA_PUBLIC_KEY=your_actual_public_key
```
**Note**: Get your API key and public key from the M-Pesa Developer Portal
### 2. Java Setup
```bash
# Set environment variables (Linux/Mac)
export MPESA_API_KEY="your_api_key"
export MPESA_PUBLIC_KEY="your_public_key"
# Compile
javac -cp "commons-codec-1.19.0/commons-codec-1.19.0.jar" MpesaClient.java
# Run
java -cp ".:commons-codec-1.19.0/commons-codec-1.19.0.jar" MpesaClient
```
### 3. Python Setup
```bash
# Install dependencies
pip install -r requirements.txt
# Set environment variables (Linux/Mac)
export MPESA_API_KEY="your_api_key"
export MPESA_PUBLIC_KEY="your_public_key"
# Run
python mpesa_client.py
```
### Windows Environment Variables
```cmd
# Set environment variables (Windows)
set MPESA_API_KEY=your_api_key
set MPESA_PUBLIC_KEY=your_public_key
```
## API Endpoints
Currently implements:
- **Customer Name Query** - GET `/ipg/v1x/queryCustomerName/`
## Configuration
Both implementations read credentials from environment variables:
- `MPESA_API_KEY` - Your M-P …