CodeAPI

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

FUNCTIONS

?
Perform actions on an object.
createContact( )Creates a new contact or updates an existing contact.
emailContact( )Sends a Triggered Email to a contact.

OBJECTS

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

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. You can use use it to update other site contacts.

Syntax

function createContact(contactInfo: ContactInfo): Promise<string>
PARAMETERS
?
The kind of data the property stores.
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]
  } );
}

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
?
The kind of data the property stores.
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 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
  } );
}

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 kind of data the property stores.
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]
  } );
}