• Log in
  • Contact Us

Get in touch  

Developer Guides • Orders

Create a formatted order CSV

Last updated: 24 Jun 2019

This is a walkthrough how you could generate a list of your orders that include cart items.


  • Moltin account with existing orders

Summary of steps required

  • Fetch your existing orders.
  • Create an array including all orders with cart items.
  • Add the array in a CSV file.

1. Get your access token

You will need to get a client_credentials access token to follow along making the API requests outlined below.

const MoltinGateway = require('@moltin/sdk').gateway

const Moltin = MoltinGateway({
	client_id: 'X',
	client_secret: 'X'

2. Fetch all of your order

You can use filter to only grab the orders you want. Example of that would be to filter based on time. In the example we will be grabbing them all.

const MoltinGateway = require('@moltin/sdk').gateway
//This global orders array will be used through out the example code snippets.
let orders;

const Moltin = MoltinGateway({
  client_id: 'X',
  client_secret: 'X'

Moltin.Orders.All().then(orders => {
  this.orders = orders

3. Create an array of orders and cart items

Using the above API response you will want to loop through the data and create an array that has all if the data you may need.

exports.itemsLookup(orders.data[0], orders.included.items).then((order) => {

The itemLookup function is taking an order which includes an array of items. The lookup loops through the orders and assigns the items to the id.

exports.itemsLookup = function(order, items) {
    return new Promise(function(resolve, reject) {
      var itemsProcessed = 0;
      let itemsArray = [];
      //for each of the orders related items
      order.relationships.items.data.forEach(function(item) {
        // ID for the orders item 
        let id = item.id;
        // simple counter
        // look up each item
        items.forEach(function(item) {
          if(item.id === id) {
            console.log('matching order item object found');

        // if there are not order items left to process
        if(itemsProcessed === order.relationships.items.data.length) {
          console.log('finished processing items');
          order.relationships.items = itemsArray;

4. Add the array to CSV

All you need to do now is to add the array to a CSV file.

.then((orders) => {
	exports.formatOrders(orders, orders.included.items)
    .then((formattedOrders) => {

.catch((e) => {

To see the full code, visit GitHub.

Further reading

Explore More Developer Guides

26 Jun 2019

Manage abandoned carts with Flows

Quick intro on how carts work There are three aspects of how Moltin carts operate:

Read More
Matt Foyle
in Flows | Carts
14 Jun 2019

Extend the order and cart resource with Flows

Extend the order resource to add shipping provider and a consignment number.

Read More
Joanna Suau
in Flows | Orders | Carts
14 Jun 2019

Store data form third party integration with Flows

Store nexus locations from a tax calculation service using Flows.

Read More
Joanna Suau
in Flows | Integrations