Wifi Network: Tribe Kampala
Wifi Key: LXWX1H
- Introduction to the API
- Basic Auth
- API Requests with Curl
- Sample Code Walkthrough
- Best Practices
First, what is an API?
"a set of functions and procedures that allow the creation of applications which access the features or data of an operating system, application, or other service." - Wikipedia
- Sign Up
- Subscribe to a Product
- Generate API User and API Key
Head over the to domain below
https://momodeveloper.mtn.com/signup
After creating an account, head on over to the products page. The list of products:
- Collections
- Disbursements
- Remittance
- Collection Widget
Click on Collections
and hit the Subscribe
button. This generates your API Key
. The API is unique to the product and you will need an API Key
for each product you use.
Currently, there are two ways to create these:
- Using Curl
- Using the Sparkplug MoMo Python library
First we generate a UUID on bash
$ uuidgen
First we generate a UUID with python
$ curl -v -X POST -H "X-Reference-Id: UUID" -H "Ocp-Apim-Subscription-Key: APIKey"
-H "Content-Type: application/json" -d '{"providerCallbackHost": "sparkpl.ug"}' https://ericssonbasicapi2.azure-api.net/v1_0/apiuser
Encode ApiKey:ApiSecret as base64 string. Pass it as a header in your requests using commandline
$ curl -i -X POST -H "Content-Type:application/json" -H "Ocp-Apim-Subscription-Key: ApiKey" -d '{}' https://ericssonbasicapi2.azure-api.net/v1_0/apiuser/UserId/ApiKey
$ openssl enc -base64 <<< 'UUID'
Where 41187126-ab69-46ed-b7a5-7f3316c7942e
is the ApiKey 9ad0de137e194567bf310590b9471a63
is the API Secret
Basic Auth using Python
>> import base64
>> base64.b64encode('41187126-ab69-46ed-b7a5-7f3316c7942e:9ad0de137e194567bf310590b9471a63'.encode())
Lets use basic auth to get an Auth Token
with curl
$ curl -i -X POST -H "Content-Type:application/json" -H "Authorization: Basic NDExODcxMjYtYWI2OS00NmVkLWI3YTUtN2YzMzE2Yzc5NDJlOjlhZDBkZTEzN2UxOTQ1NjdiZjMxMDU5MGI5NDcxYTYz" -d {} https://ericssonbasicapi2.azure-api.net/colection/token/
This returns the token. Lets use this token to do bearer token authentication. This is what we shall use for the rest of the API calls.
https://github.com/sparkplug/momoapi-python
Follow the README
git clone https://github.com/sparkplug/momoapi-python
Creation of virtualenv:
$ virtualenv -p python3 <desired-path>
Activate the virtualenv:
$ source <desired-path>/bin/activate
Next, we install from source:
$ python setup.py install
$ momoapi
$ providerCallBackHost: https://akabbo.ug
$ Ocp-Apim-Subscription-Key: f83xx8d8xx6749f19a26e2265aeadbcdeg
where providerCallBackHost
is your callback host and Ocp-Apim-Subscription-Key
is your API key for the specific product to which you are subscribed.
You should get the following response.
Here is your User Id and API secret : {'apiKey': 'b0431db58a9b41faa8f5860230xxxxxx', 'UserId': '053c6dea-dd68-xxxx-xxxx-c830dac9f401'}
There are two main APIs for payments:
- requestToPay
- transfer
- This operation is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment.
- The transaction will be executed once the payer has authorized the payment.
- The requesttopay will be in status PENDING until the transaction is authorized or declined by the payer or it is timed out by the system.
POST /v1_0/requesttopay
https://app.swaggerhub.com/apis-docs/sparkplug/collection/1.0#/default/requesttopay-POST
from momoapi.client import MomoApi
client = MomoApi(APIKEY,USERID,APISECRET)
ref=client.requestToPay("256772123456", "600", "123456789", note="dd", message="dd", currency="EUR", environment="sandbox")
So, what just happened? We create a client on the commandline, and made a requestToPay transaction.
>>> ref
You should see a response similar to this:
>>> ref
{'transaction_ref': '33a9d94b-6828-4879-xxxx-e0ecb946d465'}
So, what just happened? We create a client on the commandline, and made a requestToPay transaction.
>>> ref
You should see a response similar to this:
>>> ref
{'transaction_ref': '33a9d94b-6828-4879-xxxx-e0ecb946d465'}
We can then use this Transaction Reference
to get the status of the Transaction
>>> client.getTransactionStatus(ref['transaction_ref'])
{'financialTransactionId': '1854386795', 'externalId': '123456789', 'amount': '600', 'currency': 'EUR', 'payer': {'partyIdType': 'MSISDN', 'partyId': '256794631873'}, 'payerMessage': 'dd', 'payeeNote': 'dd', 'status': 'SUCCESSFUL'}
Voila!
>>> client.getBalance()
https://github.com/sparkplug/momoapi-python https://github.com/sparkplug/momoapi-node https://github.com/sparkplug/momoapi-example-app https://github.com/sparkplug/momo-developer-training-slides
Send Pull Requests
Feedback form: http://bit.do/apitraining
Community Chat: https://spectrum.chat/momo-api-developers/
Thank you.