Rebrand 2023 Learn more 

  • Orders/
    Create formatted orders

    Create formatted orders

    This walkthrough shows how you can generate a Comma-Separated Values (CSV) list of orders that include cart items.


    If you want to follow along, you need the following items:

    • A Composable Commerce account with existing orders
    • An access token

    Step-by-step walkthrough

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

    Fetch all of your orders

    You can use filters to only grab the orders you want. An example of that is to filter based on time. In the example, we are grabbing them all.

    const MoltinGateway = require('@moltin/sdk').gateway
    //This global orders array is used throughout the example code snippets.
    let orders;
    const Moltin = MoltinGateway({
      client_id: 'X',
      client_secret: 'X'
    Moltin.Orders.All().then(orders => {
      this.orders = orders

    Create an array of orders and cart items

    Using the previous API response, loop through the data and create an array that has all the data you need.

    exports.itemsLookup([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
            // ID for the orders item
            let id =;
            // simple counter
            // look up each item
            items.forEach(function(item) {
              if( === id) {
                console.log('matching order item object found');
            // if there are not order items left to process
            if(itemsProcessed === {
              console.log('finished processing items');
              order.relationships.items = itemsArray;

    Add the array to a CSV file

    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.

    Orders in Commerce Manager