wix-crm

wix-crm

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

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

ICAgaW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzs=
   import wixCRM from 'wix-crm';

Contents

createContact( ) Creates a new contact or updates an existing contact.
emailContact( ) Sends a Triggered Email to the contact.
ContactInfo An object that contains information about a site contact.
createContact( )

createContact( )

Creates a new contact or updates an existing contact.

function createContact(contactInfo: ContactInfo): Promise<String>

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.

Parameters

contactInfo ContactInfo The information for the contact being created or updated.

Return Value

Returns a Promise

On fulfillment String The ID of the new or updated contact.
On rejection String Error message.

Examples

Create a new contact

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCi8vIC4uLgoKbGV0IGZpcnN0TmFtZSA9IC8vIGdldCBmaXJzdCBuYW1lCmxldCBsYXN0TmFtZSA9IC8vIGdldCBsYXN0IG5hbWUKbGV0IGVtYWlsID0gLy8gZ2V0IGVtYWlsIGFkZHJlc3MKbGV0IHBob25lID0gLy8gZ2V0IHBob25lIG51bWJlcgoKd2l4Q1JNLmNyZWF0ZUNvbnRhY3QoIHsKICAiZmlyc3ROYW1lIjogZmlyc3ROYW1lLAogICJsYXN0TmFtZSI6IGxhc3ROYW1lLAogICJlbWFpbHMiOiBbZW1haWxdLAogICJwaG9uZXMiOiBbcGhvbmVdCn0gKQoudGhlbiggKGNvbnRhY3RJZCkgPT4gewogIC8vIGNvbnRhY3QgY3JlYXRlZAp9ICk7Cg==
import wixCRM from 'wix-crm';

// ...

let firstName = // get first name
let lastName = // get last name
let email = // get email address
let phone = // get phone number

wixCRM.createContact( {
  "firstName": firstName,
  "lastName": lastName,
  "emails": [email],
  "phones": [phone]
} )
.then( (contactId) => {
  // contact created
} );

Create a new contact with custom fields

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCi8vIC4uLgoKbGV0IGZpcnN0TmFtZSA9IC8vIGdldCBmaXJzdCBuYW1lCmxldCBsYXN0TmFtZSA9IC8vIGdldCBsYXN0IG5hbWUKbGV0IGVtYWlsID0gLy8gZ2V0IGVtYWlsIGFkZHJlc3MKbGV0IHBob25lID0gLy8gZ2V0IHBob25lIG51bWJlcgoKbGV0IGNvbnRhY3RJbmZvID0gewogICJmaXJzdE5hbWUiOiBmaXJzdE5hbWUsCiAgImxhc3ROYW1lIjogbGFzdE5hbWUsCiAgImVtYWlscyI6IFtlbWFpbF0sCiAgInBob25lcyI6IFtwaG9uZV0sCiAgImN1c3RvbUZpZWxkMSI6ICJjdXN0b21WYWx1ZTEiLAogICJjdXN0b21GaWVsZDIiOiAiY3VzdG9tVmFsdWUyIgp9OwoKd2l4Q1JNLmNyZWF0ZUNvbnRhY3QoY29udGFjdEluZm8pCi50aGVuKCAoY29udGFjdElkKSA9PiB7CiAgLy8gY29udGFjdCBjcmVhdGVkCn0gKTsK
import wixCRM from 'wix-crm';

// ...

let firstName = // get first name
let lastName = // get last name
let email = // get email address
let phone = // get phone number

let contactInfo = {
  "firstName": firstName,
  "lastName": lastName,
  "emails": [email],
  "phones": [phone],
  "customField1": "customValue1",
  "customField2": "customValue2"
};

wixCRM.createContact(contactInfo)
.then( (contactId) => {
  // contact created
} );

