Mautic get user id by email

I had trouble finding information on how to retrieve a contact id by email address from API.

Here is the code I am using:

require __DIR__ . '/vendor/autoload.php';

use Curl\Curl;

$curl = new Curl();

$un = 'your_username';
$pw = 'your_password';
$hash = base64_encode($un.':'.$pw);

$curl->setUserAgent('MyUserAgent/0.0.1 (+https://www.fwefe.com)');
$curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
$curl->setHeader('Authorization','Basic '.$hash);

$curl->get(
	'https://mautic.domain.com/api/contacts',
	[
		'search' => 'email:email@domain.com',
		'start'=> 0,
		'limit'=> 1,
		'orderBy'=> 'email',
		'minimal' => false
	]
);

// convert object to array
$array = json_decode(json_encode($curl->response), true);

// get ID contact
print_r(array_keys($array['contacts'])[0]);

I hope it will help other user.

1 Like

What is the response whey you execute the script

You get the user ID corresponding to the email address passed as a parameter to the API of your Mautic installation.

Regards

2 Likes