# API to create a contact doesn't always work

**URL:** <https://community.hubspot.com/t/api-to-create-a-contact-doesnt-always-work/105923>\
**Category:** APIs & Integrations\
**Tags:** starter\
**Created:** [April 26, 2024, 10:14pm UTC](https://community.hubspot.com/t/api-to-create-a-contact-doesnt-always-work/105923 "2024-04-26T22:14:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![russianmath](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@russianmath](https://community.hubspot.com/u/russianmath)\
**Post date:** [April 26, 2024, 10:14pm UTC](https://community.hubspot.com/t/api-to-create-a-contact-doesnt-always-work/105923/1 "2024-04-26T22:14:22Z")

</div>

Hello, I have this code in PHP, but it doesn’t always create a contact. I checked apache logs and didn’t find any errors:

public static function addContact(array $userRecord)

{

$accessToken = HUBSPOT\_ACCESS\_TOKEN;

// The API URL for creating contacts

$url = ‘[https://api.hubapi.com/crm/v3/objects/contacts](https://api.hubapi.com/crm/v3/objects/contacts)’;

// Data to be sent to the API

$data = [

‘properties’ =\> [

‘email’ =\> $userRecord[‘user\_email’],

‘firstname’ =\> $userRecord[‘user\_first\_name’],

‘lastname’ =\> $userRecord[‘user\_last\_name’],

‘phone’ =\> $userRecord[‘user\_phone’]

]

];

// JSON encode the data

$jsonData = json\_encode($data);

// Create a stream context for file\_get\_contents()

$options = [

‘http’ =\> [

‘method’ =\> ‘POST’,

‘header’ =\> “Authorization: Bearer {$accessToken}\r\n” .

“Content-Type: application/json\r\n”,

‘content’ =\> $jsonData

]

];

$context = stream\_context\_create($options);

// Make the HTTP request

$response = file\_get\_contents($url, false, $context);

// Check for errors

if ($response === FALSE) {

error\_log("Error creating contact in HubSpot: " . $userRecord[‘user\_email’]);

}

}

---

<div class="post-metadata">

**Author:** ![nomijack](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@nomijack](https://community.hubspot.com/u/nomijack)\
**Post date:** [April 27, 2024, 1:12pm UTC](https://community.hubspot.com/t/api-to-create-a-contact-doesnt-always-work/105923/2 "2024-04-27T13:12:10Z")

</div>

Your PHP code attempts to add a contact to HubSpot CRM using its API, but it’s not always successful, despite no errors being logged in the Apache logs. Several factors could contribute to this, including API rate limits, validation errors, network issues, HubSpot configuration, and error handling. To improve reliability, ensure your code addresses these potential issues effectively.

---

<div class="post-metadata">

**Author:** ![russianmath](https://avatars.discourse-cdn.com/v4/letter/r/73ab20/32.png) [@russianmath](https://community.hubspot.com/u/russianmath)\
**Post date:** [April 27, 2024, 2:16pm UTC](https://community.hubspot.com/t/api-to-create-a-contact-doesnt-always-work/105923/3 "2024-04-27T14:16:53Z")

</div>

Hi,  
Thank you for your quick response.  
Would you provide me with the php code or sample app, please. For some reason, you only have the javascript code on the site.
