Skip to content

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

1.9 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { Client } from '@notionhq/client';

import { notionAuth } from '../auth';
import { getNotionToken, notionCommon } from '../common';
import { markdownToBlocks } from '@tryfabric/martian';
import { BlockObjectRequest } from '@notionhq/client/build/src/api-endpoints';
import { appendToPageActionOutputSchema } from '../output-schemas';

export const appendToPage = createAction({
  auth: notionAuth,
  name: 'append_to_page',
  classification: 'WRITE',
  displayName: 'Append to Page',
  description: 'Appends content to the end of a page.',
  audience: 'human',
  aiMetadata: {
    description:
      'Appends new block content (parsed from markdown) to the end of an existing Notion page or block. Use when an agent must add to a page without altering existing content; requires the target page/block id. Not idempotent: each call appends again, duplicating the content on repeat.',
    idempotent: false,
  },
  propertyGroups: [
    {
      key: 'page',
      display: 'section',
      label: 'Page',
      icon: 'file',
      props: ['pageId'],
    },
    {
      key: 'content',
      display: 'section',
      label: 'Content',
      icon: 'text',
      props: ['content'],
    },
  ],
  props: {
    pageId: notionCommon.page,
    content: Property.LongText({
      displayName: 'Content',
      description: 'Markdown is rendered into Notion blocks.',
      required: true,
    }),
  },
  outputSchema: appendToPageActionOutputSchema,
  async run(context) {
    const { pageId, content } = context.propsValue;

    const notion = new Client({
      auth: getNotionToken(context.auth),
      notionVersion: '2022-02-22',
    });

    return await notion.blocks.children.append({
      block_id: pageId as string,
      children: markdownToBlocks(content) as unknown as BlockObjectRequest[],
    });
  },
});