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

2.3 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { HttpMethod } from '@activepieces/pieces-common';
import { resendAuth } from '../..';
import { resendClient } from '../common/client';
import { resendProps } from '../common/props';
import { createContactOutputSchema } from '../output-schemas';

export const createContact = createAction({
  name: 'create_contact',
  classification: 'WRITE',
  auth: resendAuth,
  displayName: 'Create Contact',
  outputSchema: createContactOutputSchema,
  description: 'Add a contact to a Resend audience',
  audience: 'both',
  aiMetadata: { description: 'Adds a new contact (email plus optional name and subscription status) to a specific Resend audience. Use this to grow a mailing list; requires the target audience ID (use List Audiences to find it). Not idempotent — each call creates a new contact, so guard against re-adding the same email.', idempotent: false },
  propertyGroups: [
    {
      key: 'contact',
      display: 'section',
      label: 'New Contact',
      icon: 'user',
      props: ['audience_id', 'email', 'first_name', 'last_name', 'unsubscribed'],
    },
  ],
  props: {
    audience_id: resendProps.audienceId,
    email: Property.ShortText({ displayName: 'Email', placeholder: '[email protected]', required: true }),
    first_name: Property.ShortText({ displayName: 'First Name', placeholder: 'Jane', width: 'half', required: false }),
    last_name: Property.ShortText({ displayName: 'Last Name', placeholder: 'Doe', width: 'half', required: false }),
    unsubscribed: Property.Checkbox({
      displayName: 'Unsubscribed',
      description: 'Turn on to add the contact as unsubscribed from broadcasts.',
      required: false,
      defaultValue: false,
    }),
  },
  async run({ auth, propsValue }) {
    const body: Record<string, unknown> = { email: propsValue.email };
    if (propsValue.first_name) body['first_name'] = propsValue.first_name;
    if (propsValue.last_name) body['last_name'] = propsValue.last_name;
    if (propsValue.unsubscribed !== undefined) body['unsubscribed'] = propsValue.unsubscribed;

    return await resendClient.sendRequest<{ object: string; id: string }>({ auth: auth.secret_text, method: HttpMethod.POST, path: `/audiences/${propsValue.audience_id}/contacts`, body: body });
  },
});