Generating product SKUs with Claude
Here’s a nice little thing I did with Claude recently. I have a few hundred books in my Shopify store, all different. Entering each book, I took a picture, transcribed the title, added it to a collection, pressed save —
Then, I discovered that I wanted SKUs, too —
As a task, this is sort of the worst mix of repetitive and fiddly, so, I wrote the following code, which, I am pleased to report, did actually take less time than the repetitive/fiddly task would have —
require "anthropic"
ANTHROPIC = Anthropic::Client.new(
api_key: CLAUDE_API_KEY
)
def generate_sku(title_and_author)
prompt = <<~PROMPT
I'm going to give you the title and author of a book. I need you to generate a SKU code for this book, following these rules:
* If the title is very short, the SKU should be TITLE-AUTHORLASTNAME
* If the title is medium-length or long, the SKU should use a key word from the title, and it should be WORD-AUTHORLASTNAME
* If the book has multiple authors, use the last name of the first listed author
Please reply with ONLY the SKU, without any additional text or commentary.
Here is the title and author:
#{title_and_author}
PROMPT
response = ANTHROPIC.messages.create(
max_tokens: 64,
messages: [{role: "user", content: prompt}],
model: "claude-sonnet-4-20250514",
temperature: 0.7
)
return response.content[0][:text]
end
items = get_items_from_shopify # not included in this code listing
items.each do |item|
sku = generate_sku(item[:title_and_author])
apply_sku(item, sku) # not included in this code listing
end
Pretty nice!
To the blog home page