APIs & Integrations

Kristaf
Member

Script fails to submit to hubspot contacts with form API

Am trying to submit sample data to hubspot contacts using custom form fields and hubspot form API but it's failing. Need some help where am going wrong. This is my javascript:

function formv3(){
    let xhr = new XMLHttpRequest();
    let url = 'https://api.hsforms.com/submissions/v3/integration/submit/62515/b68a1eb2-9c96-4483-baec-815482d30083';

    // Example request JSON:
    let data = {
        "fields": [
            {
                "name": "email",
                "value": "example@example.com"
            },
            {
                "name": "firstname",
                "value": "Jeff"
            }
        ]
    };

    let final_data = JSON.stringify(data);

    xhr.open('POST', url);
    // Sets the value of the 'Content-Type' HTTP request headers to 'application/json'
    xhr.setRequestHeader('Content-Type', 'application/json');

    xhr.onreadystatechange = function() {
        if(xhr.readyState == 4 && xhr.status == 200) {
            alert(xhr.responseText); // Returns a 200 response if the submission is successful.
        } else if (xhr.readyState == 4 && xhr.status == 400){
            alert(xhr.responseText); // Returns a 400 error if the submission is rejected.
        } else if (xhr.readyState == 4 && xhr.status == 403){
            alert(xhr.responseText); // Returns a 403 error if the portal isn't allowed to post submissions.
        } else if (xhr.readyState == 4 && xhr.status == 404){
            alert(xhr.responseText); //Returns a 404 error if the formGuid isn't found
        }
    };
    xhr.send(final_data)
}

formv3();
0 Upvotes
1 Reply 1
Kristaf
Member

Script fails to submit to hubspot contacts with form API

I have found out why it wasn't, the portal id was wrong and I added the correct one. Now it works. Thanks

0 Upvotes