NodeJS library for Mastercard API compliant payload encryption/decryption.
Compatibility
NodeJS 17+
There shouldn't be any Node compatibility issues with this package, but it's a good idea to keep your Node versions up-to-date. It is recommended that you use one of the LTS Node.js releases, or one of the more general recent releases. A Node version manager such as nvm (Mac and Linux) or nvm-windows is a good way to stay on top of this.
const fle = new clientEncryption.FieldLevelEncryption(config);
// …let responsePayload = fle.decrypt(encryptedResponsePayload);
Configuring the Field Level Encryption
FieldLevelEncryption needs a config object to instruct how to decrypt/decrypt the payloads. Example:
const config = {
paths: [
{
path: "/resource",
toEncrypt: [
{
/* path to element to be encrypted in request json body */element: "path.to.foo",
/* path to object where to store encryption fields in request json body */obj: "path.to.encryptedFoo",
},
],
toDecrypt: [
{
/* path to element where to store decrypted fields in response object */element: "path.to.encryptedFoo",
/* path to object with encryption fields */obj: "path.to.foo",
},
],
},
],
ivFieldName: "iv",
encryptedKeyFieldName: "encryptedKey",
encryptedValueFieldName: "encryptedData",
dataEncoding: "hex",
encryptionCertificate: "./path/to/public.cert",
privateKey: "./path/to/your/private.key",
oaepPaddingDigestAlgorithm: "SHA-256",
};
Performing Encryption
Call FieldLevelEncryption.encrypt() with a JSON request payload, and optional header object.
This library uses JWE compact serialization for the encryption of sensitive data.
The core methods responsible for payload encryption and decryption are encryptData and decryptData in the JweEncryption class.
const jwe = new clientEncryption.JweEncryption(config);
// …let responsePayload = jwe.decrypt(encryptedResponsePayload);
• Configuring the JWE Encryption
JweEncryption needs a config object to instruct how to decrypt/decrypt the payloads. Example:
const config = {
paths: [
{
path: "/resource1",
toEncrypt: [
{
/* path to element to be encrypted in request json body */element: "path.to.foo",
/* path to object where to store encryption fields in request json body */obj: "path.to.encryptedFoo",
},
],
toDecrypt: [
{
/* path to element where to store decrypted fields in response object */element: "path.to.encryptedFoo",
/* path to object with encryption fields */obj: "path.to.foo",
},
],
},
],
mode: "JWE",
encryptedValueFieldName: "encryptedData",
encryptionCertificate: "./path/to/public.cert",
privateKey: "./path/to/your/private.key",
};
Mode must be set to JWE to use JWE encryption
• Performing JWE Encryption
Call JweEncryption.encrypt() with a JSON request payload, and optional header object.
{"path":{"to":{"foo":{"sensitive":"this is a secret","sensitive2":"this is a super secret!"}}}}
• Encrypting Entire Payloads
Entire payloads can be encrypted using the "$" operator as encryption path:
const config = {
paths: [
{
path: "/resource1",
toEncrypt: [
{
/* path to element to be encrypted in request json body */element: "$",
/* path to object where to store encryption fields in request json body */obj: "$",
},
],
toDecrypt: [],
},
],
mode: "JWE",
encryptedValueFieldName: "encryptedData",
encryptionCertificate: "./path/to/public.cert",
privateKey: "./path/to/your/private.key",
};
Example:
const payload = JSON.parse(
"{" +
' "sensitive": "this is a secret",' +
' "sensitive2": "this is a super secret!"' +
"}");
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
// …let responsePayload = jwe.encrypt("/resource1", header, payload);
Entire payloads can be decrypted using the "$" operator as decryption path:
const config = {
paths: [
{
path: "/resource1",
toEncrypt: [],
toDecrypt: [
{
/* path to element where to store decrypted fields in response object */element: "$",
/* path to object with encryption fields */obj: "$",
},
],
},
],
mode: "JWE",
encryptedValueFieldName: "encryptedData",
encryptionCertificate: "./path/to/public.cert",
privateKey: "./path/to/your/private.key",
};
Example:
const encryptedPayload = JSON.parse(
"{" +
' "encryptedData": "eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw"' +
"}");
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
let responsePayload = jwe.decrypt(encryptedPayload);
Output:
{"sensitive":"this is a secret","sensitive2":"this is a super secret!"}
• First Level Field Encryption and Decryption
To have encrypted results in the first level field or to decrypt the first level field, specify encryptedValueFieldName to be the same as obj (for encryption) or element (for decryption):
Example of configuration:
const config = {
paths: [
{
path: "/resource1",
toEncrypt: [
{
/* path to element to be encrypted in request json body */element: "sensitive",
/* path to object where to store encryption fields in request json body */obj: "encryptedData",
},
],
toDecrypt: [
{
/* path to element where to store decrypted fields in response object */element: "encryptedData",
/* path to object with encryption fields */obj: "sensitive",
},
],
},
],
mode: "JWE",
encryptedValueFieldName: "encryptedData",
encryptionCertificate: "./path/to/public.cert",
privateKey: "./path/to/your/private.key",
};
Example of encryption:
const payload = {
sensitive: "this is a secret!",
notSensitive: "not a secret",
};
const jwe = new (require("mastercard-client-encryption").JweEncryption)(config);
// …let responsePayload = jwe.encrypt("/resource1", header, payload);
Output:
{"encryptedData":"eyJraWQiOiI3NjFiMDAzYzFlYWRlM….Y+oPYKZEMTKyYcSIVEgtQw","notSensitive":"not a secret"}
{"sensitive":"this is a secret","notSensitive":"not a secret"}
Integrating with OpenAPI Generator API Client Libraries
OpenAPI Generator generates API client libraries from OpenAPI Specs.
It provides generators and library templates for supporting multiple languages and frameworks.
The client-encryption-nodejs library provides the Service decorator object you can use with the OpenAPI generated client. This class will take care of encrypting request and decrypting response payloads, but also of updating HTTP headers when needed, automatically, without manually calling encrypt()/decrypt() functions for each API request or response.
OpenAPI Generator
OpenAPI client can be generated, starting from your OpenAPI Spec / Swagger using the following command:
openapi-generator-cli generate -i openapi-spec.yaml -l javascript -o out
Client library will be generated in the out folder.
Import the OpenAPI Client using the Service decorator object:
const openAPIClient = require("./path/to/generated/openapi/client");
const config = {
/* service configuration object */
};
const service = new mcapi.Service(openAPIClient, config);
Use the service object as you are using the openAPIClient to make API requests.
Example:
let api = service.ServiceApi();
let merchant =
/* … */
api.createMerchants(merchant, (error, data, response) => {
// requests and responses will be automatically encrypted and decrypted// accordingly with the configuration used to instantiate the mcapi.Service./* use response/data object here */
});
FAQs
Library for Mastercard API compliant payload encryption/decryption.
The npm package mastercard-client-encryption receives a total of 1,736 weekly downloads. As such, mastercard-client-encryption popularity was classified as popular.
We found that mastercard-client-encryption demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Package last updated on 04 Dec 2025
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Socket Firewall Free is now bundled into Docker Hardened Images, adding build-time and dependency-install supply chain protection on top of hardened base images for Node.js, Python, and Rust.
Impostor NuGet package Tracer.Fody.NLog typosquats Tracer.Fody and its author, using homoglyph tricks, and exfiltrates Stratis wallet JSON/passwords to a Russian IP address.