export_amazon_transactions_csv
Export Amazon payment transactions to CSV for financial tracking. Extracts transaction data including date, order ID, amount, payment method, and card information from order detail pages.
Instructions
Export Amazon payment transactions to CSV file. Extracts transaction data from each order's detail page. CSV columns include: date, order ID, amount, payment method, card info. For faster bulk transaction export, consider get_amazon_transactions which scrapes the dedicated transactions page.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| region | Yes | Amazon region code | |
| year | No | Year to export (defaults to current year) | |
| start_date | No | Start date in ISO format (YYYY-MM-DD) | |
| end_date | No | End date in ISO format (YYYY-MM-DD) | |
| output_path | No | Full path to save CSV file. Defaults to ~/Downloads/amazon-{region}-transactions-{year}-{date}.csv | |
| max_orders | No | Maximum number of orders to process |
Implementation Reference
- src/index.ts:345-381 (schema)Tool definition including name, description, and input schema for validation. Registers the tool in the MCP server tools list.{ name: "export_amazon_transactions_csv", description: "Export Amazon payment transactions to CSV file. Extracts transaction data from each order's detail page. CSV columns include: date, order ID, amount, payment method, card info. For faster bulk transaction export, consider get_amazon_transactions which scrapes the dedicated transactions page.", inputSchema: { type: "object", properties: { region: { type: "string", description: "Amazon region code", enum: getRegionCodes(), }, year: { type: "number", description: "Year to export (defaults to current year)", }, start_date: { type: "string", description: "Start date in ISO format (YYYY-MM-DD)", }, end_date: { type: "string", description: "End date in ISO format (YYYY-MM-DD)", }, output_path: { type: "string", description: "Full path to save CSV file. Defaults to ~/Downloads/amazon-{region}-transactions-{year}-{date}.csv", }, max_orders: { type: "number", description: "Maximum number of orders to process", }, }, required: ["region"], }, },
- src/index.ts:1085-1157 (handler)Executes the tool: validates region, fetches orders with transaction extraction enabled, generates output path, exports transactions to CSV, and returns success/error with file path and stats.case "export_amazon_transactions_csv": { const regionParam = args?.region as string | undefined; const regionError = validateRegion(regionParam, args); if (regionError) return regionError; const region = regionParam!; const currentPage = await getPage(); const year = args?.year as number | undefined; const startDate = args?.start_date as string | undefined; const endDate = args?.end_date as string | undefined; const maxOrders = args?.max_orders as number | undefined; const outputPath = getOutputPath( args?.output_path as string | undefined, "transactions", region, { year, startDate, endDate }, ); const fetchResult = await fetchOrders(currentPage, amazonPlugin, { region, year, startDate, endDate, includeItems: false, includeShipments: false, includeTransactions: true, maxOrders, }); const timeEstimate = estimateExtractionTime(fetchResult.orders.length, { includeItems: false, includeShipments: false, }); const exportResult = await exportTransactionsCSV( fetchResult.transactions, outputPath, ); return { content: [ { type: "text", text: JSON.stringify( { status: exportResult.success ? "success" : "error", params: { region, year, startDate, endDate, maxOrders, outputPath, }, filePath: exportResult.filePath, rowCount: exportResult.rowCount, error: exportResult.error, fetchErrors: fetchResult.errors, timing: { orderCount: fetchResult.orders.length, transactionCount: fetchResult.transactions.length, estimate: timeEstimate.formattedEstimate, warnings: timeEstimate.warnings, recommendations: timeEstimate.recommendations, }, }, null, 2, ), }, ], }; }
- src/tools/export-csv.ts:113-133 (helper)Supporting function to write transaction data to CSV file using predefined columns and standard ExportResult format.export async function exportTransactionsCSV( transactions: Transaction[], outputPath: string, ): Promise<ExportResult> { try { const csv = toCSVWithColumns(transactions, TRANSACTION_CSV_COLUMNS); await writeFile(outputPath, csv, "utf-8"); return { success: true, filePath: outputPath, rowCount: transactions.length, }; } catch (error) { return { success: false, filePath: outputPath, rowCount: 0, error: String(error), }; }
- src/tools/csv-columns.ts:371-444 (schema)Defines the CSV column structure for transaction exports, specifying headers and data keys for output formatting.export const TRANSACTION_CSV_COLUMNS: CSVColumn<Transaction>[] = [ { key: "date", header: "Transaction Date", getValue: (t) => formatDate(t.date), }, { key: "orderIds", header: "Order ID(s)", getValue: (t) => t.orderIds.join(", "), }, { key: "vendor", header: "Payment Method", getValue: (t) => t.vendor }, { key: "cardInfo", header: "Card Info", getValue: (t) => t.cardInfo }, { key: "amount", header: "Amount", getValue: (t) => formatMoney(t.amount) }, { key: "currency", header: "Currency", getValue: (t) => t.amount.currency }, ]; /** * Gift card transaction data for CSV export. */ export interface GiftCardTransactionCSVData { date: Date; description: string; amount: Money; closingBalance: Money; type: string; orderId?: string; claimCode?: string; serialNumber?: string; region: string; } /** * Gift card transaction CSV columns. */ export const GIFT_CARD_CSV_COLUMNS: CSVColumn<GiftCardTransactionCSVData>[] = [ { key: "date", header: "Date", getValue: (t) => formatDate(t.date) }, { key: "description", header: "Description", getValue: (t) => t.description }, { key: "type", header: "Type", getValue: (t) => t.type }, { key: "amount", header: "Amount", getValue: (t) => formatMoney(t.amount) }, { key: "closingBalance", header: "Closing Balance", getValue: (t) => formatMoney(t.closingBalance), }, { key: "orderId", header: "Order ID", getValue: (t) => t.orderId || "" }, { key: "claimCode", header: "Claim Code", getValue: (t) => t.claimCode || "", }, { key: "serialNumber", header: "Serial Number", getValue: (t) => t.serialNumber || "", }, { key: "currency", header: "Currency", getValue: (t) => t.amount.currency }, { key: "region", header: "Region", getValue: (t) => t.region }, ]; /** * Get column headers for a given export type. */ export function getColumnHeaders( exportType: "orders" | "items" | "shipments" | "transactions" | "gift-cards", ): string[] { switch (exportType) { case "orders": return ORDER_CSV_COLUMNS.map((c) => c.header); case "items": return ITEM_CSV_COLUMNS.map((c) => c.header); case "shipments": return SHIPMENT_CSV_COLUMNS.map((c) => c.header); case "transactions":