Import Transactions
ynab_import_transactionsImport available transactions from all linked financial accounts into your YNAB budget, triggering a sync equivalent to clicking the Import button in the app.
Instructions
Imports available transactions on all linked accounts for the budget. This triggers an import from connected financial institutions (equivalent to clicking 'Import' in the YNAB app).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| budgetId | No | The ID of the budget (optional, defaults to YNAB_BUDGET_ID environment variable) |
Implementation Reference
- The execute() function that runs the import. Calls the YNAB API's importTransactions method with the budget ID and returns the result (success/failure with transaction IDs).
export async function execute(input: ImportTransactionsInput, api: ynab.API) { try { const budgetId = getBudgetId(input.budgetId); console.error(`Importing transactions for budget ${budgetId}`); const response = await api.transactions.importTransactions(budgetId); return { content: [{ type: "text" as const, text: JSON.stringify({ success: true, transaction_ids: response.data.transaction_ids, imported_count: response.data.transaction_ids.length, message: response.data.transaction_ids.length > 0 ? `Successfully imported ${response.data.transaction_ids.length} transaction(s)` : "No new transactions to import", }, null, 2), }], }; } catch (error) { console.error("Error importing transactions:", error); return { content: [{ type: "text" as const, text: JSON.stringify({ success: false, error: getErrorMessage(error), }, null, 2), }], }; } } - Input schema definition using Zod. Accepts an optional budgetId string (defaults to YNAB_BUDGET_ID env var).
export const inputSchema = { budgetId: z.string().optional().describe("The ID of the budget (optional, defaults to YNAB_BUDGET_ID environment variable)"), }; - src/index.ts:117-121 (registration)Registration of the tool with the MCP server using server.registerTool(), binding the name, description, inputSchema, and execute handler.
server.registerTool(ImportTransactionsTool.name, { title: "Import Transactions", description: ImportTransactionsTool.description, inputSchema: ImportTransactionsTool.inputSchema, }, async (input) => ImportTransactionsTool.execute(input, api)); - Helper function getBudgetId() that resolves the budget ID from input or environment variable.
function getBudgetId(inputBudgetId?: string): string { const budgetId = inputBudgetId || process.env.YNAB_BUDGET_ID || ""; if (!budgetId) { throw new Error("No budget ID provided. Please provide a budget ID or set the YNAB_BUDGET_ID environment variable."); } return budgetId; } - src/tools/errorUtils.ts:5-39 (helper)getErrorMessage helper used to extract meaningful error messages from various error types including YNAB API errors.
export function getErrorMessage(error: unknown): string { // Handle standard Error objects if (error instanceof Error) { return error.message; } // Handle YNAB API error responses which have the structure: // { error: { id: '...', name: '...', detail: '...' } } if ( typeof error === 'object' && error !== null && 'error' in error && typeof (error as any).error === 'object' ) { const ynabError = (error as any).error; if (ynabError.detail) { return ynabError.detail; } if (ynabError.name) { return ynabError.name; } } // Fallback: try to stringify the error try { const stringified = JSON.stringify(error); if (stringified !== '{}') { return stringified; } } catch { // Ignore stringify errors } return 'Unknown error occurred'; }