A step-by-step guide on how to set up an organization from scratch, using our CLI
To begin receiving orders and making sales through Commerce Layer, you need to configure your organization to meet some requirements: the manual configuration involves creating all the necessary resources, linking them based on their mutual relationships, and properly populating your environment with sample (or real) data. This guide will walk you through the process of setting up a new organization using the command line from start to finish and is a good opportunity to learn (by doing) how Commerce Layer works, in deep.
Please refer to Commerce Layer data model documentation to get an overview of the most relevant API entities, their mutual relationships, and common usage with the help of E-R diagrams.
As an alternative, if you want to go straight to the chase, you can follow the short onboarding tutorial you'll be prompted as soon as you sign up to create a new organization, seed it with test data, and place your first order from scratch, in minutes.
These are the steps you need to follow to create an organization on Commerce Layer:
Create a free developer account here. If you already have an account, please skip to step 3.
Upon successful signup, skip the onboarding tutorial for the purposes of this guide (you will set up and seed the organization manually through the CLI shortly).
Click on your current organization name in the left upper corner of your dashboard homepage to access your list of organizations and select New organization from the dropdown to create a new one for your business. Then, on the following page, enter a name and select the region where your data will be stored, as seen in the screenshots below.
Once the organization is created, the chosen data storage location cannot be changed anymore. If — for whatever reason — you need to delete one or more of the organizations you created, please ask the organization owner to reach out to our support team and send a specific request to support@commercelayer.io specifying the list of the organization slug you want to be deleted.
Create integration API credentials
Follow these steps to create new integration API credentials (client ID, client secret, and base endpoint) from the Dashboard with an Admin role. Remember to save them as we'll use them later to interact with the CLI.
Install the Commerce Layer CLI
Run the command below on your terminal to install the CLI using your favorite package manager:
npminstall-g@commercelayer/cli
yarn global add @commercelayer/cli
Installing the CLI provides access to the commercelayer command, which can also be accessed with the aliases clayer and cl.
You can run commercelayer help at any point to learn about the available command options.
Log in using the API credentials
Log in via the CLI using the previously created integration API credentials like so:
Here’s a breakdown of each of the command options:
The applications:login command lets you manage the login with the CLI using your API credentials.
The -o flag allows you to pass in an organization slug as a parameter.
<organizationSlug> represents the slug from your organization’s base endpoint (e.g. sample-shop from https://sample-shop.commercelayer.io).
The -i flag allows you to pass in a client ID as a parameter.
<clientId> represents your client ID (you can find it on the API credentials details page on the dashboard).
The -s flag allows you to pass in a client secret as a parameter.
<clientSecret> represents your client secret (you can find it on the API credentials details page on the dashboard).
The -a flag allows you to pass in an alias that will be associated with the credentials you want to log with on your terminal (this is useful when switching between different API credentials).
Install the resources plugin
To get started, you need to install the resources plugin so that you can execute CRUD operations on the API resources. To do that, use the command below:
clplugins:installresources
You can run the command cl resources to list all the available Commerce Layer API resources and a link to the documentation of each one. Use commercelayer resources --help to see the available commands and what they do.
For this guide, we would be using:
The commercelayer resources:create [RESOURCE] command to create new resources.
The -r, --relationship=<value> flag which defines a relationship with another resource(s).
The -a, --attribute=<value> flag which defines the attributes of a resource that will be used.
Configure your organization
To get your organization ready to receive orders, you need to create some resources. These resources are grouped into Markets, Shipping, Payments, and Products. The sections below describe each of these resources in sequential order, and how to create them using the previously installed resources plugin of the CLI. All the resources will be created using sample data, and we will add the following for each of them:
A description of the resource with a link to the related object details in the API reference.
The command for creating the resource.
The output response of the successfully run command.
A link to the related data model where you can check the flowchart that illustrates how the specific resource relates to other API entities.
When you successfully create a new resource, you can confirm this by running cl resources:list <resource_type> and checking if the resource is in the returned list (e.g. cl resources:list payment_gateways will list all the payment gateways that have been created).
Markets
Address
The address resource is the foundation of users and merchants in Commerce Layer. It includes the literal details of a physical address of a person. An address can be associated with other resources or orders as their shipping or billing addresses.
On successful run, the newly created address object information is prompted:
{ id:'dqJkunqnQa', type:'addresses', business:true, first_name:null, last_name:null, company:'Sample Shop SRL', full_name:'Sample Shop SRL', line_1:'Via Roma 123', line_2:null, city:'Firenze', zip_code:'50123', state_code:'FI', country_code:'IT', phone:'+39 055 1234567890', full_address:'Via Roma 123, 50123 Firenze FI (IT) +39 055 1234567890', name:'Sample Shop SRL, Via Roma 123, 50123 Firenze FI (IT) +39 055 1234567890', email:'sample-shop@example.com', notes:null, lat:null, lng:null, is_localized:false, is_geocoded:false, provider_name:null, map_url:null, static_map_url:null, billing_info:null, created_at:'2022-03-17T14:27:26.526Z', updated_at:'2022-03-17T14:27:26.526Z', reference:null, reference_origin:null, metadata: {}}
Merchant
A merchant is a fiscal representative that is selling in a specific market. Tax calculators use the merchant's address to determine the tax rate for an order. The merchant needs to be associated with a billing address. To do that we will use the ID from the previously created address resource.
The following command creates a new merchant and associates it with a billing address:
Stock locations contain the inventory of the SKUs (stock items) that are to be sold. A new stock location needs to be associated with an address. If the address is different from the merchant's one, then you should create a new address following the same method above and use the new address ID here. To keep it simple, we’ll use the merchant’s address.
The following command creates a new stock location and associates it with an address:
An inventory model defines a list of stock locations ordered by priority. The priority determines how the availability of SKUs gets calculated within a market. If an order contains line items from two or more stock locations, the order can be split into two or more shipments, according to the selected inventory strategy.
The following command creates a new inventory model:
Inventory stock locations build a hierarchy of stock locations within an inventory model. In cases where an SKU is available in more stock locations, it gets shipped from the one with the highest priority. You can decide to put the associated shipments on hold if fulfilled from a specific stock location (false by default). This is useful to manage use cases like back-orders, pre-orders, or personalized orders that need to be customized before being fulfilled. To create a new inventory stock location, you need to associate it with a stock location and an inventory model. We will use the ones we created earlier.
The following command creates a new inventory stock location and associates it with a stock location and an inventory model:
Price lists determine the SKU prices and their currency within a market. When you create a price list you have to specify the related currency and you can decide whether the associated prices include taxes or not (by default taxes are included).
The following command creates a new price list associated with the European currency, with tax included (default):
A market is an aggregation of business rules, often determined by a geographical region where merchants can sell items and customers can purchase. An organization may have one or more markets associated with a merchant, inventory model, SKUs, price lists, stock items, customer groups, and more. When you create a new market you need at least to associate it with a merchant, an inventory model, and a price list. We will use the ones we created earlier.
The following command creates a new market and associate it list associates it with a merchant, an inventory model, and a price list:
Shipping categories determine which shipping methods are available for the associated SKUs. With this, an order will be split into multiple shipments if it contains line items belonging to more than one shipping category.
The following command creates a new shipping category:
Shipping zones determine the available shipping method(s) for a given shipping address. The match is evaluated against a set of regular expressions on the address (country, state, or zip code) of a customer. Now let’s create a new shipping zone to match Italy (IT), Spain (ES), Germany (DE), France (FR), and the United Kingdom (UK).
The following command creates a new shipping zone to match Italy (IT), Spain (ES), Germany (DE), France (FR), and the United Kingdom (UK):
Shipping methods are used to provide customers with delivery options. Each shipping method can have a price and can be free if the order total is over a certain amount or based on some specific promotion rules. When you create a new shipping method you need to specify its cost (amount) and the currency code. If you associate the shipping method with a market, the currency will be inherited from the market's price list. You need also to associate it with a shipping zone and a shipping category. We will use the previously created ones.
The following command creates a new shipping method and associates it with a market, a shipping zone, and a shipping category.
On successful run, the newly created shipping method object information is prompted. You can see that the currency code (EUR) has been inherited by the market's price list:
Payment gateways allow users to process payments through Commerce Layer's API. By default, we currently support Adyen, Braintree, Checkout.com, Klarna, PayPal, and Stripe. Manual gateways are also available (useful to process payments through wire transfer, cash, and other kinds of manual payment options). On top of that, external gateways let you integrate any payment service that is not available out-of-the-box (even your custom one). You can check the API references of each of them to understand the attributes/relationships required for their creation. For the purpose of this guide, we will create a manual gateway
The following command creates a new manual payment gateway:
You can use the command cl resources:list payment_gateways to list all the payment gateway(s) created or cl resources:list <gateway_type>_gateways to list only the specified ’s payment gateways created.
Payment methods represent the type of payment sources (e.g. credit card, PayPal, or Apple Pay) offered in a market. They can have a price and must be present before placing an order. By default, we currently support the following payment source types: Adyen payment, Braintree payment, Checkout.com payment, credit card, PayPal payment, Stripe payment, external payment, or wire transfer — based on the payment gateway to be associated with. The manual payment gateway only accepts the wire transfer payment source type and that's the one we will use to create the payment method, associated with the previously created payment gateway.
The following command creates a new payment method and associates it with a manual payment gateway and a wire transfer payment source:
SKUs describe specific product variations that are being sold. To create an SKU you need to associate it with a shipping category that determines the available shipping options for that SKU. We will use the previously created one. You can add any additional optional attribute that fits your needs.
The following command created a new SKU and associates it with a shipping category:
clresources:createskus-r \shipping_category="MWjgYFbYVK"-a \code="SKUCODE0001XLXX" \name="Grey Aeron Ergonomic Office Chair" \description="Aeron office chair is a revolutionized office seating with its defining design qualities and its patented PostureFit SL back support..." \image_url="https://images.hermanmiller.group/m/7b7b8463a1433d26/W-REK_18179_20160811174200565.png" \reference="SKUCODE0001" \do_not_ship="false" \do_not_track="false" \weight="500" \unit_of_weight="gr"
On successful run, the newly created SKU object information is prompted:
{ id:'BorzSOKrYq', type:'skus', code:'SKUCODE0001XLXX', name:'Grey Aeron Ergonomic Office Chair', description:'Aeron office chair is a revolutionized office seating with its defining design qualities and its patented PostureFit SL back support...', image_url:'https://images.hermanmiller.group/m/7b7b8463a1433d26/W-REK_18179_20160811174200565.png', pieces_per_pack:null, weight:500, unit_of_weight:'gr', hs_tariff_number:null, do_not_ship:false, do_not_track:false, created_at:'2022-03-17T14:54:33.123Z', updated_at:'2022-03-17T14:54:33.123Z', reference:'SKUCODE0001', reference_origin:null, metadata: {}, shipping_category: { id:'MWjgYFbYVK', type:'shipping_categories', name:'Demo Shipping Category', created_at:'2022-03-17T14:41:06.380Z', updated_at:'2022-03-17T14:41:06.380Z', reference:null, reference_origin:null, metadata: {} }}
A stock item keeps the available inventory of an SKU in a given stock location. When you create a stock item, you must specify its quantity, and the associated SKU must be available in one of the market's stock locations.
The following command creates a new stock item for a given SKU and associates it with a stock location:
Prices are the defined cost price of an SKU associated with a currency and price list. When a customer purchases an SKU, it gets the price associated with the order's price list for that market.
When you create a price, you must specify the amount and the compare-at amount. The currency is defined by the associated price list.
The following command creates a price for a given SKU and associates it with a price list:
Commerce Layer lets you import multiple resources (e.g. orders, coupons, SKUs, prices, stock items, customers, and more) in batches directly with our imports API. All you have to do is create a new import resource, specify the resource type you want to import, and populate the inputs attribute with a JSON list of items. Each element of the inputs array contains the resource attributes and relationships. You can learn more about importing resources in our documentation.
To do that using the CLI, first you need to install the imports plugin:
clplugins:installimports
Then create a JSON file named skus.json with an array containing the list of additional SKUs you want to import:
[ {"code":"BEACHBAGFFFFFF000000XXXX","name":"White Beach Bag with Black Logo","description":"Meet your companion for a sunny summer day — our beach bag! It's large, comfy, and you can match it with our towel to create the perfect beach combo.","image_url":"https://data.commercelayer.app/seed/images/skus/BEACHBAGFFFFFF000000XXXX_FLAT.png","reference":"BEACHBAGFFFFFF000000","shipping_category_id":"MWjgYFbYVK" }, {"code":"BEANIEXX000000FFFFFFXXXX","name":"Black Beanie with White Logo","description":"Soft double-layered customizable beanie. 95% polyester, 5% spandex. Regular fit. Accurately printed, cut, and hand-sewn.","image_url":"https://data.commercelayer.app/seed/images/skus/BEANIEXX000000FFFFFFXXXX_FLAT.png","reference":"BEANIEXX000000FFFFFF","shipping_category_id":"MWjgYFbYVK" }]
Also, create a JSON file named stock_items.json with an array containing the list of the related stock items:
If you run into any error, you can run cl imports:details <ID> -l to view the error log for that import process using the process ID.
Create an order
An order consists of a customer, some line items (SKUs, shipping method, payment method, taxes, etc.), a billing address, a shipping address, discount(s) calculated from active promotions, redeemed gift card(s), a payment method, and a payment source type. The sections below will walk you through the process of creating an order by associating it with the required resources, step by step.
Remember that to place and checkout an order you can also leverage our hosted checkout application (automatically enabled for every Commerce Layer account) — if you want to learn more about how to generate a checkout URL using our CLI, feel free to skip the manual steps below and jump directly here.
Search for the ID of the SKU you intend to purchase
You can find the ID of the SKU you want to purchase in the dashboard. Alternatively, you can use the CLI:
The following command will search for all SKUs and filter out the results:
On successful run, an array of SKUs that match the search query is prompted:
[ { id:'WKjRSOlEOP', type:'skus', code:'TSHIRTMSB0B0B2000000LXXX', name:'Men's Gray T-Shirt with Black Logo (L)', description: "This is THE classic t-shirt. Its fine jersey cotton construction makes it extremely soft and comfortable to wear, so it's a good choice for a day-to-day outfit — it's durable and can withstand several washings while retaining its shape and color.", image_url:'https://data.commercelayer.app/seed/images/skus/TSHIRTMSB0B0B2000000LXXX_FLAT.png', pieces_per_pack:null, weight:null, unit_of_weight:null, hs_tariff_number:null, do_not_ship:false, do_not_track:false, created_at:'2022-02-22T04:13:02.232Z', updated_at:'2022-02-22T04:13:02.232Z', reference:'sku_219', reference_origin:'CLI', metadata: {} }, { id:'WJoqSjvNjw', type:'skus', code:'TSHIRTMSB0B0B2000000MXXX', name:'Men's Gray T-Shirt with Black Logo (M)', description: "This is THE classic t-shirt. Its fine jersey cotton construction makes it extremely soft and comfortable to wear, so it's a good choice for a day-to-day outfit — it's durable and can withstand several washings while retaining its shape and color.", image_url:'https://data.commercelayer.app/seed/images/skus/TSHIRTMSB0B0B2000000MXXX_FLAT.png', pieces_per_pack:null, weight:null, unit_of_weight:null, hs_tariff_number:null, do_not_ship:false, do_not_track:false, created_at:'2022-02-22T04:13:05.219Z', updated_at:'2022-02-22T04:13:05.219Z', reference:'sku_220', reference_origin:'CLI', metadata: {} }, { id:'WaDeSyLbyg', type:'skus', code:'TSHIRTMSB0B0B2000000SXXX', name:'Men's Gray T-Shirt with Black Logo (S)', description: "This is THE classic t-shirt. Its fine jersey cotton construction makes it extremely soft and comfortable to wear, so it's a good choice for a day-to-day outfit — it's durable and can withstand several washings while retaining its shape and color.", image_url:'https://data.commercelayer.app/seed/images/skus/TSHIRTMSB0B0B2000000SXXX_FLAT.png', pieces_per_pack:null, weight:null, unit_of_weight:null, hs_tariff_number:null, do_not_ship:false, do_not_track:false, created_at:'2022-02-22T04:13:08.246Z', updated_at:'2022-02-22T04:13:08.246Z', reference:'sku_221', reference_origin:'CLI', metadata: {} }, {... }]
Search for the ID of the market you intend to purchase from
You can find the ID of the market you want to purchase from in the dashboard. Alternatively, you can use the CLI (feel free to filter out the results as shown in the previous section if the list of your markets is too long):
The following command will search for all existing markets:
clresources:getmarkets
On successful run, an array of existing markets is prompted:
Now you can add the SKU to the newly created order by associating the related line item. Use the quantity attribute to add multiple units of the same product:
The following command creates a line item and associates it with an order and an SKU:
You can associate different addresses for shipping and billing. All you need to do is create another address as described in the previous step and associate the ID here.
Get the order shipments and the available shipping methods
To choose a shipping method for the shipment(s) associated with your order, you need first to check what are the available ones:
The following command fetches the shipments associated with an order and all the available shipping methods:
You can create a checkout link using our hosted checkout application. With this, you can checkout an order in a web browser. To get started, you need to install the checkout plugin using the command below:
clplugins:installcheckout
You can use the cl checkout --help command to see the list of available commands and what they do.
Only a sales channel access token can be used to generate a checkout URL. So, use the command below to log with your previously created sales channel API credentilas (similar to what you did before):
The -S flag is required for sales channels and allows you to pass in your application scope as a parameter.
If you have already logged in, you can just switch to the sales channel API credentials using the command below:
clapp:switch-a<applicationAlias>
Place an order with the order ID
If you have the ID of an order ready to be placed, you can generate a valid checkout URL like so:
The following command creates a checkout URL associated with an order:
clcheckout-O<order-id>
The checkout URL is returned:
Checkout URL for order wjobhWrZka:
https://cake-store.checkout.commercelayer.app/wjobhWrZka?accessToken=eyJhbGciOiJIUzUxMiJ9.eyJvcmdhbml6YXRpb24iOnsiaWQiOiJFbnBWT0ZyS3Z5Iiwic2x1ZyI6ImNha2Utc3RvcmUiLCJlbnRlcnByaXNlIjpmYWxzZX0sImFwcGxpY2F0aW9uIjp7ImlkIjoid05iam1pd0JvTiIsImtpbmQiOiJzYWxlc19jaGFubmVsIiwicHVibGljIjp0cnVlfSwidGVzdCI6dHJ1ZSwiZXhwIjoxNjk2MzkxMTMwLCJtYXJrZXQiOnsiaWQiOlsiWWxxeEdoS3JRZyJdLCJwcmljZV9saXN0X2lkIjoid2xacnZDWXJLbCIsInN0b2NrX2xvY2F0aW9uX2lkcyI6WyJ6bkJqeHVsTFZuIiwiWmtZcVh1ZFZkayJdLCJnZW9jb2Rlcl9pZCI6bnVsbCwiYWxsb3dzX2V4dGVybmFsX3ByaWNlcyI6ZmFsc2V9LCJyYW5kIjowLjExNTQ0NjUyOTM2MjgzMn0.Bh-DuqgqgxLHy8QhdcTj8rf2SS3xeu5NECyZSWsJw3ZwTyVqB5U9oSx3tki2vwFbAyxcLLHv-U69nCWqy2FbtA
Checkout one or more SKUs
Alternatively, you can add new items to an order or directly create a new one and then place it, like so:
The following command creates an order and the related line items, associated with one or more SKUs. It also creates a checkout URL associated with the order:
The created order ID, line items ID, and checkout URL are returned:
Created order wnlKhYmZEE
Created line item NMlZtWKbKw for SKU BEACHBAG000000FFFFFFXXXX and associated to order wnlKhYmZEE
Created line item yeYRtdQmQE for SKU 5PANECAP000000FFFFFFXXXX and associated to order wnlKhYmZEE
Created line item vbmetaMdMP for SKU BACKPACKFFFFFF000000XXXX and associated to order wnlKhYmZEE
Checkout URL for order wnlKhYmZEE:
https://cake-store.checkout.commercelayer.app/wnlKhYmZEE?accessToken=eyJhbGciOiJIUzUxMiJ9.eyJvcmdhbml6YXRpb24iOnsiaWQiOiJFbnBWT0ZyS3Z5Iiwic2x1ZyI6ImNha2Utc3RvcmUiLCJlbnRlcnByaXNlIjpmYWxzZX0sImFwcGxpY2F0aW9uIjp7ImlkIjoid05iam1pd0JvTiIsImtpbmQiOiJzYWxlc19jaGFubmVsIiwicHVibGljIjp0cnVlfSwidGVzdCI6dHJ1ZSwiZXhwIjoxNjk2MzkxMTMwLCJtYXJrZXQiOnsiaWQiOlsiWWxxeEdoS3JRZyJdLCJwcmljZV9saXN0X2lkIjoid2xacnZDWXJLbCIsInN0b2NrX2xvY2F0aW9uX2lkcyI6WyJ6bkJqeHVsTFZuIiwiWmtZcVh1ZFZkayJdLCJnZW9jb2Rlcl9pZCI6bnVsbCwiYWxsb3dzX2V4dGVybmFsX3ByaWNlcyI6ZmFsc2V9LCJyYW5kIjowLjExNTQ0NjUyOTM2MjgzMn0.Bh-DuqgqgxLHy8QhdcTj8rf2SS3xeu5NECyZSWsJw3ZwTyVqB5U9oSx3tki2vwFbAyxcLLHv-U69nCWqy2FbtA
Here’s a breakdown of each of the command options:
The checkout command uses the installed checkout plugin.
The -O flag allows you to pass in an order ID.
The -S flag allows you to pass in an SKU code.
The -m flag allows you to pass in a market ID.
The -e flag allows you to pass in a customer email.
The -c flag allows you to pass in a coupon code.
The -B flag allows you to pass in a bundle code.
Further resources
Now that your first organization is properly configured and you managed to place your first order, you can take the time to explore the rest of this getting started section, check the API reference, have a look at our recipe-like how-tos, and become a pro!
Do you need to extract some kind of data information from your Commerce Layer's organization? We've got you covered. Check our Metrics API and see how it can enable you to gather and aggregate useful data that you can leverage to measure the health and performance of your ecommerce business.
If you want to exercise greater control over your organizational structure and user roles and be able to manage provisioning tasks programmatically instead of manually, leverage our Provisioning API.
For everything else, join us on Slack and feel free to ask any questions to the team or get involved in the conversation with the rest of our developers' community.