How to add an email to a contact module using the v8 API?

This is my code I’m running it using XAMPP

I get the idea from here: API v8: adding account email addresses

$email_id = $decoded_email->data->id;

$ch = curl_init();

    $postStr = json_encode(array(
    'data' => array(
					"type" => "Contacts",
					"attributes" => array(
										"first_name" => "test",
										"last_name" => "test last test",
									),
					"relationships" => array(
										"email_addresses" => array(
																"data" => array(
																			array(
																				"id" => $email_id,
																				"type" => "EmailAddress"
																			)
																		)
															)
									)
				)
));
$url = 'http://localhost/suitecrm/Api/V8/module';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$output = curl_exec($ch);

I’m able to create the email address record, I’m just having problems connecting that to the contact module.

When I run the code above I get this error:

{
    "errors": {
        "status": 400,
        "title": null,
        "detail": "The option \"relationships\" does not exist. Defined options are: \"attributes\", \"id\", \"type\"."
    }
}

The documentation for the V8 API is severely lacking on any information on how to get this done so I posted it here instead.

and Yes everything must be done via the V8 API since we have a different system that will feed this information to Suite.

######################################

Found the create relationship code

$ch = curl_init();

$postStr = json_encode(
	array(
		'data' => array(
						"type" => "EmailAddress",
						"id" => "e9610c47-bdd9-6a3a-bae7-5f4c9078b1bf"
					)
	)
);

$url = 'http://localhost/suitecrm/Api/V8/modules/Contacts/60f801b8-de80-aa6c-d171-5f4ca51928c9/relationships';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$output = curl_exec($ch);

This is returning a {“message”:“Not found”} error

Hi,
there is a method specifically for setting relationships:

if you need more descriptions like that, have a look at Api\docs\swagger\swagger.json.

I found that one but I’m getting a {“message”:“Not found”} error. I’ll edit my post to include the new relationship code

nvm I added an extra “s” in the target URL.

For anyone interested this worked in adding an email to a contact module:

$ch = curl_init();

$postStr = json_encode(
	array(
		'data' => array(
						"type" => "EmailAddress",
						"id" => "e9610c47-bdd9-6a3a-bae7-5f4c9078b1bf"
					)
	)
);

// echo $postStr;

$url = 'http://localhost/suitecrm/Api/V8/module/Contacts/60f801b8-de80-aa6c-d171-5f4ca51928c9/relationships';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$output = curl_exec($ch);