API Examples for On-Form Use

Formsite API examples

Formsite’s API lets account users retrieve information about forms and results. With the correct code, form owners have the ability to display data on the form itself. Some API examples for use on the form include:

API Examples for Results Numbers

Formsite API examples integration

Sometimes form owners want to display the total number of results for a variety of reasons. They may want to show the form’s popularity or use that number to trigger rules. Every example uses this method:

  1. Add a Custom Code item and a Hidden Field item to the top of the form
  2. Type or paste the API code with the highlighted parts completed
  3. Use the Hidden Field item’s pipe code to access the data

The code to retrieve the total number of results and insert into a Hidden Field item:

<script>
$.ajax({
        url: 'https://fsX.formsite.com/api/v2/billy001/forms/api1/results',
        beforeSend: function(xhr) {
             xhr.setRequestHeader("Authorization", "bearer xxxxxxxxxx")
        }, success: function(data){
            $("#RESULT_TextField-1").val(Object.keys(data.results).length);
        }
})
</script>

Areas to change:

  • The form’s Form Settings -> Integrations -> Formsite API page contains two values:
    • API base URL
    • Account access token
  • Use the API base URL in the url parameter, ending with “/results”
  • Replace the “xxxxxx” in the Authorization header with the Account access token
  • If the Hidden Field item is the first text input field on the form, its id value will be the same as above. If it is not at the top of the form before any other text input fields, view the page source to get the item’s id value.

Test the script by changing the Hidden Field to a Short Answer item then visiting the form. After verifying that everything works as it should, change the item back to a Hidden Field item.

Get Average Score

Using the same code, we can work with the “data” object to get more information, like the sum of all scores:

<script>
$.ajax({
        url: 'https://fsX.formsite.com/api/v2/billy001/forms/api1/results',
        beforeSend: function(xhr) {
             xhr.setRequestHeader("Authorization", "bearer xxxxxxxx")
        }, success: function(data){
            var r = Object.values(data.results).reduce((a, b) => a + parseInt(b.score), 0);
           $("#RESULT_TextField-1").val(r);
        }
})
</script>

To show the average score, divide by the number of results:

<script>
$.ajax({
        url: 'https://fsX.formsite.com/api/v2/billy001/forms/api1/results',
        beforeSend: function(xhr) {
             xhr.setRequestHeader("Authorization", "bearer xxxxxxxx")
        }, success: function(data){
            var e = Object.keys(data.results).length;
            var r = Object.values(data.results).reduce((a, b) => a + parseInt(b.score), 0);
            var a = Math.round((r/e) * 100) / 100;
           $("#RESULT_TextField-1").val(a);
        }
})
</script>
Billions of forms submitted