Home > Software design >  WP REST API can't get the products from wp rest api first time connection with node.js
WP REST API can't get the products from wp rest api first time connection with node.js

Time:08-06

not showing any errors. yes this is code I am running with my url and secret keys.Also getting products with these secret keys in postman but not with nodejs.

Image of code

Code Here

const express = require("express");
const app = express();

const cors = require("cors");
const mysql = require("mysql2");


var WooCommerceAPI = require("woocommerce-api");

var WooCommerce = new WooCommerceAPI({
  url: "http://mysite.local",
  consumerKey: "my_key",
  consumerSecret: "my_secret",
  wpAPI: true,
  version: "wc/v3"
});

WooCommerce.getAsync("products").then(function(result) {
  return JSON.parse(result.toJSON().body);
});

app.listen("3001", () => {
  console.log("Server is running on port 3001");
});

**Please guide me the or share the updated resources thankyou **

CodePudding user response:

please confirm Are you running WordPress on your localhost?

CodePudding user response:

Try this snippet

WooCommerce.get("products")
  .then((response) => {
    console.log(response.data);
  })
  .catch((error) => {
    console.log(error.response.data);
  });

CodePudding user response:

Sorry use these steps then it will work

// Install:
// npm install --save @woocommerce/woocommerce-rest-api

// Setup:
const WooCommerceRestApi = require("@woocommerce/woocommerce-rest-api").default;
// import WooCommerceRestApi from "@woocommerce/woocommerce-rest-api"; // Supports ESM

const WooCommerce = new WooCommerceRestApi({
  url: 'http://example.com', // Your store URL
  consumerKey: 'consumer_key', // Your consumer key
  consumerSecret: 'consumer_secret', // Your consumer secret
  version: 'wc/v3' // WooCommerce WP REST API version
});
  • Related