This page gives a brief introduction to the library. It assumes you have the library installed, if you don’t check the Installing portion.
Get your API keys, Credential
For sandbox environement you just need to Signup if it's your first time to use MTN MOMO API or Signin, then subscribe for a production to get your Credential
For production environement, API USER & API KEY & CREDENTIAL are provided by MTN. you need to GO LIVE, submit your document to get all live credential
Quick Example
const { Client } = require('mobilemoney.js');
(async()=>{
let user = new Client();
user.isSandbox();
let subscriptionKey = "COLLECTION_OR_DISBURSEMENT_CREDENTIAL_MAKE_SURE_TO_REPLACE_IT_CORRECTLY";
// creating uuid version 4 from the library
let uuid = user.getReferenceId();
console.log(`UUID : ${uuid}`)
// Creating user in sandbox env
let [done, ] = await user.createApiUser(uuid, subscriptionKey);
if (done){
console.log('Create successfully');
console.log(done);
}
let [, apiUser] = await user.getApiUser(uuid, subscriptionKey)
console.log(`apiUser : `)
console.log(apiUser)
let [, data] = await user.createApiKey(uuid, subscriptionKey)
// Convert to basic token from apiuser & apikey, Library do it for you
let basicToken = user.basicToken(uuid, data.apiKey)
console.log(`Basic Token : ${basicToken}`)
// Getting collection product with credential
let collection = user.collection(subscriptionKey)
// Creating access token for collection product
let [,accessToken] = await collection.createAccessToken(basicToken)
accessToken = accessToken.access_token
console.log(`AccessToken : ${accessToken}`)
// Convert to bearer token from access token
let bearerToken = user.bearerToken(accessToken)
console.log(`Bearer Token : ${bearerToken}`)
//request to pay
let body = {
"amount": "5",
"currency": "EUR",
"externalId": "45464546454",
"payer": {
"partyIdType": "MSISDN",
"partyId": "87937389"
},
"payerMessage": "BUY THING",
"payeeNote": "THANKS"
}
let [reqToPay,] = await collection.requestToPay(bearerToken,
user.getReferenceId(),
apiUser.targetEnvironment,
body)
console.log(`Request to Pay : ${reqToPay}`)
})()
Run the code
Save the file with a name like momo_example.js to avoid conflict with the Library, don't save it with mobilemoney.js