Shopify / shopify-app-js

MIT License
284 stars 112 forks source link

Newb question: How to add a product in NodeJS #1015

Closed Philomath88 closed 4 months ago

Philomath88 commented 4 months ago

Before opening this issue, I have:

Upgraded to the latest version of the relevant packages @shopify/admin-api-client package and version: 2.0.0 Node version: 16.13.0 Operating system: macOS 14.2.1 Set { logger: { level: LogSeverity.Debug } } in my configuration, when applicable Found a reliable way to reproduce the problem that indicates it's a problem with the package Looked for similar issues in this repository Checked that this isn't an issue with a Shopify API If it is, please create a post in the Shopify community forums or report it to Shopify Partner Support Expected behavior I expect to be able to add a product to my Shopify store using a Node.js script without encountering errors related to missing required parameters.

Actual behavior When attempting to add a product, I receive an error indicating that required parameters are missing.

Steps to reproduce the problem Initialize the Shopify Admin API client with the correct store details and API version. Define the product data in the script. Attempt to add the product using the restClient.post method. Observe the error indicating missing required parameters. Debug logs javascript Copy code // Relevant debug logs (if any) Error adding product: Required parameter missing Additional details Here is the Node.js script I am using:

// Import the required Shopify Admin API client
const { createAdminRestApiClient, DataType } = require('@shopify/admin-api-client');

// Initialize the REST API client with your store's details
const restClient = createAdminRestApiClient({
  storeDomain: 'xxxx.myshopify.com', // Replace with your store's domain
  apiVersion: '2024-07', // Use the correct API version
  accessToken: 'xxxx'
});

// Function to add a sample product
async function addSampleProduct() {
  try {
    // Define the product data
    const productData = {
      product: {
        title: "Sample Product",
        body_html: "<strong>Awesome</strong>",
        vendor: "John's Apparel",
        product_type: "Shirts",
        tags: ["Barnes & Noble", "John's Apparel", "Big Air"],
        variants: [
          {
            option1: "First",
            price: "10.00"
          }
        ]
      }
    };

    // Make the POST request to add the product
    const response = await restClient.post({
      path: 'products',
      data: productData,
      type: DataType.JSON
    });

    // Log the newly created product response
    console.log(response);
  } catch (error) {
    // Log any errors that occur during the request
    console.error("Error adding product:", error.message);
  }
}

// Call the function to add the sample product
addSampleProduct();

Despite following the documentation and ensuring that all required parameters are included in the productData, I continue to receive errors. Any guidance or examples of a working script to add a product would be greatly appreciated.

Thank you!

Philomath88 commented 4 months ago

In case anyone is wondering...

It worked for me but I had to use the API Node

const Shopify = require('shopify-api-node');

const product = { title: 'Burton Custom Freestyle 151', body_html: 'Good snowboard!', vendor: 'Burton', product_type: 'Snowboard', status: 'draft' };

shopify.product.create(product) .then(response => { console.log('Product created successfully...', response); }) .catch(error => { console.log('Error adding the product', error); });