import wixCRM from ‘wix-crm-backend’; // To create contacts in Wix CRM
import { ok, badRequest } from ‘wix-http-functions’; // For HTTP responses
// This function listens for POST requests and creates a contact
export async function post_insertContact(request) {
try {
// Parse the incoming request body
const requestBody = await request.body.json();
// Log incoming request
console.log('Received request body:', requestBody);
// Extract contact details from the request body
const { email, name, phone, countryCode } = requestBody;
// Validate required fields
if (!email || !name) {
return badRequest({ body: { error: "Missing email or name" } });
}
// Ensure we are passing the required structure
const newContact = {
"info": {
"name": {
"first": name,
"last": name,
},
"emails": {
"items": [
{
"tag": "MAIN", // Tag the email as 'MAIN'
"email": email
}
]
}
}
};
// Add phone if provided
if (phone) {
newContact.info.phones = {
"items": [
{
"tag": "HOME", // Tag the phone number as 'HOME'
"phone": phone,
"countryCode": countryCode || "US" // Ensure countryCode is included
}
]
};
}
// Create contact using Wix CRM API
const result = await wixCRM.createContact(newContact);
return ok({ body: { message: "Contact created successfully", data: result } });
} catch (error) {
console.error(“Error creating contact:”, error);
return badRequest({ body: { error: error.message } });
}
}
I am using this code but I am facing issue Please help, I tried every thing as same as Contact Created | REST
error: “Type ‘{ info: { name: { first: any; last: any; }; emails: { items: { tag: string; email: any; }; }; }; }’ has no properties in common with type ‘ContactInfo’.”