MpesaElixir.API (MpesaElixir v0.2.1)
HTTP client for interacting with the M-Pesa API.
This module provides a lightweight wrapper around the Req HTTP client, specifically configured for making requests to the Safaricom M-Pesa API. It handles authentication, request formatting, and environment-specific base URLs.
Features
- Automatic bearer token authentication
- JSON request body encoding
- Content-type header management
- Environment-aware base URL selection (sandbox vs production)
- Simplified request interface
Examples
# Simple request to an M-Pesa endpoint
MpesaElixir.API.request("/mpesa/stkpush/v1/processrequest",
body: %{
BusinessShortCode: "174379",
Password: "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMjAwMjE2MTY1NjI4",
Timestamp: "20200216165628",
TransactionType: "CustomerPayBillOnline",
Amount: 1,
PartyA: "254712345678",
PartyB: "174379",
PhoneNumber: "254712345678",
CallBackURL: "https://example.com/callback",
AccountReference: "Test",
TransactionDesc: "Test"
}
)
# Creating a customized client for multiple requests
client = MpesaElixir.API.new(
base_url: "https://custom-mpesa-proxy.example.com",
retry: [max_attempts: 3]
)
Req.post(client, url: "/some/endpoint", body: %{key: "value"})
Configuration
Configure the environment in your application config:
# For sandbox environment
config :mpesa_elixir, sandbox: true
# For production environment
config :mpesa_elixir, sandbox: false