Getting 403 CloudFront error while integrating contact form with HubSpot API

I’m working on connecting my website’s contact form to HubSpot but I keep getting a 403 error from CloudFront. The error says the distribution doesn’t support the HTTP method I’m using and only allows cachable requests.

Here’s my PHP code that’s causing the issue:

<?php

$contact_data = array(
    'properties' => array(
        array(
            'property' => 'email',
            'value' => '[email protected]'
        ),
        array(
            'property' => 'firstname', 
            'value' => 'John'
        ),
        array(
            'property' => 'lastname',
            'value' => 'Smith'
        ),
        array(
            'property' => 'phone',
            'value' => '555-123-4567'
        )
    )
);

$json_data = json_encode($contact_data);
$api_url = 'https://app.hubspot.com/contacts/v1/contacts?hapikey=' . $api_key;

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_data);
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$errors = curl_error($curl);
curl_close($curl);

echo "Errors: " . $errors;
echo "\nHTTP Code: " . $http_code;
echo "\nAPI Response: " . $result;

?>

I’ve tested this on both HTTPS and HTTP environments but neither works. Has anyone encountered this CloudFront error when working with HubSpot’s contact API? What could be causing this issue?

had the same problem last month. switch to the newer contacts api endpoint - use https://api.hubapi.com/crm/v3/objects/contacts instead of the old v1. also double-check your hapikey is still valid. cloudfront errors usually mean you’re hitting a deprecated endpoint or wrong url.