Generating CSV Files

Generate CSV Flat Files through the API

Generating CSV flat files can be done through the API in Xano.

Snippet: https://www.xano.com/snippet/LiPGIfYL

Generating a CSV requires the use of headers and a filter in Xano.

Headers:

Use the HTTP Header function to set two HTTP headers required to generate a CSV through the API.

Content-Disposition

Content-Disposition: attachment;filename="example.csv"

You can choose any file name you want, you don't have to use "example". The name can be made dynamic with sprintf as shown in the video above.

Content-Type

Content-Type: text/csv

Filter: csv_create

The csv_create filter creates a CSV format data stream from a list of column names and their corresponding data rows.

Start with an array of the header names.

Then apply the csv_create filter. Add the array of values of the rows. It's recommended to leave the default values for the separator, enclosure, and escape.

Basic example of a function stack creating a CSV:

Downloading a CSV file

When you run an API endpoint in this format, Xano will pick up that there is a downloadable CSV file and provide the option to download the file in the debugger:

When you run this endpoint live, it will automatically start to download the generated CSV file. Give it a try in your browser.

Last updated