Cognito Forms logo

Webhooks

In most cases, Cognito Forms handles everything you need to manage your forms and entries.  On the rare occasion when you might need a bit more control, we offer webhooks. Webhooks allow Cognito Forms to communicate with a third-party system (or internally developed application) in order to post new entries as they are submitted.

JSON

JSON, or JavaScript Object Notation, is a common data interchange format that is very popular on the web.  It allows different systems (or applications) to communicate data in a fast and efficient manner.  The below example illustrates a JSON payload describing a user:

  1. {  
  2.     "id":"12345",  
  3.     "email":"test@test.com",  
  4.     "name":"Test User",  
  5.     "date_created":"2015-01-20T16:42.000Z"  
  6. }  

Webhooks

Webhooks are a set of callbacks created within an application which enable that application to send data to a third-party based upon an event.  So, for example, you may have a system that you want to be notified whenever a new entry is submitted to your form.

Enabling Web Hooks in Cognito Forms

In your form’s Submission Settings, check the box next to “Post JSON data to a website?”.

  • Submit Entry Endpoint: This is the destination URL for the webhook.  New entries (i.e. an entry that changes status from Incomplete to Submitted) will be posted as JSON to this endpoint.

  • Update Entry Endpoint: This is the destination URL for the webhook.  Updated entries (i.e. an entry that is updated when the status is not Incomplete) will be posted as JSON to this endpoint.

Examples

The following examples illustrate configuring and consuming data from Cognito Forms.  They are not necessarily meant to be used in Production, but more as guidance.

Viewing a Post from Cognito Forms

In this example, we are going to illustrate how one may view data as it is sent from Cognito Forms.  In order to do this, we are going to use a third-party service which allows developers to intercept posts to endpoints.

  1. Navigate to http://requestb.in and select “Create a RequestBin”.

  2. Select and copy the “Bin URL” that is displayed.

  3. Enter the URL into the field marked “Post JSON data to a website” under “Submission Settings” on your form in Cognito Forms and save your form.

  4. View your form and submit an entry.

  5. Refresh the page at http://Requestb.in.You should see something along the following lines:

     

Capturing Form Data to a Remote Database

In this example, we will simulate having a remote database in which we will be recording new entries.  Most databases communicate data in a language/format known as “Sql”.  Because Cognito Forms communicates data using JSON, we will need an intermediary program to handle this translation.

  1. Create a small application which can capture data from a Cognito Forms web hook and save that data to a database:

  1. <?php  
  2.     $json = file_get_contents("php://input");  
  3.       
  4.     if (empty($json)) {  
  5.         echo "No data payload";  
  6.         die;  
  7.     }  
  8.       
  9.     $contact = json_decode($json);  
  10.     if ($contact == null && json_last_error() !== JSON_ERROR_NONE) {  
  11.         echo "Error reading JSON: " . json_last_error();  
  12.     }  
  13.   
  14.     $mysqli = new mysqli("localhost", "phptest", "vc3123!!", "phptest");  
  15.     if ($mysqli->connect_errno) {  
  16.         echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;  
  17.         die;  
  18.     }  
  19.       
  20.     $query = "insert into contacts (firstname,lastname,address_line1,address_line2,address_city,address_state,address_postalcode,phone,email,company,comments) values (?,?,?,?,?,?,?,?,?,?,?)";  
  21.     if (!($stmt = $mysqli->prepare($query))) {  
  22.         echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;  
  23.         die;  
  24.     }  
  25.       
  26.     if (!($stmt->bind_param("sssssssssss",   
  27.         $contact->{'Name'}->{'First'},   
  28.         $contact->{'Name'}->{'Last'},  
  29.         $contact->{'Address'}->{'Line1'},  
  30.         $contact->{'Address'}->{'Line2'},   
  31.         $contact->{'Address'}->{'City'},  
  32.         $contact->{'Address'}->{'State'},  
  33.         $contact->{'Address'}->{'PostalCode'},  
  34.         $contact->{'Phone'},  
  35.         $contact->{'Email'},  
  36.         $contact->{'Company'},  
  37.         $contact->{'CommentsOrQuestions'}))) {  
  38.           
  39.         echo "Bind failed: (" . $stmt->errno . ") " . $stmt->error;  
  40.     }  
  41.       
  42.     if (!($stmt->execute())) {  
  43.         echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;      
  44.     }  
  45. ?>  
  1. Configure your web server so that this script can receive an HTTP POST and insert the data into your database. For example, create a new script on your web server called, save_contact.php, so that when a POST is made to http://mysite.com/save_contact.php the script is executed.

  2. Enter the URL into the "Submit Entry Endpoint" option (located under "Post JSON data to a website" in your form's Submission Settings) and save.

  3. View your form and submit an entry.

  4. Verify that the data was saved in the database.

Printable View