This CLI generates gift card codes from a CSV containing recipient email addresses. Those generated codes are then mass imported into Shopify using the Shopify Admin API.
In Shopify, it's not possible to mass create gift cards and see their codes prior to activation.
Sometimes, it's useful to get a list of the generated codes such that you can prepare custom emails from your marketing software, prior to activating the codes in Shopify.
This script allows you to import a list of customers, each with a desired gift card amount. For those customers, you can then generate a list of codes which is output into a separate file. You can use this file however you like, for example to import into your email tool and generate emails.
Then, when you're ready to actually create the gift cards in Shopify, run the import command which will take the generated codes and create them in Shopify. Optionally, you may suppress the generic gift card email which Shopify usually sends to the customer.
- Install dependencies
- Prepare customer file
- Validate customer file
- Generate gift cards
- Import gift cards
After cloning this repo, install dependencies using npm.
npm install
You must prepare a CSV containing the customers and amounts for the generated gift cards. The customer emails are used to lookup the customer record in Shopify. If the customer does not exist, then it's created using the name attributes.
Attribute | Description |
---|---|
first_name | First name for customer. |
last_name | Last name for customer. |
Email address for customer. Must be unique within the CSV. | |
amount | Money amount for the gift card. |
To enable detection of duplicates, this tool uses the Sync API for CSV parsing, not the streaming API. This means that your import CSV must be small enough to fit into memory. If your CSV is too big, consider breaking it up into smaller files.
It's advised to validate that your customer CSV file is ready to be imported. This command will check that the attribute names are correct, and that there are no duplicates. It will output a summary on completion.
npm start -- validate-customer-file ./path/to/customers.csv
This command will take your customer CSV file, generate a unique gift card code for each one, and generate a new CSV file which is equal to your customer CSV file, but with additional code
, expires
and note
attributes.
You may optionally provide a --prefix
option, which will prefix the generated code with a static string. The tool will automatically remove ambiguous characters (like 1
and I
) from the code. You should keep this as a short as possible, as the code can only be 20 characters long. The longer your prefix, the more likely you will generate a conflicting code.
You may optionally provide a --expires
attribute, which must be a date in ISO 8601 format. If you leave this blank, the expires
column in the resulting CSV will be empty.
You may optionally provide a --note
attribute, which will set a private note on the gift card in Shopify. It's recommended that you use this to explain why the gift card was created.
npm start -- generate-gift-cards \
--input ./path/to/customers.csv \
--output ./path/to/gift-cards.csv \
--prefix "SUMMER" \
--expires "2023-01-01" \
--note "Summer VIP giveaway"
You may use the resulting file for creating personalized emails for your gift card recipients, and other marketing activity which would not be possible unless you knew the gift card codes.
When you are ready to import the codes into Shopify, run the import command using the CSV file generated in the previous step.
By default, Shopify will send a generic "You have a gift card" email to the customer when the gift card is created. If you would like to suppress this email (because you plan to send the codes in another way), use the --suppress-email
option.
This works by creating the gift card without a customer first, and then attaching it to the customer in a second API request. When created like this, Shopify does not notify the customer.
You need to provide a Shopify access token with the write_customers
and write_gift_cards
scopts for the --token
attribute. See the Obtaining a Shopify access token section if you're not sure how to do that.
The command will output a results file in CSV format, which is the same as your input gift cards file, but with additional customer_id
, gift_card_id
and error
columns. You should review this file after the command completes to check that all the gift cards were created successfully.
npm start -- import-gift-cards \
--input ./path/to/gift-cards.csv \
--output ./path/to/results.csv \
--token ACCESS_TOKEN \
--shopify-domain foo.myshopify.com \
--suppress-email
Shopify regularly update this process, so here's a quick guide for creating a token.
- Login to your Shopify admin
- Click "Settings" in the bottom left
- Click "Apps and sales channels" in the left column
- Click "Develop apps" in the top right
- Click "Create an app"
- Enter something description for the app name, like "Shopify Gift Card Generator"
- Select yourself as the app developer
- Click the "Configure Admin API scopes" button
- Enable
write_customers
,read_customers
,write_gift_cards
andread_gift_cards
- Click the "Save" button in the top right
- Click the "Install app" button in the top right
- Click the "Install" button in the popup
- On the "API credentials" tab, click "Reveal token once"
- Copy the token, and store it somewhere safe (it should begin with
shpat_
)
When you are done with this tool, you should delete your access token.
- Click the "Uninstall app" button in the top right
- Click the "Uninstall" button in the popup
- Click on the "App settings" tab
- Click the "Delete app" button at the bottom
- Click the "Delete" button in the popup