title: How to print data from API call into a CSV file
body: <p>I'm using the Alpaca trading API and want to export data from a function call into a CSV file.</p>
  
  <p>When I run a call like this:</p>
  
  <pre><code>    closed_orders = api.list_orders(
      status='closed',
      limit=2,
      nested=True  # show nested multi-leg orders
  )
  print(closed_orders)
  </code></pre>
  
  <p>I get back:</p>
  
  <pre><code>[Order({   'asset_class': 'us_equity',
      'asset_id': '8a9-43b6-9b36-662f01e8fadd',
      'canceled_at': None,
      'client_order_id': 'e38a-b51c-349314bc6e9e',
      'created_at': '2020-06-05T16:16:53.307491Z',
      'expired_at': None,
      'extended_hours': False,
      'failed_at': None,
      'filled_at': '2020-06-05T16:16:53.329Z',
      'filled_avg_price': '7.8701',
      'filled_qty': '45',
      'id': '8-4888-9c7c-97bf8c2a3a16',
      'legs': None,
      'limit_price': '7.87',
      'order_class': '',
      'order_type': 'limit',
      'qty': '45',
      'replaced_at': None,
      'replaced_by': None,
      'replaces': None,
      'side': 'sell',
      'status': 'filled',
      'stop_price': None,
      'submitted_at': '2020-06-05T16:16:53.293859Z',
      'symbol': 'CARS',
      'time_in_force': 'day',
      'type': 'limit',
      'updated_at': '2020-06-08T11:21:51.411547Z'}), Order({   'asset_class': 'us_equity',
      'asset_id': '1aef-42f4-9975-750dbcb3e67d',
      'canceled_at': None,
      'client_order_id': '2bde-4572-a5d0-bfc32c2bf31a',
      'created_at': '2020-06-05T16:16:37.508176Z',
      'expired_at': None,
      'extended_hours': False,
      'failed_at': None,
      'filled_at': '2020-06-05T16:16:37.531Z',
      'filled_avg_price': '10.8501',
      'filled_qty': '26',
      'id': '4256-472c-a5de-6ca9d6a21422',
      'legs': None,
      'limit_price': '10.85',
      'order_class': '',
      'order_type': 'limit',
      'qty': '26',
      'replaced_at': None,
      'replaced_by': None,
      'replaces': None,
      'side': 'sell',
      'status': 'filled',
      'stop_price': None,
      'submitted_at': '2020-06-05T16:16:37.494389Z',
      'symbol': 'IGT',
      'time_in_force': 'day',
      'type': 'limit',
      'updated_at': '2020-06-08T11:21:51.424963Z'})]
  </code></pre>
  
  <p>How do I grab this data and print it into a CSV? I tried to write something like this but I get the error "Order" object has no keys. I assumed I'd be able to loop through the API response and write according to the headers; how do I break down/flatten the API response accordingly?</p>
  
  <pre><code>with open('historical_orders.csv', 'w', newline='') as csvfile:
      fieldnames = [
          'asset_id',
          'canceled_at',
          'client_order_id',
          'created_at',
          'expired_at',
          'extended_hours',
          'failed_at',
          'filled_at',
          'filled_avg_price',
          'filled_qty',
          'id',
          'legs',
          'limit_price',
          'order_class',
          'order_type',
          'qty',
          'replaced_at',
          'replaced_by',
          'replaces',
          'side',
          'status',
          'stop_price',
          'submitted_at',
          'symbol',
          'time_in_force',
          'type',
          'updated_at'
      ]
      writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
  
      writer.writeheader()
  
      for c in closed_orders:
          writer.writerow(c)
  </code></pre>