Automatically Generate Shopify Category Descriptions for SEO Using ChatGPT

Joey Hoer

Joey Hoer

Many SEO specialists recommend adding descriptions to eCommerce categories, because category descriptions can help search engines understand the context and content of a page. Additionally, category descriptions improve the user experience, because category descriptions help users understand what type of products they can expect to find in that category, which helps users quickly find what they are looking for and make informed purchasing decisions, leading to a better overall experience on the site. Well-written and optimized category descriptions can improve search engine visibility and rankings, increase click-through rates from search results, and provide valuable information to potential customers. This can lead to increased traffic, engagement, and conversions for an eCommerce website.

However, if your site has dozens or hundreds of categories, writing descriptions for all of your categories can be time consuming and expensive, so it’s not unusual to see eCommerce websites that lack category descriptions.

Now, with new copy writing capabilities powered by artificial intelligence like OpenAI and ChatGPT, it’s possible to generate content at scale that requires minimal human interaction.

Today, we’re going to explore how it is possible to leverage the REST APIs provided by Shopify and OpenAI to automatically generate category descriptions for Shopify using OpenAI GPT-3 models. This allows you to use the power of AI to improve you website’s content and ranking in search engines.

Video Tutorial On How To Generate Shopify Category Descriptions for SEO Using ChatGPT

Workflow

The basic workflow of this script is to:

  1. Get a list of all collections from Shopify
  2. Then iterate over each collection’s title
    1. Asking OpenAI to generate a description for each category based on it’s title
    2. Then updating each collection’s description in Shopify with the description provided by OpenAI

However, before we can do that, we must first generate credentials for Shopify’s REST API and generate credentials OpenAI’ REST API, so that we can authenticate our API requests.

Note: Ensure that your Shopify API credentials are permitted to use the read_product_listings and write_product_listings Admin API access scopes.

Once we have generated our API keys, we can begin writing our script. Let’s start by using theCustomCollection GET endpoint to retrieve a list of all custom collections from Shopify’s Admin API.

# Get a list of all custom collections from Shopify's API,
# and reformat the JSON response to a simplified CSV format
curl -sX GET "https://${SHOPIFY_STORE_NAME}.myshopify.com/admin/api/2023-01/custom_collections.json" \
  -H 'Content-Type: application/json' \
  -H "X-Shopify-Access-Token: $SHOPIFY_ACCESS_TOKEN" \
  | jq -r '.custom_collections[] | [ .id, .title ] | @csv'

Note: The command above returns a JSON formatted response, but not all of the information returned by Shopify’s API is relevant to our use case. We use jq, a command-line JSON processor, to reformat the JSON into a simplified CSV format, which allows us to work with the data on the command-line more easily.

Now we can iterate over each row of the output and generate a request to OpenAI for each category.

# Loop through all lines in the collections CSV
{
  while IFS=$',' read collection_id collection_title; do
    # Generate a prompt for each collection, using the collection title
    openai_prompt="Our company is an ecommerce brand. Write a description for our company's ${collection_title} category."
  done
} < <(echo "$collections_csv")

OpenAI’s API accepts a few parameters which allow us to control how the response we receive. In our case, there are four parameters we want to include: model, prompt, temperature, and max_tokens. These parameters control the following behavior:

  • The model parameter defines which AI model we want OpenAI to use when generating the response.
  • The prompt parameter (as we’ve seen above) defines the prompt to which we want OpenAI to respond.
  • The temperature parameter defines amount of “risks” allow OpenAI to take. A higher value will produce a more “creative” response, while a lower value will produce a more “well-defined” response.
  • The max_tokens parameter defines maximum length of the output. OpenAI’s Tokenizer page says “one token generally corresponds to ~4 characters of text for common English text. This translates to roughly ¾ of a word (so 100 tokens ≈ 75 words).”

Note: You can find more information about the parameters which the completions endpoint accepts in OpenAI’s API documentation.

While there is no defined SEO rule on the ideal length (word count) of category descriptions. Google’s John Mueller has said if your “product names are a little bit hard to understand, and then it might make sense to add some additional text to give us some context there. But it’s usually additional context in the size of … two or three sentences”. Mueller reiterated a “little bit of content [on category pages] is always useful.”

We will use a value of 300 for the max_tokens parameter to limit the response to approximately 225 words.

# Construct the request data for OpenAI's API
request_data=$(cat <<JSON
{
  "model": "text-davinci-003",
  "prompt": "${openai_prompt}",
  "temperature": 0,
  "max_tokens": 300
}
JSON
)
# Generate text using OpenAI's API (using a GPT-3 model)
# and extract the text from the JSON response
curl -sX POST 'https://api.openai.com/v1/completions' \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d "${request_data}" \
  | | jq '.choices[].text'

Note: The command above returns a JSON formatted response, but not all of the information returned by OpenAI’s API is relevant to our use case. We use jq to extract only the text from the JSON response.

We have successfully generate a category description using OpenAI’s API! All we have left to do is to update the collection’s description in Shopify.

Shopify requires us to

# Construct the request data for Shopify's API
request_data=$(cat <<JSON
{
  "custom_collection": {
    "id": ${collection_id},
    "body_html": "${collection_body_html}"
  }
}
JSON
)
# Update a collection (with a specified ID) using Shopify's API
curl -sX PUT "https://${SHOPIFY_STORE_NAME}.myshopify.com/admin/api/2022-10/custom_collections/${collection_id}.json" \
  -H 'Content-Type: application/json' \
  -H "X-Shopify-Access-Token: $SHOPIFY_ACCESS_TOKEN" \
  -d "${request_data}"

And that’s it! When you put all of these pieces together, and run the script, the collection descriptions for every category on your Shopify store will be updated automatically with content written by AI!

It’s probably a good idea to manually review each of your category descriptions, just to make sure that you are satisfied with the content that OpenAI has generated. Fortunately, reading the descriptions and making minor modifications should be much easier, and less time-consuming than writing all of the descriptions yourself!

The approach could easily be modified to print the descriptions to a CSV file that could be manually reviewed, and imported later using a tool like Matrixify, and it could be easily updated to work with any other eCommerce platform that has an API—such as BigCommerce or Adobe Commerce.

If you have any questions about this article, or would like to speak with someone about how to leverage AI to simplify workflows on your ecommerce website contact Trellis—we’re looking forward to meeting you!

More Reading

Leave a Comment

Share this post

Related Posts

See all posts