CorvidReference

wix-crm-backend

The wix-crm-backend module contains functionality for working with your site's contacts from backend code.

To use the CRM API, import wixCrm from the wix-crm-backend module:

   import wixCrm from 'wix-crm-backend';

Table of Contents

OBJECTS

?
Objects used when setting, getting, or calling the properties and methods listed above.
ContactInfoAn object that contains information about a site contact.
DeleteOptionsAn object that contains contact deletion options.

createContact( )

Creates a new contact or updates an existing contact.

Description

The createContact() function returns a Promise that resolves to the newly created or updated contact's ID when the contact has been created or updated.

The passed ContactInfo object must contain at least one email address or phone number.

Calling createContact() performs one of the following. (The contact information specified in the contactInfo parameter matches an existing contact if it contains an email address or phone number from an existing contact.)

  • If there is no matching existing contact, a new contact is created using the information specified using the contactInfo parameter.
  • If there is a matching existing contact, it is updated with the information specified using the contactInfo parameter. Any existing contact information that is not explicity overriden in the contactInfo parameter retains its existing value.

Note

You cannot use the createContact() function to update contact information for existing site members. To do so, use updateContact.

Syntax

function createContact(contactInfo: ContactInfo): Promise<string>
PARAMETERS
?
Values that you pass to a function.
contactInfo
The information for the contact being created or updated.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<string>

Fulfilled - The ID of the new or updated contact. Rejected - Error message.

Examples

Create a new contact

This example contains a backend function that creates a new contact and returns the new contact's ID to the code that called the function.

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(firstName, lastName, email, phone) {
  return wixCrm.createContact( {
    "firstName": firstName,
    "lastName": lastName,
    "emails": [email],
    "phones": [phone]
  } );
}

deleteContact( )

Deletes an existing contact.

Description

The deleteContact() function returns a Promise that resolves when the contact with the given ID is deleted.

Syntax

function deleteContact(contactId: string, options: DeleteOptions): Promise<void>
PARAMETERS
?
Values that you pass to a function.
contactId
string
The ID of the contact to delete.
options
Options to use when deleting the contact.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>

Fulfilled - When the contact is deleted. Rejected - Error message.

Examples

Delete a contact

import wixCrm from 'wix-crm-backend';

export function deleteContact(contactId) {
  wixCrm.deleteContact(contactId)
    .then( () => {
      // contact has been deleted
    })
    .catch( (err) => {
      // there was an error deleting the contact
    });
}

Force the deletion of a contact

import wixCrm from 'wix-crm-backend';

export function deleteContactForcefully(contactId) {
  const options = {
    "deleteMembers": true
  };
  
  wixCrm.deleteContact(contactId, options)
    .then( () => {
      // contact has been deleted
    })
    .catch( (err) => {
      // there was an error deleting the contact
    });
}

emailContact( )

Sends a Triggered Email to a contact.

Description

To learn more about Triggered Emails, see:

Before using the emailContact() function, you need to set up at least one Triggered Email.

Specify which email to send by passing the email's ID in the emailId parameter.

Specify which contact the email is sent to by passing the contact's ID in the toContact parameter.

If the specified Triggered Email contains variables, you can pass values for those variables using the optional options parameter. You pass a TriggeredEmailOptions object which contains the values to be inserted into your email in place of the variables defined in that email. The values passed must be strings. If the object you pass to the options parameter does not contain a key:value pair for a variable in your Triggered Email, the fallback value defined when creating your Triggered Email is inserted in place of the variable.

Note that Triggered Emails generates a code snippet for each of your email templates. The generated code includes the email's ID and keys for all the email's variable names. You can copy and paste the snippet into your code. Then, you need to define values for the toContact property and for each variable key. To learn how to use the generated snippet in your code, see How to Send a Triggered Email with Code.

Syntax

function emailContact(emailId: string, toContact: string, [options: TriggeredEmailOptions]): Promise<void>
PARAMETERS
?
Values that you pass to a function.
emailId
string
The Email ID of the Triggered Email to send.
toContact
string
The Contact ID.
options(Optional)
Variable values to insert into the email.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>

Fulfilled - When the email is sent. Rejected - Error message.

Examples

