Create a formatted order csv

A walkthrough how you could generate a list of your orders that includes items.

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. Creating an array that has orders and 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([0], orders.included.items).then((order) => {

The itemLookup function is taking a 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;

4. Adding array to CSV

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

To see the full code