Class MpesaConfig

java.lang.Object
io.github.openpaydev.mpesa.core.MpesaConfig

public class MpesaConfig extends Object
Configuration object for the Mpesa API client.

This class holds all the necessary credentials and settings required to interact with the Mpesa API. It is immutable and should be created once and reused throughout your application.

An instance can be created using the builder pattern or from environment variables.

Example using the builder:


 MpesaConfig config = MpesaConfig.builder()
         .consumerKey("your_key")
         .consumerSecret("your_secret")
         .businessShortCode("174379")
         .passKey("your_passkey")
         .environment(MpesaEnvironment.SANDBOX)
         .build();
 
  • Constructor Details

  • Method Details

    • fromEnv

      public static MpesaConfig fromEnv()
      Creates a MpesaConfig instance by reading credentials from environment variables.

      This is the recommended way to configure the SDK in a production environment. It uses System.getenv() as the source for the variables.

      Returns:
      A new MpesaConfig instance.
    • builder

      public static MpesaConfig.MpesaConfigBuilder builder()
    • getConsumerKey

      public String getConsumerKey()
      The Consumer Key obtained from the Safaricom Developer Portal.
    • getConsumerSecret

      public String getConsumerSecret()
      The Consumer Secret obtained from the Safaricom Developer Portal.
    • getBusinessShortCode

      public String getBusinessShortCode()
      The Business Short Code, which is the identifier for your PayBill or Till Number. Renamed from 'shortCode' to align with the official M-Pesa API documentation.
    • getPassKey

      public String getPassKey()
      The Lipa Na M-Pesa Pass Key, used to encrypt the password for STK Push requests.
    • getEnvironment

      public MpesaEnvironment getEnvironment()
      The Mpesa environment to use, either MpesaEnvironment.SANDBOX or MpesaEnvironment.PRODUCTION.