Create a new Application directly from a Webform

A common example use case whereby Enquiries are automatically generated based on an expression of interest

In this example, we will look at setting up an integration so that candidates can complete a form to express their interest in franchising, and this will automatically create an Application in FranchiseLab Recruiter to skip the manual data entry usually required.

The below is an example piece of JavaScript assumes the page contains a form with fields firstName, lastName, email and mobileNumber. Upon submission, it posts these values, along with the api_key in the header, to the specifed endpoint for Create Contact.

// Create a function to handle form submission
async function submitForm(event) {
  event.preventDefault(); // Prevent the default form submission

  // Get the form data
  const firstName = document.getElementById('firstName').value;
  const lastName = document.getElementById('lastName').value;
  const email = document.getElementById('email').value;
  const mobileNumber = document.getElementById('mobileNumber').value;

  // Prepare the data object
  const formData = {
    firstName: firstName,
    lastName: lastName,
    email: email,
    mobileNumber: mobileNumber,
  };

  // Convert the data to JSON
  const jsonData = JSON.stringify(formData);

  // Define the API URLs and API key
  const contactApiUrl = 'https://api.franchiselab.com/recruiter/contact/';
  const applicationApiUrl = 'https://api.franchiselab.com/recruiter/application/';
  const apiKey = 'eyJhbGciOiJIUzI1NiJ9.eyJ0aWQiOjMwMjE0NzQ0OSwiYWFpIjoxMSwidWlkIjozMjIxMjE5MCwiaWFkIjoiMjAyMy0xMi0xMVQyMjo1Nzo0MS4yNTJaIiwicGVyIjoibw6d3JpdGUiLCJhY3RpZCI6ODk4MzkwMCwicmduIjoidXNlMSJ9.6HS4mSfLnKvBpyC4rJHRtp_Eyv37CsWUEshwxNhehfc';

  try {
    // Create and configure the POST request to the contact API
    const contactResponse = await fetch(contactApiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`,
      },
      body: jsonData,
    });

    if (!contactResponse.ok) {
      throw new Error('Failed to create contact');
    }

    const contactData = await contactResponse.json();
    const contactId = contactData.contactId;

    // Create and configure the POST request to the enquiry API with the contact ID
    const appplicationResponse = await fetch(applicationApiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `${apiKey}`,
      },
      body: JSON.stringify({ contactId }),
    });

    if (!applicationResponse.ok) {
      throw new Error('Failed to create enquiry');
    }

    // Handle the response from the server (optional)
    const applicationResult = await applicationResponse.json();
    console.log('Enquiry Result:', applicationResult);
  } catch (error) {
    console.error('Error:', error);
  }
}