Performing a Withdrawal via the REST API (JavaScript)
This tutorial shows you how to use JavaScript to integrate with MOALA's APIs and perform a money withdrawal.
Prerequisites
- A MOALA account with the required permissions.
- Node.js installed to run the script.
1. Authentication
The API requires three headers:
- LP-ACCESS-KEY: access key
- LP-ACCESS-TIMESTAMP: timestamp
- LP-ACCESS-SIGN: HMAC-SHA256 signature
Generating the Signature
The signature is generated by combining the key, the timestamp, and the payload.
The generateSignature function generates the signature of the request to be sent.
const crypto = require('crypto');
function generateSignature(secretKey, timestamp, method, endpoint, payload) {
const message = timestamp + method + endpoint + payload;
return crypto.createHmac('sha256', secretKey).update(message).digest('hex');
}
2. Perform the Withdrawal
Now we’ll perform a money withdrawal operation using MOALA’s REST APIs.
Withdrawal Operation Endpoint
To make a withdrawal, use the endpoint below with the appropriate serviceCode for the desired operator.
POST https://api.moala.africa/v1/api/transaction/payment
The allowed serviceCode values for this endpoint are:
- CASHOUT_MTN_CM: Cashout MTN Cameroon
- CASHOUT_ORANGE_CM: Cashout Orange Cameroon
- CASHOUT_AIRTEL_GA: Cashout Airtel Gabon
- CASHOUT_MOOV_GA: Cashout Moov Gabon
- PAIEMENTMARCHAND_MTN_CM: Merchant payment MTN Cameroon
- PAIEMENTMARCHAND_ORANGE_CM: Merchant payment Orange Cameroon
- PAIEMENTMARCHAND_AIRTEL_GA: Merchant payment Airtel Gabon
- PAIEMENTMARCHAND_MOOV_GA: Merchant payment Moov Gabon
Request Body
Below are the required fields in the request body:
| Field | Type | Description |
|---|---|---|
transactionType | string | Type of transaction to perform (fixed value: deposit) |
amount | float | Amount to withdraw |
serviceCode | string | The service code to be used (see list of serviceCode values above) |
phoneNumber | string | The phone number from which the withdrawal is made |
partnerId | string | The partner identifier making the withdrawal |
Withdrawal Request
Let’s now build and send a withdrawal request via the API.
const fetch = require('node-fetch');
async function makeWithdrawal(amount, serviceCode, phoneNumber, partnerId, apiKey, secretKey) {
const endpoint = '/v1/api/transaction/payment';
const url = `https://api.moala.africa${endpoint}`;
const timestamp = Math.floor(Date.now() / 1000).toString();
const payload = JSON.stringify({ amount, transactionType: 'withdrawal', serviceCode, phoneNumber, partnerId });
const signature = generateSignature(secretKey, timestamp, 'POST', endpoint, payload);
const response = await fetch(url, {
method: 'POST',
headers: {
'LP-ACCESS-KEY': apiKey,
'LP-ACCESS-TIMESTAMP': timestamp,
'LP-ACCESS-SIGN': signature,
'Content-Type': 'application/json'
},
body: payload
});
const data = await response.json();
console.log(data);
}
The partnerId field must be unique for each transaction, otherwise the request will fail.
3. Check the Status
Once the withdrawal request has been sent, you can check the success of the operation by making another API call.
Status Check Endpoint
The endpoint below allows you to check the status of the previously submitted withdrawal transaction.
GET https://api.moala.africa/v1/api/transaction/check/{partnerId}
Status Check Request
async function checkStatus(partnerId, apiKey, secretKey) {
const endpoint = `/v1/api/transaction/check/${partnerId}`;
const url = `https://api.moala.africa${endpoint}`;
const timestamp = Math.floor(Date.now() / 1000).toString();
const signature = generateSignature(secretKey, timestamp, 'GET', endpoint, '');
const response = await fetch(url, {
method: 'GET',
headers: {
'LP-ACCESS-KEY': apiKey,
'LP-ACCESS-TIMESTAMP': timestamp,
'LP-ACCESS-SIGN': signature
}
});
const data = await response.json();
console.log(data);
}
4. Run the Code
Now we’ll run the code presented above.
We’ll try to withdraw 500F.
const API_KEY = 'votre_api_key';
const SECRET_KEY = 'votre_secret_key';
const serviceCode = 'CASHOUT_ORANGE_CM';
const phoneNumber = 'XXXXXXXXX';
const partnerId = 'xxxxxxxx';
const amount = 1000;
makeWithdrawal(amount, serviceCode, phoneNumber, partnerId, API_KEY, SECRET_KEY).then(() => {
checkStatus(partnerId, API_KEY);
});
5. Conclusion
You now know how to integrate MOALA’s API in JavaScript to perform a money withdrawal and check its status.