How to Create Shopify Product Using GraphQL API in Node.js API

Step-by-Step Guide

  1. Set Up Your Environment:
    • Install the required packages:

Create Your .env File:

  • Create a .env file in the root of your project and add your Shopify store details:
  1. Create Your Script:
    • Create a file, e.g., createProduct.js, and add the following code:

Explanation

  • Environment Variables: dotenv is used to load environment variables from the .env file.
  • Shopify API Node: This package simplifies the interaction with Shopify’s API.
  • GraphQL Mutation: The productCreate mutation creates a new product with specified attributes like title, bodyHtml, vendor, tags, and variants.
  • Error Handling: The try-catch block captures any errors during the API call and logs them.

Run the Script

To run the script, execute the following command in your terminal:

This will create a new product in your Shopify store with the details specified in the mutation.

By following these steps, you can successfully create products in Shopify using the GraphQL API with Node.js. If you need further customization or additional features, you can modify the mutation accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *