Upsert – Create or update - Heyloyalty/api GitHub Wiki

If you wish to create and/or update members on your list in a single action, you can do so using our upsert function.

The function takes a POST event To use the function, you need to call /lists/[listId]/members/upsert The full URL would look like this https://api.heyloyalty.com/loyalty/v1/lists/[listId]/members/upsert

The function takes a standard json object, with a "unique_field" as the key for locating a possible existing member. Besides the unique_field, the functions needs a "member" object as the data for the member.

Example

Below is a POST call made to a specific list

{
	"unique_field": "mobile",
	"member": {
		"firstname": "James",
		"lastname": "Heyloyalty",
		"mobile": "121314151313",
		"email": "[email protected]",
		"birthdate": "1982-05-01",
		"address": "Jens Baggesens vej 47",
		"postalcode": "8200",
		"city": "Århus N",
		"shop": "1464",
		"sex": "1",
		"country": "53",
		"single_choice_fieldtest": "31995",
		"Yes_no": "1",
		"mulitple_choice_fieldtest": ["44738","44739"]
	}
}

In the above example, the email will be used as the unique identifier to update the member with [email protected] as his email. If no such member exist, it will be created. The POST should be sent as raw JSON.

Response on create

{
	"type": "create",
	"member_id": "[memberId]"
}

Response on update

{
	"type": "update",
	"member_id": "[memberId]"
}

When using opt-in

If your list is using opt-in (meaning members need to accept the sign-up to the list), you can add a key-value pair, that will search through temporary members as well and update those if no active member is found. If omitted, the action will not look through temporary members

add the key-value "search_temporary": true, to the existence json, before the member-key

If you wish to skip the optin process and have the member create immediately, you can do that as well, using the key-value "skipOptIn", with the value 1. If omitted, the member will be created with an optin handle (and an optin-automation will be sent, if set up)

{
	"unique_field": "mobile",
	"search_temporary": true,
            "skipOptIn": 1,
	"member": {
		"firstname": "James",
		"lastname": "Heyloyalty",
		"mobile": "121314151313",
		"email": "[email protected]",
		"birthdate": "1982-05-01",
		"address": "Jens Baggesens vej 47",
		"postalcode": "8200",
		"city": "Århus N",
		"shop": "1464",
		"sex": "1",
		"country": "53",
		"single_choice_fieldtest": "31995",
		"Yes_no": "1",
		"mulitple_choice_fieldtest": ["44738","44739"]
	}
} 

Only the fields sent using the upsert, will be created or updated on the member. If more fields are available on the list, these will be untouched.

NOTES

  • You can use any one of the fields on your the list as the identifier. If the unique_field isn’t unique, you will receive an error status and no members will be updated.

  • If you send an object with both mobile and email and a member exists with data matching your current object, but the unique_field does not match the member, you will receive a status 400 and a message saying “member_exists”.

  • The maximum request size when using this function is 5mb.