What is the scope of the WIX REST APIs?

Question:
Can the WIX REST APIs (Contact in particular) only be called from code running within WIX or a WIX app, or can they be called from code on an external web site? If they can, what is the authentification process for such a call?

Product:
WIX REST APIs

What are you trying to achieve:
I am trying to migrate an existing PHP/MySQL based ecommerce web site to WIX and want to know if/how I can write code on the existing WEB site that will copy the customer data across by creating new WIX Contacts.

What have you already tried:
I have read the WIX data API and REST API reference documentation and although this explains how to write the code, it does not seem to explain the wider picture of when and how these functions can be used.
I tried using cURL to call the Contact REST API from a PHP script on my existing site using an API key set up in Headless Settings in the WIX Dashboard Settings but this just returned {“message”:“”,“details”:{}} which I assume is either because you cannot access the REST APIs from outside WIX or I have not setup the auth correctly. I just need to rule out the former before looking into the latter.
I have tried the Contact import function in the WIX Dashboard but this is too primitive and only allows the import of a small set of fields.

Additional information:
I am completely new to WIX and Velo but have been coding PHP for many years and am moderately proficient at Javascript.

OK, I finally found a documentation page that clarified how to authenticate an API without using OAuth or having to create an App.

With the header correctly setup I was then able to make a CURL call from another web site using the WIX Contact API, so the answer to my original question is YES!

So in a nutshell, to access the WIX REST APIs from outside your WIX site…

  1. Create an API Key here making sure you grant permission for the data collections that you want to access (WIX Stores, Wix Contacts & Members, WIX Data etc.).
  2. Copy and save that API key and also the Account Key displayed on that page.
  3. If you don’t have it noted down already, get the site ID of the WIX site you want to access the data from (This is the bit between ‘…wix.com/dashboard/’ and ‘/home’ in the URL of your WIX Dashboard).

Here is a sample PHP call to the WIX Contact REST API that creates a new contact:

		$url = 'https://www.wixapis.com/contacts/v4/contacts';
		$ch = curl_init( $url );
		$authcode = '<insert your API Key here>';
		$siteID = '<insert your site ID here>';
		$headers = [
			'Content-Type: application/json',
			'Authorization: ' . $authcode,
//			'wix-account-id: '.$accountID 		// Not used for this API
			'wix-site-id: '.$siteID
		];

		$payload = '{
		 "info": {
			  "name": {
				"first": "Mickey",
				"last": "Mouse"
			  },
			  "emails": {
				"items": [
				  {
					"tag": "MAIN",
					"email": "mickey@disney.com"
				  }
				]
			  },
			  "phones": {
				"items": [
				  {
					"tag": "MOBILE",
					"countryCode": "GB",
					"phone": "07999 999999"
				  }
				]
			  },
			  "addresses": {
				"items": [
				  {
					"tag": "HOME",
					"address": {
					  "country": "GB",
					  "city": "Liverpool",
					  "postalCode": "L99 9ZZ",
					  "addressLine": "101 Comedy Lane",
					}
				  }
				]
			  },
			  "company": "Disney",
			  "jobTitle": "Actor",
			  "locale": "en-gb",
			  "labelKeys": {
				"items": [
				  "custom.imported"
				]
			  }
			},
		  "allowDuplicates": false
		}';
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
		$result = curl_exec($ch);

Note: you don’t include the site ID in the header of API calls that are not site specific and you don’t include the account ID in calls to APIs that are site specific (like the example above).

I hope this helps some noobies get going with APIs.

2 Likes

Documentation I found was API Keys

Thanks for sharing your solution with us Nick!