Node.js
Introduction
The Mangopay Node.js SDK makes working with the Mangopay API easier in a Node.js environment. This SDK is open-source and available on GitHub.
Mangopay Node.js SDK →Prerequisites
To run the Mangopay Node.js SDK, you’ll need:
- A
ClientId
and an API key – if you don’t have these, contact Sales to get access to the Mangopay Hub - Node.js installed (v.14 and higher)
- npm package manager installed
Note - Mangopay SDK compatibility
The Node.js SDK is only compatible with the v2.01 version of the Mangopay API.
Getting started
- Install the SDK
npm install mangopay2-nodejs-sdk --save
- Initialize the SDK
const mangopay = require('mangopay2-nodejs-sdk');
const paymentApi = new mangopay({
clientId: 'your-mangopay-client-id',
clientApiKey: 'your-mangopay-api-key',
baseUrl: 'https://api.sandbox.mangopay.com'
});
The configuration object of the SDK supports all the following properties:
Key | Type | Default value | Description |
---|---|---|---|
clientId | string | null | Your Mangopay ClientId – can be found in the Hub. |
clientApiKey | string | null | Your Mangopay API key – can be found in the Hub. |
baseUrl | string | http://api.sandbox.mangopay.com/ | The API sandbox URL. Set to the sandbox environment by default. To enable production environment, set it to http://api.mangopay.com/ |
debugMode | boolean | false | Activates the debug mode. Recommended only in Sandbox. |
logClass | function() {console.log(arguments)} | function() {console.log(arguments)} | |
connectionTimeout | integer | 30000 | Set the time to wait (in milliseconds) while trying to establish a connection before terminating the attempt and generating an error. |
responseTimeout | integer | 80000 | Set response timeout limit (in milliseconds). |
apiVersion | string | v2.01 | The API version. |
errorHandler | null | function(options, err) {console.error(options, err)} | Set a custom error handler. |
ukHeaderFlag | boolean | false | Platforms that have contracted with Mangopay’s UK entity (MANGOPAY U.K. LIMITED) must include the following header in all requests. If you’re using an SDK, you need to set it to true . |
SDK usage
All endpoints are documented with the related Node.js SDK method throughout the Mangopay documentation. The code examples provided should be adjusted for your usage.
TypeScript
The SDK is compatible with TypeScript and provides a set of predefined models. They are available in the /lib/models/ GitHub folder.
Using callbacks instead of promises
Across the Mangopay endpoint documentation, the usage of the SDK is documented with promises. If you prefer callbacks, here is an example on how to use them:
const mangopayInstance = require('mangopay2-nodejs-sdk')
const mangopay = new mangopayInstance({
clientId: 'your-client-id',
clientApiKey: 'your-api-key',
})
let user = {
Id: '171602348',
}
mangopay.Users.get(user.Id, function(data, response, err){
console.log(data)
console.log(response)
console.log(err)
})
Pagination and filtering
For endpoints that support pagination and filtering, you can use options.parameters
to specify these options:
const mangopayInstance = require('mangopay2-nodejs-sdk')
const mangopay = new mangopayInstance({
clientId: 'your-client-id',
clientApiKey: 'your-api-key',
})
let parameters = {
// Options for pagination
per_page: 2,
page: 2,
// Options for Filters
BeforeDate: 1683129820,
AfterDate: 1682957020,
// Nature: REGULAR,
// Status: FAILED,
// Type: TRANSFER,
};
const indexEvents = async () => {
return await mangopay.Events.getAll({ parameters: parameters })
.then((response) => {
console.info(response)
return response;
})
.catch((err) => {
console.log(err)
return false;
});
};
indexEvents()
Accessing response headers
For reading the server response headers, you can use resolveWithFullResponse. This is useful to access information about the pagination or rate limiting.
const mangopayInstance = require('mangopay2-nodejs-sdk')
const mangopay = new mangopayInstance({
clientId: 'your-client-id',
clientApiKey: 'your-api-key',
})
const indexEvents = async () => {
return await mangopay.Events.getAll({ resolveWithFullResponse: true })
.then((response) => {
console.info(response.headers['x-number-of-pages'])
return response
})
.catch((err) => {
console.log(err)
return false
});
};
indexEvents()
Report an issue
Found a problem with the SDK? Create an issue on GitHub:
Report an issue →Was this page helpful?