Skip to content

Instantly share code, notes, and snippets.

@AbhimanyuAryan
Created February 19, 2023 16:05
Show Gist options
  • Save AbhimanyuAryan/8f15686e22e35f8b7c025b5d39c2a50b to your computer and use it in GitHub Desktop.
Save AbhimanyuAryan/8f15686e22e35f8b7c025b5d39c2a50b to your computer and use it in GitHub Desktop.
hubspot form submission bearer token
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const url = 'https://api.hsforms.com'
const endpoint = 'submissions/v3/integration/secure/submit'
const portalId = '26984581'
const formId = '53b71b97-dfdf-452e-baba-21c9adb7f63f'
const hapiKey = 'generated from developer panel'
function formv3(){
// Create the new request
var xhr = new XMLHttpRequest();
var apiurl = `${url}/${endpoint}/${portalId}/${formId}`
// Example request JSON:
var data = {
"fields": [
{
"objectTypeId": "0-1",
"name": "firstname",
"value": "Jeff"
},
{
"objectTypeId": "0-1",
"name": "lastname",
"value": "Nippard"
},
{
"objectTypeId": "0-1",
"name": "email",
"value": "[email protected]"
}
]
}
var final_data = JSON.stringify(data)
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authenication', 'Bearer XXXXXXXXXX');
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
console.log('Returns a 200 response if the submission is successful.')
console.log(xhr.responseText);
} else if (xhr.readyState == 4 && xhr.status == 400){
console.log('Returns a 400 error the submission is rejected.')
console.log(xhr.responseText);
} else if (xhr.readyState == 4 && xhr.status == 403){
console.log('Returns a 403 error if the portal isn\'t allowed to post submissions.')
console.log(xhr.responseText);
} else if (xhr.readyState == 4 && xhr.status == 404){
console.log('Returns a 404 error if the formGuid isn\'t found')
console.log(xhr.responseText);
}else {
console.log("none of the state worked")
}
}
// Sends the request
xhr.send(final_data)
}
formv3()
@AbhimanyuAryan
Copy link
Author

just replace XXXXXXXX with your token

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment