Answered

Get total number of responses of my plan by API

  • 19 March 2021
  • 7 replies
  • 219 views

Hello everyone!

I am working on a project to implement the Servicenow software license control module, and I need to use the API to bring the total number of responses used in my contract.
I basically need the same information that is displayed when I go to Settings> Plan & Billing, where the number of responses already used and the total that I still have available is displayed.

Is it possible to obtain this information through a get at some endpoint?

Or any documentation that explains how that value is calculated so that I can try to make this calculation based on data from other endpoints?

Thank you for your help!

icon

Best answer by picsoung 20 March 2021, 00:47

View original

7 replies

Userlevel 7
Badge +5

Hi @cschiavi !

This is above my tech knowledge level but @john.desborough is super helpful and insightful with the various technologies (perhaps even APIs!). 

If not, @Liz and/or @Mariana have been super helpful in the community as well. So I tagged those I’m familiar with and who might be able to answer your question. 

My apologies for not being helpful on this topic!

Userlevel 7
Badge +5

Hi @cschiavi this is a great question! We don’t currently have this information available via the API, but if there’s any further context you’d like to provide for this call to share with our product team, that would be great!

For now, that would be the need for the moment. An endpoint where I could see the total responses consumed from my plan through an API, so that I could import this data into the company's asset control software.
I also use the information of users who have access to the platform, but this I can already consult by API.

I tried to consult all of the forms that are with public status as "true" and then get and add all the answers from these forms, but the numbers did not match the data displayed in my plan settings, so to get this today I would need at least know the rules that are used to calculate the number of responses used in my plan.

Userlevel 7
Badge +5

Hi @cschiavi 

As ​​​@Liz mentioned, we don’t have an this data directly available via the API.

Right now, one way you can reconstruct this, would be to get all the forms `GET /forms` and then get all the responses for each form `GET /forms/{formId}/responses?since={begin_date}&until={end_date}` for a period of time. And then add numbers up.

It should tell you how many responses you have collected over this period of time across all the forms.

But you would not know the limit of your plan, this is not available in current public API.

Are you doing this for yourself, or as an integration that could be distributed? (this could help us prioritize the work on such an API)

Hi @picsoung 

Thanks for the suggestion, I will perform the tests based on it.

I am integrating with Servicenow's license control tool, with the objective of centralizing all the company's license contracts and consumptions in a centralized way.

I believe that this type of consumption information from the plan by API can be very useful for several other software for controlling the company's assets.

Thanks!

Hi @picsoung ,

I performed the queries by making the date filter based on the reset date of my plan's responses, but unfortunately the number of responses returned does not match what is displayed on my plan's plainel, today we have consumed 136,900 responses but the number returned in the API was 746,553. I honestly do not know if I will get this number, or a documentation that clearly shows how this number of responses is assembled in the administrative panel of my plan ...

Below the script (javascript and Servicenow platform methods) used for querying and processing the data:

 

//Get Forms
var epFM = "https://api.typeform.com/forms?page_size=200";

var reqFM = new sn_ws.RESTMessageV2();
reqFM.setEndpoint(epFM);
reqFM.setHttpMethod('get');
reqFM.setRequestHeader("Authorization", "Bearer XXXXX");
var respFM = reqFM.execute();
var fmBody = respFM.getBody();
var parseFM = JSON.parse(fmBody);

var totalPages = parseFM.page_count;
var qtdRespostas = 0;

var anoAtual = new Date().getFullYear();
var anoAnterior = anoAtual-1;
var fatInicio = new GlideDateTime(anoAnterior+'-06-29 00:00:00');
var fatFim = new GlideDateTime(anoAtual+'-06-28 23:59:59');
var dtInicio = fatInicio.getValue().replace(' ','T');
var dtFim = fatFim.getValue().replace(' ','T');

for(pg=1; pg <= totalPages; pg++){
    var reqFMpg = new sn_ws.RESTMessageV2();
    reqFMpg.setEndpoint(epFM+'&page='+pg);
    reqFMpg.setHttpMethod('get');
    reqFMpg.setRequestHeader("Authorization", "Bearer XXXXX");
    var respFMpg = reqFMpg.execute();
    var fmPgBody = respFMpg.getBody();
    var parseFMpg = JSON.parse(fmPgBody);

    for(y=0; y < parseFMpg.items.length; y++){

        if(parseFMpg.items[y].settings.is_public){

            //Get Responses from each form          
            var epRESP = 'https://api.typeform.com/forms/'+parseFMpg.items[y].id+'/responses?since='+dtInicio+'&until='+dtFim+'&page_size=1';

            var reqResponse = new sn_ws.RESTMessageV2();
            reqResponse.setEndpoint(epRESP);
            reqResponse.setHttpMethod('get');
            reqResponse.setRequestHeader('Content-Type','text/plain')
            reqResponse.setRequestHeader('Accept','application/json')
            reqResponse.setRequestHeader('Authorization', 'Bearer XXXXX');
            var respResponse = reqResponse.execute();
            var responseBody = respResponse.getBody();
            var parseResponse = JSON.parse(responseBody);

            var qtdRespForm = parseResponse.total_items;
            if(qtdRespForm > 0){
                qtdRespostas += qtdRespForm;
            }
        }
    }   
}
gs.info('TOTAL RESP: '+qtdRespostas);

Userlevel 7
Badge +5

Thank you @cschiavi for providing this snippet.
You probably need to filter responses that are completed (actually submitted) by adding completed=true.

Hopefully the number get closer to what you see in your dashboard :)

Reply