HubSpot Form Submission API

A generic, reusable API endpoint for submitting any form to HubSpot across the Endless Commerce website.

Examples

JavaScript (Client-Side)

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();
}

Astro (Server-Side or HTML Forms)


<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>

Response Format

Success Response (200)

{
  "success": true,
  "inlineMessage": "Thanks for submitting the form.",
  "redirectUri": ""
}

Error Response (4xx/5xx)

{
  "error": "HubSpot submission failed",
  "status": 400,
  "details": {
    "message": "Error details from HubSpot"
  }
}

Common HubSpot Field Names

Use these standard field names for automatic mapping to HubSpot contact properties: