https://www.liplips.co.uk/ is my sister’s small business. She sells handmade jewelry products online. Social media particularly Instagram is one of her main channels for reaching to customers. She creates videos for different collections and shares them with her followers and minor influencers.
When we were talking her process I saw how much time she spent on creating videos and most of it was repetitive tasks. Here’s one her reels.
In this blog post I summarize how we were able to automate creation of these types of videos by connecting 3 APIs.
- Shopify API to get a list of collections and product information
- ChatGPT for creating text content for the videos
- Creatomate APi for creating the videos
Step 1: Get a list of collections and product information
For marketing the products Liplips Studio uses collections that are groups of similar items.
Using the following code we can get collection and product info from Shopify API
def get_shopify_data(element, query_tuple=None): access_token = userdata.get('liplips_shopify_token') shop_url = 'your_shop_name.myshopify.com' # Set up the headers with the necessary authentication headers = { "Content-Type": "application/json", "X-Shopify-Access-Token": access_token } params = {'limit': 250} # Define the endpoint for fetching products url = get_api_url(element, query_tuple) # Make the HTTP GET request to the Shopify API response = requests.get(url, headers=headers, params=params) # Check if the request was successful if response.status_code == 200: # Load the JSON data from the response products = response.json() else: print(f"Failed to retrieve products: {response.status_code}") return products def get_collection_dict(): element = 'custom_collections' collections = get_shopify_data(element)[element] print(f'{len(collections)} collections') collection_dict = {} for collection_data in collections: collection = Collection(collection_data) collection_dict[collection.id] = collection return collection_dict def get_product_dict(): element = 'products' products = get_shopify_data(element)[element] print(f'{len(products)} products') products_dict = {} for product_data in products: product = Product(product_data) products_dict[product.id] = product return products_dict products_dict = get_product_dict() collection_dict = get_collection_dict()
With these calls we can now get the list of products in each collection and descriptions and image asset URL’s for each of the product.
- Image assets will be used in the videos
- We’ll use the descriptions to generate text content that we can include in the videos
Step 2: Use ChatGPT API to get text content for the video
We want the video to include a one sentence description of the product.
We used following code for generating one sentence description from products descritpion that we obtained using the Shopify API.
def check_run(client, thread_id, run_id): while True: # Refresh the run object to get the latest status run = client.beta.threads.runs.retrieve( thread_id=thread_id, run_id=run_id ) if run.status == "completed": print(f"Run is completed.") break elif run.status == "expired": print(f"Run is expired.") break else: print(f"OpenAI: Run is not yet completed. Waiting...{run.status}") time.sleep(10) # Wait for 1 second before checking again def add_user_input(thread, user_input, print_repsonse=True): message = client.beta.threads.messages.create(thread_id=thread.id, role="user", content=user_input ) run = client.beta.threads.runs.create( thread_id=thread.id, assistant_id=assistant.id, ) check_run(client, thread.id, run.id) messages = client.beta.threads.messages.list( thread_id=thread.id ) assistant_message = messages.data[0].content[0].text.value if print_repsonse: print(len(messages.data)) print(assistant_message) return messages def last_chatgpt_reponse(messages): return messages.data[0].content[0].text.value client = openai.Client(api_key=userdata.get('openai_api_key')) assistant = client.beta.assistants.create( name="Shopify helper", instructions="""You are a shopify site content generator for products and product collections. You generate product titles and descriptions using the details provived by the users. you follow instructions and feedback from the users to generate the best titles and descriptions for maximizing product sales. """, tools=[{"type": "code_interpreter"}], model="gpt-4o" ) def get_collection_intro(collection): style_thread = client.beta.threads.create() prompt = f'give a 12-15 words summary for {collection.body_html}' messages = add_user_input(style_thread, prompt, False) return last_chatgpt_reponse(messages)
Here we used ChatGPT Assistants API with the following prompt
You are a shopify site content generator for products and product collections. You generate product titles and descriptions using the details provived by the users. You follow instructions and feedback from the users to generate the best titles and descriptions for maximizing product sales.
and we used the following prompt to get a short sentence we can include in our video. We included the description of the product (stored in body_html variable) that we pulled from Shopify.
give a 12-15 words summary for {collection.body_html}
This made chatGPT generate us callout sentence for the Mum Jewellery collection
Explore the Mum Jewellery collection with elegant necklaces and jewellery, perfect for showing love.
and this callout sentence for Flower Jewellery collection
Discover the elegance of our Flower Jewellery collection, featuring exquisite pieces like the flower necklace and the stunning lotus flower necklace.
These will passed to video generation API to be included in the final slide of the video.
We also get the image url’s from first 3 products in the collection in the collection using the code
def get_product_image_url(self, product_index): products = self.products_list() return products[product_index].shopify_data['images'][0]['src']
Product 1: Flower-of-life-apatite-necklace
Product 2 Landscape Silver Necklace
Product 3: snowdrop-flower-silver-rose-gold-plated-necklace
These images will be passed to the video API to be used in the video.
Step 3: Creatomate API for creating the videos
This step will collect it all.
https://creatomate.com/ is a website that provides API’s to create videos from templates you create on their site. It is a paid service but well worth the money.
Here’s the template we have created for this task. This video will show a collection name, 3 product photos and a callout action at the end.
We’ll pass all of it to the API with the following code
def create_video(collection, collection_intro): url = "https://api.creatomate.com/v1/renders" headers = { "Authorization": userdata.get('creatify_bearer_token'), "Content-Type": "application/json" } data_payload = { "template_id": "5c847abb-2055-4e43-ae2a-4fc1c04ea2fc", "modifications": { "Image 3": collection.get_product_image_url(0), "Image 2": collection.get_product_image_url(1), "Image 1": collection.get_product_image_url(2), "Call to Action": "Shop Now", "0ecbf5cf-2a08-47d7-bd31-09f5a918d6d7": collection.title, "6260e63c-b340-4e42-9f1e-96a36ea30a3d": collection_intro } } response = requests.post(url, headers=headers, data=json.dumps(data_payload)) return response.json()[0]
This will submit our video request to Creatomate and we’ll get the following videos automatically for different collection. I include several videos to show how effective this automation is. Each video was taking 1-2 hours to prepare without this automation. Now it is under 30 seconds to create each video.
As a summary we have shown how we have automated a reptitive and time consuming task for social marketing for a jewellery site.
If you have any questions or need help with similar tasks contact us at cem@decisionsciencelab.com