Web API - create a CSV from data store query

Hi,

I'm trying to create a simple web API that gets data from the database and produces a csv of results. As I'm new to Appian, I used the 'Query Data Store option which works and provides a JSON response. I'm wondering if there's any way to change the response to CSV?

Thanks in advance!

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    There is a built in template for this, try creating another new Web API but select the "Basic - CSV Download" template. You can then combine the two examples to query from the DB and create a csv output.

  • Hi Tim, 

    Thanks for the response, I've tried to combine the two templates to achieve what I wanted but I'm getting the following error when I call the API: A null parameter has been passed as parameter 2.

    I noticed in the CSV templateis uses 'User.firstName' etc. to write to the CSV, however I can't work out how to reference specific columns from my dataset. This is my current code:

    with(
      /*
      * Run the "queryEntity()" function on "cons!API_Response_test" to retrieve data for the
      * first 50 data store entities and store this in a local variable named
      * "local!entities".
      */
      local!entities: a!queryEntity(
        entity: cons!API_Response_test,
        query: a!query(
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 50
          )
        ),
        fetchTotalCount: true
      ).data,
    
    
      a!httpResponse(
        headers: {
          a!httpHeader(
            name: "Content-Disposition",
            value: "attachment; filename=" & char(34) & "testApiResponse" & ".csv" & char(34)
          )
        },
    
        body: {
          "api_name" & char(10) &
          joinarray(
            /*column name*/
            {a!queryEntity("API_Name")},
            ","
          )
        }
      )
    )

Reply
  • Hi Tim, 

    Thanks for the response, I've tried to combine the two templates to achieve what I wanted but I'm getting the following error when I call the API: A null parameter has been passed as parameter 2.

    I noticed in the CSV templateis uses 'User.firstName' etc. to write to the CSV, however I can't work out how to reference specific columns from my dataset. This is my current code:

    with(
      /*
      * Run the "queryEntity()" function on "cons!API_Response_test" to retrieve data for the
      * first 50 data store entities and store this in a local variable named
      * "local!entities".
      */
      local!entities: a!queryEntity(
        entity: cons!API_Response_test,
        query: a!query(
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: 50
          )
        ),
        fetchTotalCount: true
      ).data,
    
    
      a!httpResponse(
        headers: {
          a!httpHeader(
            name: "Content-Disposition",
            value: "attachment; filename=" & char(34) & "testApiResponse" & ".csv" & char(34)
          )
        },
    
        body: {
          "api_name" & char(10) &
          joinarray(
            /*column name*/
            {a!queryEntity("API_Name")},
            ","
          )
        }
      )
    )

Children