A small Python script to generate a single Stripe coupon and multiple unique promotion codes, and save them all to a CSV file.
Stripe coupons and promotion codes are not the same thing:
- A coupon defines the discount you want to offer β e.g., 25% off, $20 off, etc.
- A promotion code is what your customers actually use β it links to the coupon.
So, instead of creating 1,000 separate coupons, the correct approach is:
Create one coupon, and generate 1,000 promo codes attached to it.
pip install stripe python-dotenv
Create a .env
file in the project directory with this line:
STRIPE_API_KEY=sk_live_your_secret_key_here
python generate_coupons.py
- Creates a single coupon (e.g., 25% off)
- Generates 1,000+ unique promotion codes tied to that coupon
- Saves them in a file called
promotion_codes.csv
Example CSV output:
WT7KEHFS
LIIO8NK5
AFMFDSBL
...
Inside generate_coupons.py
, you can edit:
Variable | Description |
---|---|
percent_off |
Percentage discount (e.g. 25) |
expires_at |
Expiry date for each promo code |
TOTAL_CODES |
Number of promo codes to generate |
max_redemptions |
How many times each code can be used |
metadata |
Optional: tag with product_id , etc. |
- Promo codes are saved to
promotion_codes.csv
- The script handles retries and basic rate limiting
- Python 3.12+ recommended
This project includes a .gitignore
that excludes:
.env
__pycache__/
.DS_Store
.vscode/
.idea/
So your secrets and local system files wonβt get committed.
Make sure you have permission to write files in your directory, and that your API key is active and scoped to allow coupon/promo code creation.
Happy coding β and good luck!