This project contains scripts to fetch and analyze Costco receipt data using the Costco API. Note that you need to log in to costco.com manually and look at the network requests to get the bearer token, the client ID, and the client identifier.
-
Install dependencies:
uv sync
-
Set up environment variables:
cp .env.example .env
Then edit
.envand add your actual Costco API credentials. You can get these by looking at the network requests in your browser when you're logged in to costco.com and looking at the headers in the graphql requests.COSTCO_BEARER_TOKEN- Your bearer tokenCOSTCO_CLIENT_ID- Your client IDCOSTCO_CLIENT_IDENTIFIER- Your client identifier
fetch_receipts.py- Fetches receipt lists for date rangesfetch_receipt_details.py- Fetches detailed receipt information by barcodefetch_all_receipt_details.py- Batch fetches detailed receipts for all barcodes found in data files
COSTCO_BEARER_TOKEN- Your Costco API bearer tokenCOSTCO_CLIENT_ID- Your Costco API client IDCOSTCO_CLIENT_IDENTIFIER- Your Costco API client identifier
All scripts will automatically load the required credentials from your .env file. Make sure to set up all the environment variables before running any scripts.
python fetch_receipts.py
python fetch_all_receipt_details.pyUse the generate_csv_file.py script to export all receipt data to CSV:
python generate_csv_file.pyThis script uses the following DuckDB query to process the JSON data and save as costco-items.csv:
WITH receipts AS (
SELECT
json_extract(data, '$.receiptsWithCounts.receipts[0]') AS r
FROM read_json_auto('costco-analysis/data/receipts/*json')
)
SELECT
r ->> 'transactionDate' AS transaction_date,
r ->> 'transactionBarcode' AS transaction_barcode,
r ->> 'warehouseName' AS warehouse_name,
item ->> 'itemNumber' AS item_number,
item ->> 'itemDescription01' AS description,
item ->> 'itemDescription02' AS description2,
description || ' ' || description2 AS combined_description,
(item ->> 'itemUnitPriceAmount')::DOUBLE AS item_unit_price
FROM receipts,
UNNEST(json_extract(r, '$.itemArray[*]')) AS t(item)