import { action, input } from "@prismatic-io/spectral";
export const createContact = action({
display: {
label: "Create Contact",
description: "Create a new contact record in the CRM",
directions: "Provide the contact's name and email address.",
important: true,
},
inputs: {
connection: input({
label: "Connection",
type: "connection",
required: true,
}),
firstName: input({
label: "First Name",
type: "string",
required: true,
example: "Alice",
}),
lastName: input({
label: "Last Name",
type: "string",
required: true,
example: "Smith",
}),
email: input({
label: "Email Address",
type: "string",
required: true,
placeholder: "[email protected]",
}),
},
perform: async (context, params) => {
const { connection, firstName, lastName, email } = params;
const apiKey = connection.fields.apiKey as string;
const baseUrl = connection.fields.baseUrl as string;
context.logger.info("Creating contact", { email });
const response = await fetch(`${baseUrl}/contacts`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ firstName, lastName, email }),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API error ${response.status}: ${errorBody}`);
}
const contact = await response.json();
context.logger.info("Contact created", { id: contact.id });
return {
data: contact,
instanceState: {
...context.instanceState,
lastCreatedContactId: contact.id,
},
};
},
examplePayload: {
data: {
id: "cnt_01HXYZ",
firstName: "Alice",
lastName: "Smith",
email: "[email protected]",
createdAt: "2024-01-15T10:30:00Z",
},
},
});