Send a Triggered Email to a contact

import wixCrm from 'wix-crm-backend';

export function myBackendFunction() {
  let contactId = // get contact ID

  wixCrm.emailContact("emailID", contactId)
    .then( () => {
      // email has been sent
    } )
    .catch( (err) => {
      // there was an error sending the email
    } );
}

Send a Triggered Email with variable values

import wixCrm from 'wix-crm-backend';

export function myBackendFunction() {
  let contactId = // get contact ID
  let value1 = // value for variable1

  wixCrm.emailContact("emailID", contactId, {
      "variables": {
        "variable1": value1,
        "variable2": "value for variable2"
      }
    } )
    .then( () => {
      // email has been sent
    } )
    .catch( (err) => {
      // there was an error sending the email
  } );
}

getContactById( )

Gets an existing contact by ID.

Description

The getContactById() function returns a Promise that resolves to a contact whose ID matches the given ID.

Syntax

function getContactById(contactId: string): Promise<ContactInfo>
PARAMETERS
?
Values that you pass to a function.
contactId
string
The ID of the contact to get.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<ContactInfo>

Fulfilled - The given contact's information. Rejected - Error message.

Examples

Get a contact by ID

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(contactId) {
  return wixCrm.getContactById(contactId);
}

updateContact( )

Updates an existing contact.

Description

The updateContact() function returns a Promise that resolves when the contact with the specified ID has been updated.

Only the properties passed in the ContactInfo object will be updated. All other properties will remain the same.

Syntax

function updateContact(contactId: string, contactInfo: ContactInfo): Promise<void>
PARAMETERS
?
Values that you pass to a function.
contactId
string
The ID of the contact to update.
contactInfo
The information to update.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>

Fulfilled - When the contact is updated. Rejected - Error message.

Examples

Update an existing contact

This example contains a backend function that updates a contact.
import wixCrm from 'wix-crm-backend';

export function myBackendFunction(contactId, firstName, lastName, email, phone) {
  wixCrm.updateContact(contactId, {
      "firstName": firstName,
      "lastName": lastName,
      "emails": [email],
      "phones": [phone]
  } )
    .then( () => {
      // contact has been updated
    } )
    .catch( (err) => {
      // there was an error updating the contact
    } );
}

ContactInfo

An object that contains information about a site contact.

Syntax

type ContactInfo = {
  firstName: string
  lastName: string
  picture: string
  emails: Array<string>
  loginEmail: string
  phones: Array<string>
  labels: Array<string>
  language: string
  customFields: string | number | Date
}
MEMBERS
?
The properties of an object.
firstName
string
Contact's first name.
lastName
string
Contact's last name.
picture
string
Contact's image source.
emails
Array<string>
List of contact's email addresses.
loginEmail
string

Email address the contact who is also a member uses to log into the system.

phones
Array<string>
List of contact's phone numbers.
labels
Array<string>

List of contact's labels. Labels are used to organize contacts. When setting the labels property, you can only list labels that already exist in your site's Contacts List.

language
string
Contact's language.
customFields
string | number | Date

Any number of custom fields. Customs fields are used to store additional information about your site's contacts. When setting a custom field, use key:value pairs where the key matches the names defined in your site's Contacts List. You can only set values for custom fields that already exist in the Contacts application.

Examples

Create a new contact with custom fields

import wixCrm from 'wix-crm-backend';

export function myBackendFunction(firstName, lastName, email, phone) {
  return wixCrm.createContact( {
    "firstName": firstName,
    "lastName": lastName,
    "emails": [email],
    "phones": [phone]
  } );
}

DeleteOptions

An object that contains contact deletion options.

Syntax

type DeleteOptions = {
  deleteMembers: boolean
}
MEMBERS
?
The properties of an object.
deleteMembers
boolean
Whether to perform the deletion when the contact is also a member. Defaults to false.

Examples

Force the deletion of a contact

import wixCrm from 'wix-crm-backend';

export function deleteContactForcefully(contactId) {
  const options = {
    "deleteMembers": true
  };
  
  wixCrm.deleteContact(contactId, options)
    .then( () => {
      // contact has been deleted
    })
    .catch( (err) => {
      // there was an error deleting the contact
    });
}