Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ ynab scheduled delete <id>
### Raw API Access

```bash
ynab api GET /budgets
ynab api POST /budgets/{budget_id}/transactions --data '{"transaction": {...}}'
ynab api GET /plans
ynab api POST /plans/{plan_id}/transactions --data '{"transaction": {...}}'
```

### MCP Server
Expand Down
14 changes: 8 additions & 6 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@
"commander": "^14.0.2",
"conf": "^15.0.2",
"dayjs": "^1.11.19",
"ynab": "^2.10.0",
"zod": "^3.25.0"
"ynab": "^4.0.0",
"zod": "^4.3.6"
},
"devDependencies": {
"@biomejs/biome": "^2.3.11",
"@types/bun": "latest",
"oxlint": "^1.38.0",
"tsup": "^8.0.0",
"tsx": "^4.7.0",
"typescript": "^5.3.0",
"typescript": "^6.0.2",
"vitest": "^4.0.4"
},
"engines": {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export function createApiCommand(): Command {

cmd
.argument('<method>', 'HTTP method (GET, POST, PUT, PATCH, DELETE)')
.argument('<path>', 'API path (e.g., /budgets or /budgets/{budget_id}/transactions)')
.option('-b, --budget <id>', 'Budget ID (used to replace {budget_id} in path)')
.argument('<path>', 'API path (e.g., /plans or /plans/{plan_id}/transactions)')
.option('-b, --budget <id>', 'Budget ID (used to replace {plan_id} or {budget_id} in path)')
.option('--data <json>', 'JSON data for POST/PUT/PATCH requests')
.description('Make raw API calls to YNAB')
.action(
Expand Down
10 changes: 9 additions & 1 deletion src/commands/categories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ export function createCategoriesCommand(): Command {
updateData.goal_target = amountToMilliunits(options.goalTarget);
}

const category = await client.updateCategory(id, { category: updateData }, options.budget);
const category = await client.updateCategory(
id,
{
category: updateData as NonNullable<
Parameters<typeof client.updateCategory>[1]['category']
>,
},
options.budget
);
outputJson(category);
}
)
Expand Down
46 changes: 25 additions & 21 deletions src/commands/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,34 +311,38 @@ export function createTransactionsCommand(): Command {
if (isAlreadySplit) {
await client.deleteTransaction(id, options.budget);

const transaction = {
account_id: existingTransaction.account_id,
date: existingTransaction.date,
amount: existingTransaction.amount,
payee_id: existingTransaction.payee_id,
payee_name: existingTransaction.payee_name,
category_id: null,
memo: existingTransaction.memo,
cleared: existingTransaction.cleared,
approved: existingTransaction.approved,
flag_color: existingTransaction.flag_color,
subtransactions: splitsInMilliunits,
} as unknown as NonNullable<
Parameters<typeof client.createTransaction>[0]['transaction']
>;

const recreatedTransaction = await client.createTransaction(
{
transaction: {
account_id: existingTransaction.account_id,
date: existingTransaction.date,
amount: existingTransaction.amount,
payee_id: existingTransaction.payee_id,
payee_name: existingTransaction.payee_name,
category_id: null,
memo: existingTransaction.memo,
cleared: existingTransaction.cleared,
approved: existingTransaction.approved,
flag_color: existingTransaction.flag_color,
subtransactions: splitsInMilliunits,
},
},
{ transaction },
options.budget
);
outputJson(recreatedTransaction);
} else {
const transactionData = {
category_id: null,
subtransactions: splitsInMilliunits,
} as unknown as NonNullable<
Parameters<typeof client.updateTransaction>[1]['transaction']
>;

const transaction = await client.updateTransaction(
id,
{
transaction: {
category_id: null,
subtransactions: splitsInMilliunits,
},
},
{ transaction: transactionData },
options.budget
);
outputJson(transaction);
Expand Down
22 changes: 12 additions & 10 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,27 @@ export class YnabClient {

async getBudgets(includeAccounts = false) {
const api = await this.getApi();
const response = await api.budgets.getBudgets(includeAccounts);
const response = await api.plans.getPlans(includeAccounts);
return {
budgets: response.data.budgets,
budgets: response.data.plans,
server_knowledge: 0,
};
}

async getBudget(budgetId?: string, lastKnowledgeOfServer?: number) {
const api = await this.getApi();
const id = await this.getBudgetId(budgetId);
const response = await api.budgets.getBudgetById(id, lastKnowledgeOfServer);
const response = await api.plans.getPlanById(id, lastKnowledgeOfServer);
return {
budget: response.data.budget,
budget: response.data.plan,
server_knowledge: response.data.server_knowledge,
};
}

async getBudgetSettings(budgetId?: string) {
const api = await this.getApi();
const id = await this.getBudgetId(budgetId);
const response = await api.budgets.getBudgetSettingsById(id);
const response = await api.plans.getPlanSettingsById(id);
return response.data.settings;
}

Expand Down Expand Up @@ -174,7 +174,7 @@ export class YnabClient {
async getBudgetMonths(budgetId?: string, lastKnowledgeOfServer?: number) {
const api = await this.getApi();
const id = await this.getBudgetId(budgetId);
const response = await api.months.getBudgetMonths(id, lastKnowledgeOfServer);
const response = await api.months.getPlanMonths(id, lastKnowledgeOfServer);
return {
months: response.data.months,
server_knowledge: response.data.server_knowledge,
Expand All @@ -184,7 +184,7 @@ export class YnabClient {
async getBudgetMonth(month: string, budgetId?: string) {
const api = await this.getApi();
const id = await this.getBudgetId(budgetId);
const response = await api.months.getBudgetMonth(id, month);
const response = await api.months.getPlanMonth(id, month);
return response.data.month;
}

Expand Down Expand Up @@ -369,9 +369,11 @@ export class YnabClient {
async rawApiCall(method: string, path: string, data?: unknown, budgetId?: string) {
await this.getApi();

const fullPath = path.includes('{budget_id}')
? path.replace('{budget_id}', await this.getBudgetId(budgetId))
: path;
let fullPath = path;
if (path.includes('{budget_id}') || path.includes('{plan_id}')) {
const id = await this.getBudgetId(budgetId);
fullPath = path.replaceAll('{budget_id}', id).replaceAll('{plan_id}', id);
}

const url = `https://api.ynab.com/v1${fullPath}`;
const accessToken = (await auth.getAccessToken()) || process.env.YNAB_API_KEY;
Expand Down
1 change: 1 addition & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function formatErrorResponse(name: string, detail: string, statusCode: number):

outputJson({ error: { name, detail: enhancedDetail, statusCode } });
process.exit(1);
throw new Error('process.exit returned unexpectedly');
}

export function handleYnabError(error: unknown): never {
Expand Down
Loading