Skip to content

Instantly share code, notes, and snippets.

@jellevdvliet

jellevdvliet/apiWalmart.js

Last active May 23, 2021
Embed
What would you like to do?
import NodeRSA from "node-rsa";
const keyData = {
consumerId: "CONSUMER_ID",
privateKey: `-----BEGIN RSA PRIVATE KEY-----
MY PRIVATE KEY
-----END RSA PRIVATE KEY-----`,
keyVer: 1,
impactId: "YOUR IMPACT AFFILIATE ID" // not required
},
const generateWalmartHeaders = () => {
const { privateKey, consumerId, keyVer } = keyData;
const hashList = {
"WM_CONSUMER.ID": consumerId,
"WM_CONSUMER.INTIMESTAMP": Date.now().toString(),
"WM_SEC.KEY_VERSION": keyVer,
};
const sortedHashString = `${hashList["WM_CONSUMER.ID"]}\n${hashList["WM_CONSUMER.INTIMESTAMP"]}\n${hashList["WM_SEC.KEY_VERSION"]}\n`;
const signer = new NodeRSA(privateKey, "pkcs1");
const signature = signer.sign(sortedHashString);
const signature_enc = signature.toString("base64");
return {
"WM_SEC.AUTH_SIGNATURE": signature_enc,
"WM_CONSUMER.INTIMESTAMP": hashList["WM_CONSUMER.INTIMESTAMP"],
"WM_CONSUMER.ID": hashList["WM_CONSUMER.ID"],
"WM_SEC.KEY_VERSION": hashList["WM_SEC.KEY_VERSION"],
};
};
export const getProductById = async (productId) => {
const options = {
method: "GET",
headers: generateWalmartHeaders(),
};
const res = await fetch(
`https://developer.api.walmart.com/api-proxy/service/affil/product/v2/items/${productId}?publisherId=${keyData.impactId}`,
options
);
return await res.json()
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment