# Webhooks - authentication type Api key - secret name and secret value - in PHP

**URL:** <https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740>\
**Category:** APIs & Integrations\
**Tags:** php, developer-tools, webhooks\
**Created:** [April 5, 2023, 8:14am UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740 "2023-04-05T08:14:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SFlorina](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SFlorina](https://community.hubspot.com/u/SFlorina)\
**Post date:** [April 5, 2023, 8:14am UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740/1 "2023-04-05T08:14:05Z")

</div>

Hello.

I am trying to authenticate a webhook using api key (secret name and secret value). So I have made two files:

webhook.php :

\<?php

include(‘webhook-api-key.php’);

// Retrieve the request body from the webhook POST request

if ($http\_status\_code === 200){

$request\_body = file\_get\_contents(‘php://input’);

// Convert the request body from JSON to a PHP object

$request\_data = json\_decode($request\_body);

// Extract the contact properties from the request data

$contact\_properties = $request\_data-\>properties;

// Extract the email property value

$email = $contact\_properties-\>email-\>value;

// Extract the first name property value

$first\_name = $contact\_properties-\>firstname-\>value;

// Extract the last name property value

$last\_name = $contact\_properties-\>lastname-\>value;

// Do something with the contact data, such as adding it to a database or sending an email notification

// For example:

$contact\_data = array(

‘email’ =\> $email,

‘first\_name’ =\> $first\_name,

‘last\_name’ =\> $last\_name

);

// Add the contact data to a database or send an email notification, etc.

// Send a HTTP response to HubSpot indicating that the webhook was successfully received and processed

http\_response\_code(200);

}

?\>

and webhook-api-key.php:

\<?php

$endpoint\_url = ‘https:/…/hubspot/webhook.php’;

// Set up the API key secret name and secret value

$api\_key\_secret\_name = ‘word’;

$api\_key\_secret\_value = ‘anther\_word’;

// Set up the HTTP POST request headers

$headers = array(

‘Content-Type: application/json’,

'Authorization: Bearer '.$api\_key\_secret\_value

);

// Set up the HTTP POST request body

$body = array(

‘api\_key’ =\> $api\_key\_secret\_value

);

// Send the HTTP POST request to the webhook endpoint URL

$ch = curl\_init($endpoint\_url);

curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true);

curl\_setopt($ch, CURLOPT\_POST, true);

curl\_setopt($ch, CURLOPT\_HTTPHEADER, $headers);

curl\_setopt($ch, CURLOPT\_POSTFIELDS, json\_encode($body));

$response = curl\_exec($ch);

// Check for errors

if(curl\_errno($ch)) {

$error\_message = curl\_error($ch);

echo 'Error: '.$error\_message;

}

// Get the HTTP response status code

$http\_status\_code = curl\_getinfo($ch, CURLINFO\_HTTP\_CODE);

// Close the HTTP POST request

curl\_close($ch);

// Handle the webhook response

if ($http\_status\_code === 200) {

echo ‘Webhook successfully authenticated.’;

} else {

echo 'Webhook authentication failed with HTTP status code: ’ . $http\_status\_code;

}

?\>

And in Hubspot configuration, the url is ‘https:/…/hubspot/webhook.php’.

Is it ok this way ? I am asking because it killed my server when I tried to test it, and I cannot find examples on the internet using this kind of authetication.

Thank you!

---

<div class="post-metadata">

**Author:** ![TDwebdev](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tdwebdev/32/78032_2.png) [@TDwebdev](https://community.hubspot.com/u/TDwebdev)\
**Post date:** [April 5, 2023, 2:24pm UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740/2 "2023-04-05T14:24:08Z")

</div>

Hi Sflorina,

You can remove the API key and only use the Bearer AUTH.

---

<div class="post-metadata">

**Author:** ![SFlorina](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SFlorina](https://community.hubspot.com/u/SFlorina)\
**Post date:** [April 5, 2023, 2:51pm UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740/3 "2023-04-05T14:51:36Z")

</div>

I understand.

But the files are ok ? The endpoint\_url ?

The files` content, the way is included the endpoint\_url, and how i divided the code ?

$ch = curl\_init($endpoint\_url); - is it ok to have this url here, and then to include the file webhook.php in the webhook-api-key.php ?

Thank you!

---

<div class="post-metadata">

**Author:** ![SFlorina](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SFlorina](https://community.hubspot.com/u/SFlorina)\
**Post date:** [April 6, 2023, 9:32am UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740/4 "2023-04-06T09:32:04Z")

</div>

I realized my code is not ok, it is not ok how I check the autenticity of the secret name and value. Here is my updated code:

\<?php

$expectedSecretName = ‘word’; // Replace with your expected secret name

$expectedSecretValue = ‘another\_word’; // Replace with your expected secret value

$requestBody = file\_get\_contents(‘php://input’);

$data = json\_decode($requestBody, true);

if (isset($data[‘secretName’]) && isset($data[‘secretValue’])) {

$actualSecretName = $data[‘secretName’];

$actualSecretValue = $data[‘secretValue’];

if ($actualSecretName === $expectedSecretName && $actualSecretValue === $expectedSecretValue) {

$contact\_properties = $request\_data-\>properties;

$email = $contact\_properties-\>email-\>value;

$firstname = $contact\_properties-\>firstname-\>value;

$lastname = $contact\_properties-\>lastname-\>value;

echo ‘Secret name and value are proper’;

} else {

echo ‘Invalid secret name or value’;

}

}

?\>

Is this the correct way to check the secret name/value ?

Thank you!

---

<div class="post-metadata">

**Author:** ![SFlorina](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@SFlorina](https://community.hubspot.com/u/SFlorina)\
**Post date:** [April 7, 2023, 2:05pm UTC](https://community.hubspot.com/t/webhooks-authentication-type-api-key-secret-name-and-secret-value-in-php/78740/5 "2023-04-07T14:05:58Z")

</div>

Hello. So it is actually quite simple. There are no examples on the internet, and the documentation is poor, it explains more about Hubspot signature than API key.  
I understood eventually how it works, and here is the working code:

$expectedSecretName = ‘word’; // Replace with your expected secret name  
$expectedSecretValue = ‘another\_word’; // Replace with your expected secret value  
$requestBody = file\_get\_contents(‘php://input’);  
$data = json\_decode($requestBody);  
if($\_SERVER[‘HTTP\_WORD’] == $expectedSecretValue){  
//do something with values  
$email = $data-\>email;  
$firstname= $data-\>firstname;  
$lastname= $data-\>lastname;  
}  
else{  
//not from Hubspot  
}
