AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import { Property, createAction, OAuth2PropertyValue } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { quickbooksAuth } from '../lib/auth';
import { quickbooksApiCall, quickbooksQuery, QuickbooksEntityResponse } from '../lib/common';
import {
QuickbooksAccount,
QuickbooksVendor,
QuickbooksPurchase,
QuickbooksRef,
} from '../lib/types';
export const createExpenseAction = createAction({
auth: quickbooksAuth,
name: 'create_expense',
classification: 'WRITE',
displayName: 'Create Expense',
description: 'Creates an expense transaction (purchase) in QuickBooks.',
audience: 'both',
aiMetadata: {
description: 'Record a new expense (purchase) paid from a chosen bank or credit-card account, with one or more account-based line items (each needs an amount and an expense account Id) and a payment type of cash, check, or credit card. Optionally attach a payee/vendor, payment date, and memo. Not idempotent: each call creates a new purchase, so guard against duplicates.',
idempotent: false,
},
props: {
accountRef: Property.Dropdown({
auth: quickbooksAuth,
displayName: 'Bank/Credit Card Account',
description: 'The account from which the expense was paid.',
required: true,
refreshers: [],
options: async ({ auth }) => {
if (!auth) {
return { disabled: true, placeholder: 'Connect account', options: [] };
}
const { access_token, props } = auth as OAuth2PropertyValue;
const companyId = props?.['companyId'];
const query = `SELECT Id, Name, AccountType FROM Account STARTPOSITION 1 MAXRESULTS 1000`;
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/account#query-an-account
const response = await quickbooksQuery<QuickbooksEntityResponse<QuickbooksAccount>>({
accessToken: access_token,
companyId,
query,
});
if (response.Fault) {
throw new Error(
`QuickBooks API Error fetching accounts: ${response.Fault.Error.map(
(e: { Message: string }) => e.Message,
).join(', ')}`,
);
}
const accounts = response.QueryResponse?.['Account'] ?? [];
return {
disabled: false,
options: accounts.map((account) => ({
label: `${account.Name} (${account.AccountType})`,
value: account.Id,
})),
};
},
}),
paymentType: Property.StaticDropdown({
displayName: 'Payment Type',
required: true,
options: {
options: [
{ label: 'Cash', value: 'Cash' },
{ label: 'Check', value: 'Check' },
{ label: 'Credit Card', value: 'CreditCard' },
],
},
defaultValue: 'Cash',
}),
entityRef: Property.Dropdown({
auth: quickbooksAuth,
displayName: 'Payee (Vendor)',
description: 'Optional - The vendor the expense was paid to.',
required: false,
refreshers: [],
options: async ({ auth }) => {
if (!auth) {
return { disabled: true, placeholder: 'Connect account', options: [] };
}
const { access_token, props } = auth as OAuth2PropertyValue;
const companyId = props?.['companyId'];
const query = `SELECT Id, DisplayName FROM Vendor STARTPOSITION 1 MAXRESULTS 1000`;
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/vendor#query-a-vendor
const response = await quickbooksQuery<QuickbooksEntityResponse<QuickbooksVendor>>({
accessToken: access_token,
companyId,
query,
});
if (response.Fault) {
throw new Error(
`QuickBooks API Error fetching vendors: ${response.Fault.Error.map(
(e: { Message: string }) => e.Message,
).join(', ')}`,
);
}
const vendors = response.QueryResponse?.['Vendor'] ?? [];
return {
disabled: false,
options: vendors.map((vendor) => ({
label: vendor.DisplayName,
value: vendor.Id,
})),
};
},
}),
txnDate: Property.DateTime({
displayName: 'Payment Date',
description: 'The date the expense occurred.',
required: false, // Defaults to today if empty
}),
// Line items for the expense details
lineItems: Property.Array({
displayName: 'Line Items',
description:
'Details of the expense (e.g., categories or items purchased). At least one line is required.',
required: true,
properties: {
amount: Property.Number({
displayName: 'Amount',
required: true,
}),
description: Property.ShortText({
displayName: 'Description',
required: false,
}),
detailType: Property.StaticDropdown({
displayName: 'Detail Type',
required: true,
options: {
options: [
{
label: 'Account Based Expense Line Detail',
value: 'AccountBasedExpenseLineDetail',
},
],
},
defaultValue: 'AccountBasedExpenseLineDetail',
}),
expenseAccountId: Property.ShortText({
displayName: 'Expense Category/Account ID',
description:
'The Id of the expense account. Use the "Find Account" action (filter by type Expense) to look this up.',
required: true,
}),
},
}),
privateNote: Property.LongText({
displayName: 'Memo (Private Note)',
description: 'Internal note about the expense.',
required: false,
}),
},
async run(context) {
const { access_token } = context.auth;
const companyId = context.auth.props?.['companyId'] as string;
const props = context.propsValue;
const lines = (props['lineItems'] as any[]).map((line) => {
const detail: any = {
Amount: line['amount'],
Description: line['description'],
DetailType: line['detailType'],
};
if (line['detailType'] === 'AccountBasedExpenseLineDetail') {
if (!line['expenseAccountId']) {
throw new Error(
'Expense Category/Account ID is required for Account Based Expense Line Detail.',
);
}
detail.AccountBasedExpenseLineDetail = {
AccountRef: { value: line['expenseAccountId'] },
};
}
return detail;
});
if (lines.length === 0) {
throw new Error('At least one line item is required.');
}
const expensePayload: Partial<QuickbooksPurchase> = {
AccountRef: { value: props['accountRef'] },
PaymentType: props['paymentType'] as 'Cash' | 'Check' | 'CreditCard',
Line: lines,
...(props['entityRef'] && { EntityRef: { value: props['entityRef'] } as QuickbooksRef }),
...(props['txnDate'] && { TxnDate: props['txnDate'].split('T')[0] }),
...(props['privateNote'] && { PrivateNote: props['privateNote'] }),
};
// https://developer.intuit.com/app/developer/qbo/docs/api/accounting/all-entities/purchase#create-a-purchase
const response = await quickbooksApiCall<{
Purchase: QuickbooksPurchase;
time: string;
Fault?: { Error: { Message: string; Detail?: string; code: string }[]; type: string };
}>({
accessToken: access_token,
companyId,
method: HttpMethod.POST,
resourceUri: '/purchase',
body: expensePayload,
});
if (response.Fault) {
throw new Error(
`QuickBooks API Error creating expense: ${response.Fault.Error.map(
(e: any) => e.Message,
).join(', ')} - Detail: ${response.Fault.Error.map((e: any) => e.Detail).join(', ')}`,
);
}
return response.Purchase;
},
});