A generic, reusable API endpoint for submitting any form to HubSpot across the Endless Commerce website.
async function submitContactForm(formData: { email: string; message: string }) {
const response = await fetch('/api/hubspot/forms/submit', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
formGuid: 'your-hubspot-form-id',
fields: {
email: formData.email,
message: formData.message,
},
pageName: document.title,
pageUri: window.location.href,
}),
});
if (!response.ok) {
throw new Error('Form submission failed');
}
return await response.json();
}
<form action="/api/hubspot/forms/submit" method="POST">
<input type="hidden" name="formGuid" value="your-hubspot-form-id" />
<label for="email">Email</label>
<input type="email" name="email" id="email" required />
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" />
<label for="lastname">Last Name</label>
<input type="text" name="lastname" id="lastname" />
<label for="company">Company</label>
<input type="text" name="company" id="company" />
<button type="submit">Submit</button>
</form>
{
"success": true,
"inlineMessage": "Thanks for submitting the form.",
"redirectUri": ""
}
{
"error": "HubSpot submission failed",
"status": 400,
"details": {
"message": "Error details from HubSpot"
}
}
Use these standard field names for automatic mapping to HubSpot contact properties:
email - Email address (usually required)firstname - First namelastname - Last namecompany - Company namephone - Phone numberwebsite - Website URLjobtitle - Job titlemessage - Message/notesindustry - Industrycompany_size - Company sizecountry - Countrystate - State/regioncity - City