Skip to content

Instantly share code, notes, and snippets.

@signorecello
Last active April 12, 2025 11:25
Show Gist options
  • Save signorecello/91295d531bc28377f20d556ca0d82602 to your computer and use it in GitHub Desktop.
Save signorecello/91295d531bc28377f20d556ca0d82602 to your computer and use it in GitHub Desktop.
Converts a Gnosis Pay transaction to CSV format

Gnosis Pay -> CSV

If you're like me and you use great open-source finance software like ActualBudget you probably want your Gnosis Pay transactions in CSV format for an easy import.

Fear not, me (and ChatGPT) have your back:

  1. Login to GnosisPay
  2. Open the console (Ctrl + Shift + J or Cmd + Option + J)
  3. Paste this script. Don't blindly trust me, if you don't know JS then ask ChatGPT to explain what does this script do.
(async () => {
    const notBefore = new Date('2025-03-24T00:00:00Z')
    const apiUrl = "https://app.gnosispay.com/api/v1/transactions";

    try {
        // Fetch the JSON data using the existing authorization cookies
        const response = await fetch(apiUrl, {
            credentials: 'include', // Include cookies for authorization
        });

        if (!response.ok) {
            throw new Error(`Failed to fetch data: ${response.statusText}`);
        }

        const data = await response.json();

        // Prepare the CSV header
        const csvHeader = "Date,Transaction Amount,Merchant Name,Type\n";

        const filteredData = data.filter(item => {
            const createdAt = new Date(item.createdAt);
            return createdAt >= notBefore && item.status === "Approved";
        });

        // Map the JSON data to CSV rows
        const csvRows = filteredData.map(item => {
            const createdAt = item.createdAt ? new Date(item.createdAt).toLocaleDateString('en-GB').replace(/\//g, '-') : "";           // Format to DD-MM-YYYY
            const decimals = item.billingCurrency?.decimals || 2; // Default to 2 decimals if not provided
            const rawAmount = parseFloat(item.billingAmount || "0");
            const transactionAmount = (rawAmount / Math.pow(10, decimals)).toFixed(decimals);
            const merchantName = (item.merchant?.name || "").trim().replace(/[	\n,]+/g, " ");
            const kind = item.kind || "";

            return `${createdAt},${transactionAmount},"${merchantName}",${kind}`;
        });

        // Combine header and rows into a single CSV string
        const csvContent = csvHeader + csvRows.join("\n");

        // Create a Blob and trigger download
        const blob = new Blob([csvContent], { type: 'text/csv' });
        const link = document.createElement('a');
        link.href = URL.createObjectURL(blob);
        link.download = 'transactions.csv';
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);

        console.log("CSV file downloaded successfully.");
    } catch (error) {
        console.error("Error:", error);
    }
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment