APIs & Integrations

charlesmiles85
Member

V3 API - Adding Note: hs_timestamp VALIDATION ERROR

SOLVE

I've been attempting to use the HubSpot V3 API PHP library created by HubSpot off gitHub, to add a note to a contact record (I've already been able to add a contact successfully).

 

However, I'm getting a "VALIDATION ERROR" for the "hs_timestamp" required parameter.

 

I've read just about all the posts I could find, and the HubSpot docs, about the UTC or UNIX milliseconds datetime variations that need to be sumbmitted to any datetime field in the API.

 

However, I still get the same validation error - so I'm stumped.

 

Here's the basic code I've tried:

 

### CODE ###

//UTC Time
$dateTime = new DateTime();
$dateTime->setTimeZone(new DateTimeZone('America/Chicago'));
$timeStamp = $dateTime->format("Y-m-d\TH:i:s\Z");
echo "<br>";
echo "<br>";
print_r($timeStamp);
//END - UTC Time

 

//Milliseconds Time
$timeStamp = $dateTime->getTimestamp()*1000;
//END - Milliseconds Time

 

$noteProperties = [
"hs_timestamp" => $timeStamp,
"hs_note_body" => "This is a test note...",
"hubspot_owner_id" => $ownerID
];

 

$noteObject = new SimplePublicObjectInput(['properties' => $noteProperties]);

 

try
{

$createNote = $hubClient->crm()->objects()->notes()->basicApi()->create($noteObject);

echo "<br>";
echo "<br>";
var_dump($createNote);
}
catch (HubSpot\Client\Crm\Objects\Notes\ApiException $e)
{
echo "Exception for basicApi-create(note) ", $e->getResponseBody();
}

### END CODE ###

 

 

I've also tried quoting my timeStamp variable in the 'properties' array when doing the millisecond method in case the API wanted a string and not an integer - same error.

 

The noteObject I'm passing into the simple input object echo-out looks like this:

 

noteObject: { "properties": { "hs_timestamp": "2022-08-06T20:20:21Z", "hs_note_body": "This is a test note...", "hubspot_owner_id": 209559322 } }​

 

Any ideas, as to what I'm doing wrong, am I using the library wrong?

 

Thanks,

Charles

1 Accepted solution
charlesmiles85
Solution
Member

V3 API - Adding Note: hs_timestamp VALIDATION ERROR

SOLVE

After some other API calls to the provided HubSpot library being "noisy" when my script was placed on a webserver and caused it to return information and freeze the rest of the script, I elected to abandon all usage of the HubSpot library and just use CURL for all my calls.

 

In the case of my above post about adding a note and the "hs_timestamp" validation issue, I thought I would post my basic code that I used to execute adding a note and associating a note successfully using CURL.

 

Be advised that while testing many "test notes" may get created and if you don't associate them they kind of sit on your app, you can use the API references "endpoint" sections to do "test posts" to list all notes, and then use all the ID's to delete the ones you don't want etc. Of course if you use a development account this isn't really an issue - I used an production account that isn't in use yet.

 

So on to the code...some of it is abbreviated, ommitted or changed, in favor of simplicity or privacy:

 

### Create The Note Code ###

 

//Get the current time in millliseconds for 'hs_timestamp'

$timeStamp = intval(microtime(true)*1000);

 

$noteBody = $noteBody = "Your Text Here...." .
"<p> Item 1 => " . $varItem1 .
"<p> Item 2 => " . $varItem2;

 

$noteFields = array(
'hs_timestamp' => $timeStamp,
'hs_note_body' => $noteBody,
);

 

//Create Note Properties Object For CURL

$noteProperties = array( 'properties' => $noteFields);
$noteObject = json_encode($noteProperties);

 

//Create Note
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/notes",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $noteObject,
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer $accessToken",
"content-type: application/json"),
));

 

$createNoteResponse = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

 

if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
$createNoteResponseJson = json_decode($createNoteResponse, true);
$noteID = $createNoteResponseJson['id'];
}
}

### END - Create The Note Code

 

 

### Associate The Note Code ###

 

//Associate Note

$curl = curl_init();

 

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/notes/" . $noteID . "/associations/contacts/" . $contactID . "/note_to_contact",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer $accessToken",
"content-type: application/json"
),
));

 

$assocNoteResponse = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

 

if ($err) {
echo "cURL Error #:" . $err;
} else {
$assocNoteResponseJson = json_decode($assocNoteResponse, true);
}
}

### END - Create The Note Code

 

I hope this code helps someone else.

 

View solution in original post

0 Upvotes
3 Replies 3
charlesmiles85
Solution
Member

V3 API - Adding Note: hs_timestamp VALIDATION ERROR

SOLVE

After some other API calls to the provided HubSpot library being "noisy" when my script was placed on a webserver and caused it to return information and freeze the rest of the script, I elected to abandon all usage of the HubSpot library and just use CURL for all my calls.

 

In the case of my above post about adding a note and the "hs_timestamp" validation issue, I thought I would post my basic code that I used to execute adding a note and associating a note successfully using CURL.

 

Be advised that while testing many "test notes" may get created and if you don't associate them they kind of sit on your app, you can use the API references "endpoint" sections to do "test posts" to list all notes, and then use all the ID's to delete the ones you don't want etc. Of course if you use a development account this isn't really an issue - I used an production account that isn't in use yet.

 

So on to the code...some of it is abbreviated, ommitted or changed, in favor of simplicity or privacy:

 

### Create The Note Code ###

 

//Get the current time in millliseconds for 'hs_timestamp'

$timeStamp = intval(microtime(true)*1000);

 

$noteBody = $noteBody = "Your Text Here...." .
"<p> Item 1 => " . $varItem1 .
"<p> Item 2 => " . $varItem2;

 

$noteFields = array(
'hs_timestamp' => $timeStamp,
'hs_note_body' => $noteBody,
);

 

//Create Note Properties Object For CURL

$noteProperties = array( 'properties' => $noteFields);
$noteObject = json_encode($noteProperties);

 

//Create Note
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/notes",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $noteObject,
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer $accessToken",
"content-type: application/json"),
));

 

$createNoteResponse = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

 

if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
$createNoteResponseJson = json_decode($createNoteResponse, true);
$noteID = $createNoteResponseJson['id'];
}
}

### END - Create The Note Code

 

 

### Associate The Note Code ###

 

//Associate Note

$curl = curl_init();

 

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.hubapi.com/crm/v3/objects/notes/" . $noteID . "/associations/contacts/" . $contactID . "/note_to_contact",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => false,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Bearer $accessToken",
"content-type: application/json"
),
));

 

$assocNoteResponse = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

 

if ($err) {
echo "cURL Error #:" . $err;
} else {
$assocNoteResponseJson = json_decode($assocNoteResponse, true);
}
}

### END - Create The Note Code

 

I hope this code helps someone else.

 

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

V3 API - Adding Note: hs_timestamp VALIDATION ERROR

SOLVE

Hi, @charlesmiles85 👋 Welcome to the community! Hey, @tjoyce, do you have any tips for @charlesmiles85?

 

Thank you! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
charlesmiles85
Member

V3 API - Adding Note: hs_timestamp VALIDATION ERROR

SOLVE

Hi Jaycee,

 

Thanks for the welcome, looking forward to any insights the community has.

 

-Charles

0 Upvotes