Search...
createContact( )
Creates a new contact.
Description
The createContact()
function returns a Promise
that resolves to the new contact when it is created.
The info
parameter object must include a name, phone number, or email address.
If all 3 of these parameters are missing,
the contact won't be created.
By default, if the call contains an email already in use by another contact, the new contact won't be created. To override this behavior, set allowDuplicates
in the options
object to true
.
Admin Method
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Authorization
Request
This endpoint does not take any parameters
Response Object
Contact.
NAME
TYPE
DESCRIPTION
contact
Contact
Contact.
Status/Error Codes
Was this helpful?
Create a new contact using a name and email (dashboard page code)
Copy Code
1import { contacts } from 'wix-crm.v2';23/* Sample contact info value:4 * {5 * name: {6 * first: 'John',7 * last: 'Doe'8 * },9 * emails: {10 * items: [11 * {12 * email: 'johndoe1@example.com',13 * }14 * ]15 * }16 * }17 */1819export async function myCreateContactFunction(info) {2021 try {22 const newContact = await contacts.createContact(info);23 console.log('Successfully created a new contact:', newContact);2425 return newContact;26 } catch (error) {27 console.log(error);28 // Handle the error29 }30}3132/* Promise resolves to:33 * {34 * "revision": 1,35 * "source": {36 * "sourceType": "WIX_CODE",37 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"38 * },39 * "lastActivity": {40 * "activityDate": "2023-12-24T11:50:46.048Z",41 * "activityType": "CONTACT_CREATED"42 * },43 * "primaryInfo": {44 * "email": "johndoe1@example.com",45 * "phone": "6465676359"46 * },47 * "info": {48 * "name": {49 * "first": "John",50 * "last": "Doe"51 * },52 * "emails": {53 * "items": [54 * {55 * "tag": "UNTAGGED",56 * "email": "johndoe1@example.com",57 * "primary": true,58 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"59 * }60 * ]61 * },62 * "phones": {},63 * "addresses": {},64 * "extendedFields": {65 * "items": {66 * "contacts.displayByLastName": "Doe John",67 * "invoices.vatId": "",68 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",69 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",70 * "contacts.displayByFirstName": "John Doe"71 * }72 * }73 * },74 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",75 * "_createdDate": "2023-12-24T11:50:46.049Z",76 * "_updatedDate": "2023-12-24T12:50:46.047Z"77 * }78 */79
Create a new contact using a name and email (export from backend code)
Copy Code
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample contact info value:6 * {7 * name: {8 * first: 'John',9 * last: 'Doe'10 * },11 * emails: {12 * items: [13 * {14 * email: 'johndoe1@example.com',15 * }16 * ]17 * }18 * }19 */2021export const myCreateContactFunction = webMethod(Permissions.Anyone, async (info) => {2223 try {24 const elevatedCreateContact = elevate(contacts.createContact);25 const newContact = await elevatedCreateContact(info);26 console.log('Successfully created a new contact:', newContact);2728 return newContact;29 } catch (error) {30 console.log(error);31 // Handle the error32 }33});3435/* Promise resolves to:36 * {37 * "revision": 1,38 * "source": {39 * "sourceType": "WIX_CODE",40 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"41 * },42 * "lastActivity": {43 * "activityDate": "2023-12-24T11:50:46.048Z",44 * "activityType": "CONTACT_CREATED"45 * },46 * "primaryInfo": {47 * "email": "johndoe1@example.com",48 * "phone": "6465676359"49 * },50 * "info": {51 * "name": {52 * "first": "John",53 * "last": "Doe"54 * },55 * "emails": {56 * "items": [57 * {58 * "tag": "UNTAGGED",59 * "email": "johndoe1@example.com",60 * "primary": true,61 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"62 * }63 * ]64 * },65 * "phones": {},66 * "addresses": {},67 * "extendedFields": {68 * "items": {69 * "contacts.displayByLastName": "Doe John",70 * "invoices.vatId": "",71 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",72 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",73 * "contacts.displayByFirstName": "John Doe"74 * }75 * }76 * },77 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",78 * "_createdDate": "2023-12-24T11:50:46.049Z",79 * "_updatedDate": "2023-12-24T12:50:46.047Z"80 * }81 */82
Create a new contact, full object with options
Copy Code
1import { Permissions, webMethod } from 'wix-web-module';2import { contacts } from 'wix-crm.v2';3import { elevate } from 'wix-auth';45/* Sample info value:6 * {7 * name: {8 * first: 'John',9 * last: 'Doe'10 * },11 * emails: {12 * items: [13 * {14 * tag: 'UNTAGGED',15 * email: 'johndoe1@example.com',16 * _id: '37db3bde-83c0-4ca2-8097-88964a2f343b'17 * }18 * ]19 * },20 * company: 'Wix',21 * jobTitle: 'Writer',22 * phones: {23 * items: [24 * {25 * phone: '6465676359',26 * countryCode: 'US',27 * primary: 'true',28 * tag: 'MOBILE'29 * }30 * ]31 * },32 * birthdate: '1995-04-25',33 * locale: 'en-us',34 * labelKeys: {35 * items: [36 * 'custom.contact'37 * ]38 * },39 * extendedFields': {40 * items: {41 * custom.nickname: 'Jonny'42 * }43 * },44 * addresses: {45 * items: [46 * {47 * tag: 'HOME',48 * address: {49 * addressLine1: '3 Park Ave',50 * city: 'NY',51 * subdivision: 'US-NY',52 * country: 'US',53 * postalCode: '10010'54 * }55 * ]56 * }57 * }58 * }59 *60 * Sample options value:61 * {62 * allowDuplicates: true63 * }64 */6566export const myCreateContactFunction = webMethod(Permissions.Anyone, async (info, options) => {6768 try {69 const elevatedCreateContact = elevate(contacts.createContact);70 const newContact = await elevatedCreateContact(info, options);71 console.log('Successfully created a new contact:', newContact);7273 return newContact;74 } catch (error) {75 console.log(error);76 // Handle the error77 }78});798081/* Promise resolves to:82 * {83 * "revision": 1,84 * "source": {85 * "sourceType": "WIX_CODE",86 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"87 * },88 * "lastActivity": {89 * "activityDate": "2023-12-24T11:50:46.048Z",90 * "activityType": "CONTACT_CREATED"91 * },92 * "primaryInfo": {93 * "email": "johndoe1@example.com",94 * "phone": "6465676359"95 * },96 * "info": {97 * "name": {98 * "first": "John",99 * "last": "Doe"100 * },101 * "emails": {102 * "items": [103 * {104 * "tag": "UNTAGGED",105 * "email": "johndoe1@example.com",106 * "primary": true,107 * "_id": "37db3bde-83c0-4ca2-8097-88964a2f343b"108 * },109 * ]110 * },111 * "phones": {112 * "items": [113 * {114 * "tag": "MOBILE",115 * "countryCode": "US",116 * "phone": "646-567-6359",117 * "e164Phone": "+16465676359",118 * "primary": true,119 * "_id": "ed1d7da0-b4a1-4164-81b7-c49dedc25dd7"120 * }121 * ]122 * },123 * "addresses": {124 * "items": [125 * {126 * "tag": "HOME",127 * "address": {128 * "formatted": "3 Park Ave\nNY, New York 10010\nUnited States",129 * "addressLine1": "3 Park Ave",130 * "city": "NY",131 * "subdivision": "US-NY",132 * "country": "US",133 * "postalCode": "10010"134 * },135 * "_id": "eeb4e174-fd0f-4ce8-8cac-dc152f284228"136 * }137 * ]138 * },139 * "company": "Wix",140 * "jobTitle": "Writer",141 * "birthdate": "1995-04-25",142 * "locale": "en-us",143 * "labelKeys": {144 * "items": [145 * "custom.contact"146 * ]147 * },148 * "extendedFields": {149 * "items": {150 * "contacts.displayByLastName": "Doe John",151 * "invoices.vatId": "",152 * "emailSubscriptions.deliverabilityStatus": "NOT_SET",153 * "emailSubscriptions.subscriptionStatus": "UNSUBSCRIBED",154 * "contacts.displayByFirstName": "John Doe",155 * "custom.nickname": "Jonny"156 * }157 * }158 * },159 * "_id": "2712ae1d-3f64-46c2-ac3a-94a6c2e29847",160 * "_createdDate": "2023-12-24T11:50:46.049Z",161 * "_updatedDate": "2023-12-24T12:50:46.047Z"162 * }163 */
Create a new contact with custom labels
This function creates a new contact with the user-defined labels retrieved by a query.
Copy Code
1/**********************************2 * Backend code - contacts.web.js *3 **********************************/45import { Permissions, webMethod } from 'wix-web-module';6import { contacts, labels } from 'wix-crm.v2';7import { elevate } from 'wix-auth';8910export const createContactWithLabels = webMethod(Permissions.Anyone, async (contactInfo) => {1112try {13 const elevatedQueryLabels = elevate(labels.queryLabels);14 const myLabels = await elevatedQueryLabels()15 .eq('labelType', "USER_DEFINED")16 .find();1718 const items = myLabels.items;19 const labelKeys = items.map(label => {20 return label.key21 });2223 contactInfo.labelKeys.items = labelKeys;2425 const elevatedContact = elevate(contacts.createContact);26 const newContact = await elevatedContact(contactInfo);2728 return newContact;2930 } catch (error) {31 console.log(error);32 // Handle the error33 }34});353637/*************38 * Page code *39 *************/4041import { createContactWithLabels } from 'backend/contacts.web'4243// Retrieve contact info when a form's submit button is clicked44export async function submitButton_click(event) {4546 try {47 const contactInfo = {48 name: {49 first: $w('#firstNameInput').value,50 last: $w('#lastNameInput').value,51 },52 emails: {53 items: [54 {55 email: $w('#emailInput').value,56 }57 ]58 },59 labelKeys: {60 items: []61 }62 };6364 // Call backend function to create a contact with labels65 const newContact = await createContactWithLabels(contactInfo);66 console.log('Contact created successfully:', newContact);6768 return contactInfo;69 } catch (error) {70 console.error('Error creating contact:', error);71 // Handle the error72 }73}