Error creating contact within HTTP Function

Hi there, I’m new to Wix, so I’m sure I’m missing something simple. I’m trying to create/update contacts from a remote source using HTTP Functions, but I’m getting an error at the first hurdle and I can’t see what I’m doing wrong. Can anyone please help me with the following?

No matter what I do I’m getting an empty error object and no created/updated user:

{“error”:{}}

Thanks in advance

import {ok, serverError} from ‘wix-http-functions’;
import wixCRM from ‘wix-crm-backend’;

export async function get_makeme(request) {
let options = { “headers”: { “Content-Type”: “application/json” } };

try {
let result = await wixCRM.createContact( {
“firstName”: ‘Firstname’,
“lastName”: ‘Lastname’,
“emails”: [‘me@mydomain.com’],
“phones”: [‘+61123456789’]
});
options.body = { “result”: result };
return ok(options);
} catch (error) {
options.body = { “error”: error };
return serverError(options);
}
}

Sorry, just noticed I wasn’t setting the error response to the error message. The catch block should have contained:

options.body = { “error”: error.message };

The error message from the above function is:

{
“error”: “server responded with "\"value\" required in setHeader(\"Authorization\", value)"”
}

This would seem to imply that my function is being knocked back due to the request not being authorised. I thought the idea of wix-crm-backend was that it could be called from backend code (including http functions) without a current session?

So if I set an Authorisation header in my request I get:

{
“error”: “server responded with 400 - {"message":"missing_context, details: {\"fields\":\"MSID / (appDefId, instanceId)\",\"apps\":\"[]\",\"entities\":\"[]\"}","details":{"entities":"[]","error":"missing_context","fields":"MSID / (appDefId, instanceId)","category":"api_error","apps":"[]"}}”
}

What is Wix expecting in the authorisation header? Can I obtain an API key from somewhere? Out of curiosity I tried a basic auth value for Authorization with my login credentials but that didn’t work.

Any help would be greatly appreciated.

Regards.

Just further to my last update, I’ve also tried to implement wixCRM.createContact as part of a database onInsert Hook and I am getting the same error. Is anyone able to call wixCRM.createContact (from wix-crm-backend)?

Thanks again,

Andrew

Hi Andrew,

There is an issue indeed with invoking CRM backend from the HTTP functions - we are looking into it and hoping to have a fix for this soon.

As for the data hook usage - it should work fine. For example, in the scenario, where you creating a custom registration form and use the beforeInsert/afterInsert hook to create a contact using wix-crm-backend .
Note , that it could not be really working in the Preview mode - only in the live (published) site mode.

This is the simple hook code (assuming the Collection has the used fields):

import wixCRM from 'wix-crm-backend';

export function MyContacts_afterInsert(item, context) {
  return wixCRM.createContact( {
    "firstName": item.name,
    "lastName": item.lastName,
    "emails": [item.email],
    "phones": [item.phone]
   } );
}


Please share your hook’s code and the error you get if you still have a problem.

Thanks Alexander, here is the function I’m using as a data hook:

import wixCRM from ‘wix-crm-backend’;
export async function Members_beforeInsert(item, context) {
item.lastname = item.lastname.toUpperCase();
try {
let result = await wixCRM.createContact( {
“firstName”: item.firstname,
“lastName”: item.lastname,
“emails”: [item.email]
});
console.log(result);
return item;
} catch (error) {
console.log(error.message);
item.error = error.message;
return item;
}
}

Nothing appears in the contact list and I receive this notice in the error column/field I added to items in the “catch” above.

server responded with “"value" required in setHeader("Authorization", value)”

BTW, how do I insert formatted code like you have done? Makes it much easier to read.

Thanks again,

Andrew

Hi Andrew,

Your code is valid. What is the flow of inserting data into the Collection? Via a form on the site? (Could you share a link to it?)

You can format code in the message by putting the code, selecting it (you will get a small popup with formatting options) and choosing the rightmost one:

Alexander

Also, good news - we deployed the fix for the HTTP functions flow - it should work now!

Hi Alexander, I tried calling the hook via 2 methods, the first was by editing a row in the published database (Dashboard → Database → Members → add item).

The second way I tried (that resulted in the error I mentioned) was to use a HTTP function to insert a record in the Member database (I was trying to see if I could use a database as a proxy for inserting into the contacts list).

When I edit a row in the database dashboard as mentioned, when are insert and update hooks fired? First cell, each cell, final cell?

Just checked the ability to call createContact from a http function and it’s working now, thanks for fixing this so quickly!

Just lastly, do you have an eta for when we can read contacts from wix-crm-background? At the moment I need to keep a canonical record in a database collection which I update or let users update and blindly blast the one in contacts when it changes. This means I need to replicate my own My Account page and Paid Membership subscriptions because I need to ensure that the user doesn’t change their contacts record directly so that I can safely blast it when other parts of the system need to change things (like incrementing a counter field “CoursesCompleted” when they complete a module in a 3rd party LMS or if I wanted to add a label to a contact without removing any existing labels).

If I could read and write contacts I wouldn’t need to maintain a separate members database (I could read/write the contacts record directly) and I could use the wix inbuilt My Account page and Membership Subscriptions.

Hi Andrew,

So the second way had suffered from the same HTTP functions issue (meaning it has the same effect as calling CRM’s createContact directly from the HTTP function).

Regarding inserting data via the dashboard - as of now, a new record inserted immediately after adding it (empty row), then it is updated after putting data - for each record. (It might change though in future.)

As for the read feature in CRM - you are welcome to submit a Feature Request in the requests’s section .

Alexander