Skip to content

Instantly share code, notes, and snippets.

@seagalputra
Created June 2, 2023 13:06
Show Gist options
  • Save seagalputra/2916db4809e172c186d53f4da7cbce76 to your computer and use it in GitHub Desktop.
Save seagalputra/2916db4809e172c186d53f4da7cbce76 to your computer and use it in GitHub Desktop.
WooCommerce API with Fetch
import OAuth from "oauth-1.0a";
import crypto from "crypto";
const authParams = {
consumer: {
key: "<consumer-key>",
secret: "<consumer-secret>",
},
signature_method: "HMAC-SHA256",
hash_function: (base: any, key: any) => {
return crypto.createHmac("sha256", key).update(base).digest("base64");
},
};
const oauth = new OAuth(authParams);
const url = "http://localhost:10004/wp-json/wc/v3/products";
const auth = oauth.authorize({
url,
method: "get",
});
const authHeader = oauth.toHeader(auth);
const res = await fetch(url, {
headers: {
...authHeader,
},
});
const products = await res.json();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment