Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Last active June 16, 2021 04:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonmichael/62cc9661569cdea990dc2020ac36582a to your computer and use it in GitHub Desktop.
Save simonmichael/62cc9661569cdea990dc2020ac36582a to your computer and use it in GitHub Desktop.
paypalcsv - download transactions CSV from Paypal's API
#!/bin/bash
# Download last 30 days of transaction history from Paypal as CSV.
# brew install jsonpp jq
# npm install -g json2csv
# credentials from an API app created in https://developer.paypal.com
CLIENT_ID=
SECRET=
TOKEN=`curl -s https://api-m.paypal.com/v1/oauth2/token -d "grant_type=client_credentials" -u "$CLIENT_ID:$SECRET" | jq .access_token -r`
# max date range is 31d
START=`date -v-30d +%Y-%m-%dT00:00:00%z`
END=`date -v+1d +%Y-%m-%dT00:00:00%z`
# max page size is 500, so this will fetch at most 500 transactions.
# sed strips the field name prefixes, while keeping them unique
curl -s -H "Authorization: Bearer $TOKEN" "https://api.paypal.com/v1/reporting/transactions?start_date=$START&end_date=$END&fields=all&page_size=500&page=1" \
| json2csv --unwind --flatten-objects \
| sed -E -e '1s/([^\.]+)\.(value|currency_code)/\1_\2/g' -e '1s/"[^"]*\.([^"]*)"/"\1"/g'
# or this ?
#curl -s -H "Authorization: Bearer $TOKEN" "https://api.paypal.com/v1/reporting/transactions?start_date=$START&end_date=$END&fields=all&page_size=500&page=1" \
#| jq '.transaction_details' \
#| json2csv --unwind --flatten-objects \
#| sed -E -e '1s/([^\.]+)\.(value|currency_code)/\1_\2/g' -e '1s/"[^"]*\.([^"]*)"/"\1"/g'
echo
@sbibauw
Copy link

sbibauw commented Jun 14, 2021

If anybody gets a "PERMISSION_DENIED, No Permission for the requested operation" error when running this, the solution is simply to add -H "Accept: application/json" -H "Accept-Language: en_US" to the curl TOKEN request on line 11:

TOKEN=`curl -s https://api-m.paypal.com/v1/oauth2/token \
  -H "Accept: application/json" \
  -H "Accept-Language: en_US" \
  -u "$CLIENT_ID:$SECRET" \
  -d "grant_type=client_credentials" | jq .access_token -r`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment