By: Samuel Umoren
Rapyd is a fintech platform that enables you to accept, send, and store funds globally. With Rapyd, you can seamlessly integrate local commerce and fintech services into your applications, creating functional and tailored local experiences anywhere in the world. One of Rapyd's essential offerings is the Disburse platform.
Rapyd Disburse lets you pay contractors, workers, suppliers, or any business. In this guide, you'll learn how to use the Rapyd Disburse API to build a gig payment app for freelancers. The app will allow freelancers to add their profile and bank details and request payments on demand or periodically.
Preparing the Project
This article focuses mainly on integration rather than building the entire app from scratch. The only prerequisite for the integration is having a Rapyd Client Portal account. The complete source code can be found on GitHub.
To get started, you have to clone the project on your browser and click
Create a wallet and fill in the form with whatever details you like. When you've done that, you should have a wallet like this. Copy the wallet ID.
and click the Transfer funds button.
In the Transfer funds modal, select the User wallet tab and paste your wallet ID. Insert an amount and select USD for the currency. Click Transfer to transfer the funds to the user wallet view of the client dashboard.
Accessing the Rapyd API Keys
In the root of your project, create a file named .env to house your environment variables. Populate this file with your Rapyd API credentials.
To obtain the accessKey and secretKey from Rapyd, open your Rapyd Client Portal and navigate to
Paste the keys into your .env file:
RAPYD_ACCESS_KEY=
RAPYD_SECRET_KEY=
PORT=5000
BASERAPYDAPIURL=https://sandboxapi.rapyd.net
Keep this file secure and excluded from version control by adding it to your .gitignore file. You're now ready to integrate the app with Rapyd.
Constructing the API Requests Body
Before making requests to the Rapyd Disburse API endpoints, it's crucial to accurately prepare the header parameters and request signatures. This ensures secure and authorized interactions with the Rapyd API. Use the following code snippet to prepare the header parameters and request signatures for secure and authorized interactions with the Rapyd API:
private _accessKey: string;
private _secretKey: string;
private _baseUrl: string;
private _axiosClient: AxiosInstance;
constructor() {
this._accessKey = config.accessKey;
this._secretKey = config.secretKey;
this._baseUrl = config.baseRapydApiUrl;
this._axiosClient = axios.create({
baseURL: this._baseUrl,
});
this._axiosClient.interceptors.request.use(req => {
const method = req.method as HttpMethod;
const salt = this.generateRandomString(8);
const timestamp = Math.floor(Date.now() / 1000);
const signature = this.generateSignature(method, req.url, salt, timestamp, req.data);
req.headers.salt = salt;
req.headers.timestamp = timestamp;
req.headers.access_key = this._accessKey;
req.headers.signature = signature;
req.headers['Content-Type'] = 'application/json';
return req;
});
}
Implementing the Beneficiaries API Endpoint
The beneficiaries endpoint allows you to create a beneficiary profile, which is essential for payouts in the gig payment app.
Making a Request to Create a Beneficiary
You can simplify the process of creating a beneficiary or payee by sending a POST request to the Rapyd Disburse beneficiary API endpoint. Implement the following code snippet to create a new beneficiary:
public async createBeneficiary(body: Beneficiary): Promise<any> {
// Implement the API call to create a beneficiary
try {
const response = await this._axiosClient.post<RapydResponse<Beneficiary>>('/v1/payouts/beneficiary', body);
return response.data
} catch (error) {
if (error.isAxiosError) {
throw new HttpException(+error.response.status, error.response.data?.status || error.response.data);
}
}
}
The createBeneficiary function creates a new beneficiary by making a POST request to the Rapyd API. It takes a Beneficiary object as its argument and returns the API response.
Making a Request to Retrieve Beneficiaries
You now need to fetch beneficiary information by sending a GET request to the Rapyd API:
public async getBeneficiaries(beneficiaryId: string): Promise<Beneficiary[]> {
try {
const response = await this._axiosClient.get<RapydResponse<Beneficiary[]>>(`/v1/payouts/beneficiary/${beneficiaryId}`);
return response.data?.data;
} catch (error) {
if (error.isAxiosError) {
// Handle the error based on your application's requirements
throw new HttpException(+error.response.status, error.response.data?.status || error.response.data);
}
throw error;
}
}
The getBeneficiaries method fetches a specific beneficiary's details from the Rapyd API. It takes a beneficiaryId, makes a GET request, and returns the beneficiary data. If the request fails, it throws an HttpException with the error details.
The UI for this feature lists the bank account information of beneficiaries, including their category, country, and entity type.
Then, based on the freelancer's bank account details, the required form fields are generated.
Implementing Recurring Payouts
In gig economy platforms, recurring payouts are common. To implement this functionality, you'll utilize the node-cron package to schedule tasks. Install the package:
npm install node-cron
Integrate the following code snippet into your project to implement the functionality for recurring payouts:
constructor() {
this.initializeCronJob();
}
private initializeCronJob() {
cron.schedule('0 0 * * *', () => {
// Loop through inMemoryPayouts to find payouts that need to be processed
for (const payout of inMemoryPayouts) {
if (payout.nextPayoutDate && payout.recurrenceFrequency) {
const now = new Date();
if (now >= new Date(payout.nextPayoutDate)) {
// Create a new payout based on the existing one
const newPayout = { ...payout };
// Update nextPayoutDate based on recurrenceFrequency
const nextDate = new Date(payout.nextPayoutDate);
if (payout.recurrenceFrequency === 'weekly') {
nextDate.setDate(nextDate.getDate() + 7);
} else if (payout.recurrenceFrequency === 'monthly') {
nextDate.setMonth(nextDate.getMonth() + 1);
}
newPayout.nextPayoutDate = nextDate;
// For demo purposes, you can log the payout
console.log(`Creating a new recurring payout for ${newPayout.beneficiary_country}`);
// Add the new payout to inMemoryPayouts
inMemoryPayouts.push(newPayout);
}
}
}
});
}
The initializeCronJob method schedules a task to run daily at midnight, scanning through inMemoryPayouts to find payouts due for processing. If a payout is due, a new payout object is created based on the existing one, updating the nextPayoutDate according to the recurrenceFrequency. This new payout is then logged to the console and added to inMemoryPayouts.
Exploring the API Documentation
You can explore the comprehensive API documentation available for the backend to enhance the app with additional features. Access it by navigating to http://localhost:5000/api/docs/ once your server is running. The documentation, generated using Swagger UI and OpenAPI, provides an interactive interface for understanding and testing the available endpoints.
, try out the Rapyd API, and see what you come up with. Share what you build or any takeaways in the developer community. Questions or feedback? Just reply below.
SOCIAL SHARE CARD GENERATOR