Skip to main content

MoMoPSBAPI Python SDK Documentation

Welcome to the MoMoPSBAPI Python SDK! This SDK enables developers to integrate with the MTN MoMo API for managing API users, API keys, OAuth 2.0 tokens, and payment processing in a seamless manner. Below, you'll find a comprehensive guide on installation, usage, and functionality.


Features

  • Create and manage API Users
  • Generate API Keys for secure communication
  • Obtain OAuth 2.0 tokens for authentication
  • Initiate and manage payment requests
  • Validate API responses

Requirements

Sandbox/Testing Users:

  1. MTN MoMo Developer Account: Sign Up Here
  2. Collection and Disbursement subscription keys

Production Users:

  1. Collection and Disbursement subscription keys
  2. API User ID and API Key

Installation

Install the required dependencies:

pip install requests

Quickstart Guide

Initialize the SDK

from momo_psb import MoMoPSBAPI

# Replace with your details
BASE_URL = "https://sandbox.momodeveloper.mtn.com"
SUBSCRIPTION_KEY = "your_subscription_key"

# Initialize the API client
api = MoMoPSBAPI(base_url=BASE_URL, subscription_key=SUBSCRIPTION_KEY)

Key API Operations

1. Create API User

Generate a unique API User.

import uuid

reference_id = str(uuid.uuid4())  # Generate a unique reference ID
callback_host = "https://your-callback-host.com"

response = api.create_api_user(reference_id, callback_host)
print("Status Code:", response.status_code)
print("Response:", response.json())

2. Generate API Key

Create an API Key for the generated API User.

api_user = "your_api_user_id"
response = api.create_api_key(api_user)
print("Status Code:", response.status_code)
print("API Key:", response.json().get("apiKey"))

3. Retrieve API User Details

Fetch details of an API User.

response = api.get_api_user_details(api_user)
print("Status Code:", response.status_code)
print("User Details:", response.json())

4. Obtain OAuth 2.0 Token

Generate an access token for authorization.

api_key = "your_api_key"
response = api.get_oauth_token(api_user, api_key)
print("Status Code:", response.status_code)
token_data = response.json()
access_token = token_data.get("access_token")
print("Access Token:", access_token)

5. Request to Pay

Initiate a payment request from a consumer.

payer_info = {
    "partyIdType": "MSISDN",
    "partyId": "+2348056042384"
}
reference_id = str(uuid.uuid4())

response = api.request_to_pay(
    reference_id=reference_id,
    access_token=access_token,
    amount="100.00",
    currency="EUR",
    external_id=str(uuid.uuid4()),
    payer=payer_info,
    payer_message="Payment for services",
    payee_note="Thank you for your payment"
)

print("Status Code:", response.status_code)
print("Response:", response.json())

Error Handling

Use the validate_response method to handle errors gracefully.

try:
    data = api.validate_response(response)
    print("Success:", data)
except Exception as e:
    print("Error:", e)

Notes

  1. Environment Setup:
    • Default environment is sandbox. Update the base URL for production use.
  2. Status Codes:
    • 200, 201, 202: Successful operations.
    • Other codes indicate errors and should be handled appropriately.
  3. Unique Identifiers:
    • Use uuid.uuid4() to generate reference IDs for requests.

Additional Resources


Metadata

Release files for momo-psb 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for momo-psb 0.1.0
File Size Uploaded Provenance Actions
momo_psb-0.1.0.tar.gz 8.2 kB Without provenance Details for momo_psb-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for momo-psb 0.1.0
File Interpreter ABI Platform Actions
momo_psb-0.1.0-py3-none-any.whl Python 3 none any Details for momo_psb-0.1.0-py3-none-any.whl

Total release size: 13.5 kB

Release files / momo_psb-0.1.0.tar.gz

Download URL momo_psb-0.1.0.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5a74909938c926f7efa105e86411c95472f704fbf973b9336be1324d3d9632b8
BLAKE2b-256 checksum
How to use checksums
041fd31bd3825b59c73a7a63b2fe3905e08e049dd91d193cecc9fa5a93e622aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Release files / momo_psb-0.1.0-py3-none-any.whl

Download URL momo_psb-0.1.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
367ad458557387d36888ebfd721b46af9b54bc2010e487f2543b94b8580cc3bb
BLAKE2b-256 checksum
How to use checksums
a3e85a569efa8978d2be98b990df16225dc5e7dfe607efe946ebc58608177298
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.13

Security for momo-psb 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page