Skip to main content

This python module provides a simple way to integrate the Safaricom MPESA Daraja 2.0 API into your Python projects. It is designed to handle common operations like generating access tokens, formatting phone numbers, and sending STK Push requests.

Project description

MPESA Daraja 2.0 API Python Client Module

This python module provides a simple way to integrate the Safaricom MPESA Daraja 2.0 API into your Python projects. It is designed to handle common operations like generating access tokens, formatting phone numbers, and sending STK Push requests.

Features

  • Generate access tokens securely.
  • Format and validate phone numbers.
  • Send STK Push requests for payments.
  • Easily configurable with environment variables.

Installation

  1. Clone the repository:

    git clone https://github.com/anomalous254/daraja_client.git
    
  2. Navigate to the project directory:

    cd daraja_client
    
  3. Install dependencies:

    pip install -r requirements.txt
    

Configuration

Add the following variables to your .env file in the root directory of your project:

DARAJA_API_CONSUMER_KEY='your_consumer_key'
DARAJA_API_CONSUMER_SECRET='your_consumer_secret'
DARAJA_API_PASS_KEY='your_pass_key'
DARAJA_API_SHORT_CODE='your_shortcode'

Usage

Import the Library

from core import DarajaClient
from decouple import config

# Configuration
auth_url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
stk_push_url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest'
call_back_url = 'https://your-callback-url.com' # 
phone_number = '+254769507699' # person to receive the prompt

# Initialize the client
cl = DarajaClient(
    auth_url=auth_url,
    consumer_key=config('DARAJA_API_CONSUMER_KEY'),
    consumer_secret=config('DARAJA_API_CONSUMER_SECRET'),
    pass_key=config('DARAJA_API_PASS_KEY'),
    shortcode=config('DARAJA_API_SHORT_CODE'),
    phone_number=phone_number,
    call_back_url=call_back_url
)

# Send STK Push
response = cl.send_stk_push(stk_push_url=stk_push_url)
print(response)

Response Examples

Success

{
    "message": "success",
    "stkpushID": "ws_CO_blahblahblah",
    "info": "You can use and store this stkpushID in your db model to be used for payment confirm during callback from Safaricom"
}

Failure

{
    "message": "failed",
    "error": "Wrong credentials"
}

Acknowledgment

This library was developed by Peter Nyando, a passionate software engineer dedicated to creating scalable and efficient solutions. Check out more of my work on my portfolio.

Ensure your .env file is properly configured.

Contributing

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes and commit them.
  4. Push to your fork and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

daraja_client-0.1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

daraja_client-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file daraja_client-0.1.0.tar.gz.

File metadata

  • Download URL: daraja_client-0.1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for daraja_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff79bb53c8551b1a3817e317e62948928b5ce7658adcea36503e6d7e64c3d9e1
MD5 ff1547d74b5d19bb11c2e65551c86a77
BLAKE2b-256 c55f9d60fa3ae32efb954c5bf9aa1e46dbe09f7b99aafb25341b66fd27a7ea82

See more details on using hashes here.

File details

Details for the file daraja_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for daraja_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8cc7fe52377b88482763a3065cd2dd629bf0c39e89da6c26e3169bc8cac83d5e
MD5 2211910da137fb4fe65ca7f612565069
BLAKE2b-256 5a31836ed8b0e1d2bf3f132f8dfec43cb840165310f9fafdd9632b0890a45a2b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page