Create a contact and then send a Triggered Email to the new contact

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCiR3Lm9uUmVhZHkoZnVuY3Rpb24gKCkgewogICR3KCIjbXlCdXR0b24iKS5vbkNsaWNrKCAoKSA9PiB7CiAgICB3aXhDUk0uY3JlYXRlQ29udGFjdCggewogICAgICAiZmlyc3ROYW1lIjogJHcoIiNmaXJzdE5hbWUiKS52YWx1ZSwKICAgICAgImxhc3ROYW1lIjogJHcoIiNsYXN0TmFtZSIpLnZhbHVlLAogICAgICAiZW1haWxzIjogWyR3KCIjZW1haWwiKS52YWx1ZV0sCiAgICAgICJwaG9uZXMiOiBbJHcoIiNwaG9uZSIpLnZhbHVlXQogICAgfSApCiAgICAudGhlbiggKGNvbnRhY3RJZCkgPT4gewogICAgICB3aXhDUk0uZW1haWxDb250YWN0KCJ0aGFua3lvdSIsIGNvbnRhY3RJZCwgewogICAgICAgICJ2YXJpYWJsZXMiOiB7CiAgICAgICAgICAiZmlyc3ROYW1lIjogJHcoIiNmaXJzdE5hbWUiKS52YWx1ZSwKICAgICAgICAgICJsYXN0TmFtZSI6ICR3KCIjbGFzdE5hbWUiKS52YWx1ZQogICAgICAgIH0KICAgICAgfSApOwogICAgfSApOwogIH0gKTsKfSApOwo=
import wixCRM from 'wix-crm';

$w.onReady(function () {
  $w("#myButton").onClick( () => {
    wixCRM.createContact( {
      "firstName": $w("#firstName").value,
      "lastName": $w("#lastName").value,
      "emails": [$w("#email").value],
      "phones": [$w("#phone").value]
    } )
    .then( (contactId) => {
      wixCRM.emailContact("thankyou", contactId, {
        "variables": {
          "firstName": $w("#firstName").value,
          "lastName": $w("#lastName").value
        }
      } );
    } );
  } );
} );
emailContact( )

emailContact( )

Sends a Triggered Email to the contact.

function emailContact(emailId: String, toContact: string, [options: TriggeredEmailOptions]): Promise<void>

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.

Parameters

emailId String The Email ID of the Triggered Email to send.
toContact String The Contact ID.
options (optional) TriggeredEmailOptions Variable values to insert into the email.

Return Value

Returns a Promise

On fulfillment void When the email is sent.
On rejection String Error message.

Examples

Send a Triggered Email to contact

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCi8vIC4uLgoKbGV0IGNvbnRhY3RJZCA9IC8vIGdldCBjb250YWN0IElECgp3aXhDUk0uZW1haWxDb250YWN0KCJlbWFpbElEIiwgY29udGFjdElkKQogIC50aGVuKCAoKSA9PiB7CiAgICBjb25zb2xlLmxvZygiVHJpZ2dlcmVkIGVtYWlsIHNlbnQiKTsKICB9ICkKICAuY2F0Y2goIChlcnIpID0+IHsKICAgIGNvbnNvbGUubG9nKGVycik7CiAgfSApOwo=
import wixCRM from 'wix-crm';

// ...

let contactId = // get contact ID

wixCRM.emailContact("emailID", contactId)
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);
  } );

Send a Triggered Email with variable values

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCi8vIC4uLgoKbGV0IGNvbnRhY3RJZCA9IC8vIGdldCBjb250YWN0IElECmxldCB2YWx1ZTEgPSAvLyB2YWx1ZSBmb3IgdmFyaWFibGUxCgp3aXhDUk0uZW1haWxDb250YWN0KCJlbWFpbElEIiwgY29udGFjdElkLCB7CiAgICAidmFyaWFibGVzIjogewogICAgICAidmFyaWFibGUxIjogdmFsdWUxLAogICAgICAidmFyaWFibGUyIjogInZhbHVlIGZvciB2YXJpYWJsZTIiCiAgICB9CiAgfSApCiAgLnRoZW4oICgpID0+IHsKICAgIGNvbnNvbGUubG9nKCJUcmlnZ2VyZWQgZW1haWwgc2VudCIpOwogIH0gKQogIC5jYXRjaCggKGVycikgPT4gewogICAgY29uc29sZS5sb2coZXJyKTsKICB9ICk7Cg==
import wixCRM from 'wix-crm';

// ...

let contactId = // get contact ID
let value1 = // value for variable1

wixCRM.emailContact("emailID", contactId, {
    "variables": {
      "variable1": value1,
      "variable2": "value for variable2"
    }
  } )
  .then( () => {
    console.log("Triggered email sent");
  } )
  .catch( (err) => {
    console.log(err);
  } );

