This Python script fetches invoice data from the Stripe API and extracts specific information into a CSV file.
- Fetches invoice data from the Stripe API
- Extracts the following fields:
idcreatedamount_paidcurrencyinvoice_pdfnumbersubtotaltaxcountry
- Sorts invoices by creation date in ascending order
- Saves the extracted data to a CSV file
- Python 3.6+
- Stripe account with API keys
-
Clone the repository:
git clone https://github.com/your-username/stripe-invoice-fetcher.git cd stripe-invoice-fetcher -
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Create a
.envfile in the root directory and add your Stripe secret key:STRIPE_SECRET_KEY=your_stripe_secret_key
-
Run the script to fetch invoices and save them to a CSV file:
python fetch_invoices.py
-
The script will print the DataFrame of invoices to the console and save it to
invoices.csv.
The resulting CSV file (invoices.csv) will look like this:
id,created,amount_paid,currency,invoice_pdf,number,subtotal,tax,country
in_1OpYlMBLRKsqtTK3BRUZ50Zs,2024-03-01 17:16:44,10,usd,https://pay.stripe.com/invoice/acct_1OcnxzBLRKsqtTK3/live_YWNjdF8xT2NueHpCTFJLc3F0VEszLF9QZXNXQWJEZ0pBY1JnMUJJenJzM1lrcFhQOFZIUkZJLDk5ODUwNjM10200nRdIx7qS/pdf?s=ap,AC1FA5E2-0002,10,1.74,ES
in_1OpWusBLRKsqtTK3kCq6xa1W,2024-03-01 15:18:26,10,usd,https://pay.stripe.com/invoice/acct_1OcnxzBLRKsqtTK3/live_YWNjdF8xT2NueHpCTFJLc3F0VEszLF9QZXFjaTI4YmI1blhsVDlFMHJaTkQ0U1FBamxpMFdBLDk5ODQ0NTA00200hJC6ETEK/pdf?s=ap,AC1FA5E2-0001,10,1.74,ES
in_1Oqd8NBLRKsqtTK3cpnHOAcZ,2024-03-04 16:08:55,10,usd,https://pay.stripe.com/invoice/acct_1OcnxzBLRKsqtTK3/live_YWNjdF8xT2NueHpCTFJLc3F0VEszLF9QZno2WHFBZHdvaTNOUVRjbXZqVmRVZlNqRHE4NHFvLDEwMDEwNTc2OA0200InB9ZwHv/pdf?s=ap,AC1FA5E2-0003,10,0,US
in_1OqvAkBLRKsqtTK3vgbSxiz2,2024-03-05 11:24:34,10,usd,https://pay.stripe.com/invoice/acct_1OcnxzBLRKsqtTK3/live_YWNjdF8xT2NueHpCTFJLc3F0VEszLF9QZ0hrVzk3ampsTHk2Tjh0UHdBUnRiYks1Nk53VEZmLDEwMDE3NTA5OA0200aI5Z7CuS/pdf?s=ap,AC1FA5E2-0004,10,1.87,PT
in_1OqwpsBLRKsqtTK3s3QIDVCw,2024-03-05 13:11:08,10,usd,https://pay.stripe.com/invoice/acct_1OcnxzBLRKsqtTK3/live_YWNjdF8xT2NueHpCTFJLc3F0VEszLF9QZ0pTMlh2Rk5SeDBqTVRHaHZ1d1dhbXhGTWMzOGw0LDEwMDE4MTQ5Mw0200WBPYIOQi/pdf?s=ap,AC1FA5E2-0005,10,1.87,PTThis project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request for any changes.