to filter the faxes in your project by a number of parameters and then insert the fax data into a CSV for your own record keeping. import Tabs from @theme/Tabs; import TabItem from @theme/TabItem; Step by step walkthroughs will be provided in detail below as the steps and methods vary with each language. The possible parameters that you can filter by are DateCreatedAfter, DateCreatedOnOrBefore, To, and From. Full code example: List Faxes to CSV
python # This code snippet is to list faxes from your LaML logs. # It will then put the data into a dataframe and export to CSV. # You MUST have datetime and pandas installed and imported for this to work. from datetime import datetime from signalwire.rest import Client as signalwire_client import pandas as pd client = signalwire_client(ProjectID, AuthToken, signalwire_space_url=your-space.signalwire.com) # Start Time is a datetime object # order for the arguments is Year, Month, Date, Hour, Minute, Seconds. # Leave hour, minute, and seconds at 0 faxes = client.fax.faxes.list( to=+18551111111, from_=+12251111111, date_created_after=datetime(2021, 0o3, 0o1, 17, 0o7, 0), date_created_on_or_before=datetime(2021, 0o4, 1, 0, 0, 0), ) # Prints call data for record in faxes: print((record.date_created, record.status, record.sid)) # # Sets up an empty array d = [] # Appends all data from calls into an array for record in faxes: d.append((record.from_, record.to, record.date_created, record.status, record.sid)) print(d) # # Puts fax log array into dataframe with headers for easier reading. df = pd.DataFrame(d, columns=(From, To, Date, Status, FaxSID)) print(dataframe) print(\n) print(df) # # Exports dataframe to csv, index=False turns off the indexing for each row df.to_csv(faxes.csv, index=False, encoding=utf-8) js import { RestClient } from @signalwire/compatibility-api; import fs from fs; const client = RestClient( ProjectID, AuthToken, { signalwireSpaceUrl: Your-Space.signalwire.com } ); let writeStream = fs.createWriteStream(Faxes.csv); let newLine = []; const header = [ Fax Sid, To, From, Date Created, Status, Price, ]; newLine.push(header); client.fax.faxes .list({ to:+18551111111, from:+12251111111, }) .then((faxes) => { faxes.forEach((c) => { newLine.push( c.sid, c.to, c.from, c.dateCreated, c.status, c.price ); }); writeStream.write(newLine.join(,) + \n, () => {}); writeStream.end(); }); ruby # For the following code to work, you will need to have Active Support and the SignalWire Ruby SDK installed. require signalwire/sdk require active_support/time require csv # Replace these values with your Project ID, Auth Token, and Space URL. @client = Signalwire::REST::Client.new ProjectID, AuthToken, signalwire_space_url: your-space.signalwire.com # Choose what parameters you want to filter by - this example filters by the past to, from, and the last 7 days. faxes = @client.fax.faxes.list(page_size: 100, to:+18551111111, from:+12251111111, date_created_after: (DateTime.now - 7.days).strftime(%a, %d %b %Y %H:%M:%S GMT)) # Create headers headers = [FaxSID,Date Created, From, To, Status] faxes.each do |record| # Create and open a CSV CSV.open(Faxes.csv, w+) do |csv| # Insert headers first csv << headers # For each record in faxes, insert the data one by one into CSV. Make sure the order of parameters here matches the order of the headers, or the data will be mismatched. faxes.each do |record| puts record.sid csv << [record.sid, record.date_created, record.from,record.to, record.status] end end end php your-space.signalwire.com)); // filters by whatever parameters you need $faxes = $client->fax->faxes->read([ dateCreatedAfter => 2021-03-01, From => +12251111111, // filter by From To => +18551111111, // filter by To ]); // Write Headers $fields = array(Fax SID, From, To, Create Date, Status, Direction, Price); echo .implode(,, $fields)..\n; // Open File named TodaysDate_fax$faxReport $fp = fopen(date(Y-m-d)._FaxReport.csv, w); // Insert headers fputcsv($fp, $fields); // Write rows foreach ($faxes as $fax) { $row = array( $fax->sid, $fax->from, $fax->to, $fax->dateCreated->format(Y-m-d H:i:s), $fax->status, $fax->direction, $fax->price, ); // Insert rows into CSV fputcsv($fp, $row); echo .implode(,, $row)..\n; } // close file fclose($fp); Python For the following code to work, you will need to have pandas, DateTime, and the SignalWire Python SDK installed. Read about the different ways to install pandas [Here](https://pandas.pydata.org/docs/getting_started/install.html) Read about DateTime and how to install using pip [Here](https://pypi.org/project/DateTime/) Read about the SignalWire Python SDK and how to install it [Here](/compatibility-api/sdks) First, we need to import the necessary resources. In this example, that is DateTime, pandas, and the SignalWire Client. We also need to instantiate the SignalWire client using the project ID, auth token, and Space URL. python from datetime import datetime from signalwire.rest import Client as signalwire_client import pandas as pd client = signalwire_client(ProjectID, AuthToken, signalwire_space_url = SpaceURL) Next, we need to choose what parameters wed like to filter by. In this example, I have included how to filter by a date range, To number, and From number. date_created_after and date_created_on_or_before are both DateTime objects where the order for the arguments is Year, Month, Date, Hour, Minute, Seconds. You can leave hour, minute, and seconds at 0, unless you have a specific time of day you would like to filter by. For months Jan - September, A slight change was made because the python version (3.9) does not support leading 0s in DateTime anymore. You must use the 0o prefix for octal literals now. That is reflected below in the code. If your version doesn’t include that limitation, you can switch it back to 01 or whatever month you need. python faxes = client.fax.faxes.list( to=+18551111111, from_=+12251111111, date_created_after=datetime(2021, 0o3, 0o1, 17, 0o7, 0), date_created_on_or_before=datetime(2021, 0o4, 1, 0, 0, 0), ) We now need to insert the data from faxes into an array. Here we set up an empty array. This will contain all of the data that we will gather from the faxes. We can loop through messages and append each of the following parameters to our empty array: date_created, status, and sid. You can expand this to include as many or as few parameters as youd like. To see all of the parameters returned in the JSON response, you can view our API documentation here: [List Faxes API](/rest/compatibility-api/endpoints/list-all-faxes) After that, we print the array so that we can see what we have inserted for debugging purposes. python d = [] # Appends all data from faxes into an array for record in faxes: d.append((record.from_, record.to, record.date_created, record.status, record.sid)) print(d) Next, we create a data frame and export it to CSV. The next section of this code is to create a data frame using pandas with column name headers. Its important to make sure that the order of the headers matches the order of the parameters you inserted into the array above. If you choose to add more or remove parameters, make sure to double-check that the order matches, or your data will be mismatched in the CSV. We then print the data frame for debugging purposes and export the data frame to CSV. Using the parameter index=False turns off the indexing for each row. You can rename faxes.csv to be as specific or general as youd like. python df = pd.DataFrame(d, columns=(From, To, Date, Status, FaxSID)) print(dataframe) print(\n) print(df) df.to_csv(faxes.csv, index=False, encoding=utf-8) PHP For the following code to work, you will need to have the SignalWire PHP SDK installed. Read about the SignalWire PHP SDK and how to install it [Here](/compatibility-api/sdks) You will also need to make sure that the vendor/autoload.php path points to the correct location on your computer, as we can’t determine that for you. However, if you want to run this script exactly, install the SDK from within the folder that contains this PHP script. First, we need to import the necessary resources. In this example, that just means we need to point to the correct path of the vendor autoload file on your computer. We also need to instantiate the SignalWire client using the project ID, auth token, and Space URL. PHP YOURSPACE.signalwire.com)); Next, we need to choose what parameters wed like to filter by. In this example, I have included how to filter by a date range, from number, and to number. PHP $faxes = $client->fax->faxes->read([ dateCreatedAfter => 2021-03-01, From => +12251111111, // filter by From To => +18551111111, // filter by To ]); In the next section, we need to create the file to insert the fax data into. Begin by writing headers and storing them in a variable called $fields. We also use implode to join the elements of the array with a string. We will then use fopen to create and open a file named **TodaysDate_FaxReport**. After that, we use fputcsv to insert our headers stored in $fields into our file. PHP $fields = array(Fax SID, From, To, Create Date, Status, Direction, Price); echo .implode(,, $fields)..\n; $fp = fopen(date(Y-m-d)._FaxReport.csv, w); fputcsv($fp, $fields); Next, we need to loop through our $faxes array in order to gather the necessary fax data and insert it into our CSV. It is important to make sure the order of the headers is the same as the order of the values we will be gathering. As we loop through each fax record, we will insert it into the CSV one by one and use implode to join the elements of the array with a string. The last step is to close the file! PHP foreach ($faxes as $fax) { $row = array( $fax->sid, $fax->from, $fax->to, $fax->dateCreated->format(Y-m-d H:i:s), $fax->status, $fax->direction, $fax->price, ); fputcsv($fp, $row); echo .implode(,, $row)..\n; } fclose($fp); Ruby - For the following code to work, you will need to have Active Support, CSV, and the SignalWire Ruby SDK installed. Read about the SignalWire Ruby SDK and how to install it [Here](/compatibility-api/sdks) Read about Active Support and how to install [Here](https://rubygems.org/gems/activesupport/versions/5.0.0.1) Read about CSV and how to install [Here](https://rubygems.org/gems/csv/versions/0.0.1) First, we need to import the necessary resources. In this case, we need to import signalwire/sdk, active_support/time, and csv. We also need to instantiate the SignalWire client using the project ID, auth token, and Space URL. Ruby require signalwire/sdk require active_support/time require csv @client = Signalwire::REST::Client.new ProjectID, Auth Token, signalwire_space_url: YOURSPACE.signalwire.com Next, we need to choose what parameters wed like to filter by. In this example, I have included how to filter by a date range, to number, from number, and the page size of the results returned. Ruby faxes = @client.fax.faxes.list(page_size: 100, to:+18551111111, from:+12251111111, date_created_after: (DateTime.now - 7.days).strftime(%a, %d %b %Y %H:%M:%S GMT)) Next, we need to create headers for our CSV and insert the headers/fax records into the CSV. Its important to make sure that the headers are in the exact same order as the message record parameters. In the code section below, we create an array of headers. Next, we open a file Faxes.csv. We insert the headers before we begin the loop, otherwise, the headers will precede every single record. After the headers have been inserted, we loop through each of the fax records in faxes and insert them one by one with all the listed parameters into the CSV. Ruby headers = [FaxSID,Date Created, From, To, Status] faxes.each do |record| CSV.open(Faxes.csv, w+) do |csv| csv << headers faxes.each do |record| puts record.sid csv << [record.sid, record.date_created, record.from,record.to, record.status] end end end Node.js - For the following code to work, you will need to have the SignalWire NodeJS SDK installed. Read about the SignalWire NodeJS SDK and how to install it [Here](/compatibility-api/sdks) First, we need to import the necessary resources. In this case, we need to import @signalwire/compatibility-api and fs. We also need to instantiate the SignalWire client using the project ID, auth token, and Space URL. Javascript import { RestClient } from @signalwire/compatibility-api; import fs from fs; const client = RestClient( ProjectID, Auth Token, { signalwireSpaceUrl: YOURSPACE.signalwire.com } ); Next, we need to create our CSV file, an empty array to put our records into, and an array of headers. Its important to make sure that later when we loop through the fax records, we push the parameters into the CSV in the same order as the headers here. Lastly, we push the headers into the CSV as the first row. Javascript let writeStream = fs.createWriteStream(Faxes.csv); let newLine = []; const header = [ Fax Sid, To, From, Date Created, Status, Price, ]; newLine.push(header); Next, we determine which parameters wed like to filter by and what parameters we expect to be pushed into the CSV. The filters used in this example are the To and From numbers. As we loop through faxes, we push each parameter into the CSV one by one. After we are finished looping through faxes We use writeStream.write to separate these values by commas and add a new line at the end. Lastly, we use writeStream.end to close the file. Javascript client.fax.faxes .list({ to:+18551111111, from:+12251111111, }) .then((faxes) => { faxes.forEach((c) => { newLine.push( c.sid, c.to, c.from, c.dateCreated, c.status, c.price ); }); writeStream.write(newLine.join(,) + \n, () => {}); writeStream.end(); }); Conclusion - Record keeping is important, so we hope these examples help make it easier to filter your faxes and export them to an external CSV with ease. All of these examples will accomplish the same goal and can be built upon to expand or further drill down the amount of data returned. Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance! Sign Up Here If you would like to test this example out, you can create a SignalWire account and Space [here](https://m.signalwire.com/signups/new?s=1). Please feel free to reach out to us on our [Community Slack](https://signalwire.community/) or create a Support ticket if you need guidance!