Create a contact and then send a Triggered Email to the new contact

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCiR3Lm9uUmVhZHkoZnVuY3Rpb24gKCkgewogICR3KCIjbXlCdXR0b24iKS5vbkNsaWNrKCAoKSA9PiB7CiAgICB3aXhDUk0uY3JlYXRlQ29udGFjdCggewogICAgICAiZmlyc3ROYW1lIjogJHcoIiNmaXJzdE5hbWUiKS52YWx1ZSwKICAgICAgImxhc3ROYW1lIjogJHcoIiNsYXN0TmFtZSIpLnZhbHVlLAogICAgICAiZW1haWxzIjogWyR3KCIjZW1haWwiKS52YWx1ZV0sCiAgICAgICJwaG9uZXMiOiBbJHcoIiNwaG9uZSIpLnZhbHVlXQogICAgfSApCiAgICAudGhlbiggKGNvbnRhY3RJZCkgPT4gewogICAgICB3aXhDUk0uZW1haWxDb250YWN0KCJ0aGFua3lvdSIsIGNvbnRhY3RJZCwgewogICAgICAgICJ2YXJpYWJsZXMiOiB7CiAgICAgICAgICAiZmlyc3ROYW1lIjogJHcoIiNmaXJzdE5hbWUiKS52YWx1ZSwKICAgICAgICAgICJsYXN0TmFtZSI6ICR3KCIjbGFzdE5hbWUiKS52YWx1ZQogICAgICAgIH0KICAgICAgfSApOwogICAgfSApOwogIH0gKTsKfSApOwo=
import wixCRM from 'wix-crm';

$w.onReady(function () {
  $w("#myButton").onClick( () => {
    wixCRM.createContact( {
      "firstName": $w("#firstName").value,
      "lastName": $w("#lastName").value,
      "emails": [$w("#email").value],
      "phones": [$w("#phone").value]
    } )
    .then( (contactId) => {
      wixCRM.emailContact("thankyou", contactId, {
        "variables": {
          "firstName": $w("#firstName").value,
          "lastName": $w("#lastName").value
        }
      } );
    } );
  } );
} );
ContactInfo

ContactInfo

An object that contains information about a site contact.

Type

Object

Properties

firstName String Contact's first name.
lastName String Contact's last name.
picture String Contact's image source.
emails 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 String[ ] List of contact's phone numbers.
labels 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

aW1wb3J0IHdpeENSTSBmcm9tICd3aXgtY3JtJzsKCi8vIC4uLgoKbGV0IGZpcnN0TmFtZSA9IC8vIGdldCBmaXJzdCBuYW1lCmxldCBsYXN0TmFtZSA9IC8vIGdldCBsYXN0IG5hbWUKbGV0IGVtYWlsID0gLy8gZ2V0IGVtYWlsIGFkZHJlc3MKbGV0IHBob25lID0gLy8gZ2V0IHBob25lIG51bWJlcgoKbGV0IGNvbnRhY3RJbmZvID0gewogICJmaXJzdE5hbWUiOiBmaXJzdE5hbWUsCiAgImxhc3ROYW1lIjogbGFzdE5hbWUsCiAgImVtYWlscyI6IFtlbWFpbF0sCiAgInBob25lcyI6IFtwaG9uZV0sCiAgImN1c3RvbUZpZWxkMSI6ICJjdXN0b21WYWx1ZTEiLAogICJjdXN0b21GaWVsZDIiOiAiY3VzdG9tVmFsdWUyIgp9OwoKd2l4Q1JNLmNyZWF0ZUNvbnRhY3QoY29udGFjdEluZm8pCi50aGVuKCAoY29udGFjdElkKSA9PiB7CiAgLy8gY29udGFjdCBjcmVhdGVkCn0gKTsK
import wixCRM from 'wix-crm';

// ...

let firstName = // get first name
let lastName = // get last name
let email = // get email address
let phone = // get phone number

let contactInfo = {
  "firstName": firstName,
  "lastName": lastName,
  "emails": [email],
  "phones": [phone],
  "customField1": "customValue1",
  "customField2": "customValue2"
};

wixCRM.createContact(contactInfo)
.then( (contactId) => {
  // contact created
} );