Product page
How to display price and availability on a product page

The Problem

You've created a product page with your favorite CMS and now you want to display the price and the availability of the selected product. You also need to implement the "add to cart" function on the related button.
A sample product page

Solution

To retrieve the price and availability of an SKU, send a GET request to the /api/skus/:id endpoint and include the associated prices.

Example

Request
Response
The following request retrieves the price and the availability of the SKU identified by the "aBmNkPQRst" ID:
1
curl -X GET \
2
https://yourdomain.commercelayer.io/api/skus/aBmNkPQRst?include=prices \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning the SKU object with the price and inventory included:
1
{
2
"data": {
3
"id": "aBmNkPQRst",
4
"type": "skus",
5
"links": {
6
"self": "https://yourdomain.commercelayer.io/api/skus/aBmNkPQRst"
7
},
8
"attributes": {
9
"code": "TSHIRTB5B5B5XL",
10
"name": "Gray T-Shirt XL",
11
"description": "Gray cotton t-shirt. Short sleeves and a round neckline. Unique high quality design.",
12
"image_url": "https://img.yourdomain.com/skus/TSHIRTB5B5B5XL.png",
13
"tag_names": "",
14
"pieces_per_pack": null,
15
"weight": null,
16
"unit_of_weight": null,
17
"hs_tariff_number": null,
18
"do_not_ship": false,
19
"do_not_track": false,
20
"inventory": {
21
"available": true,
22
"quantity": 10,
23
"levels": [
24
{
25
"quantity": 6,
26
"delivery_lead_times": [
27
{
28
"shipping_method": {
29
"name": "Standard Shipping",
30
"reference": null,
31
"price_amount_cents": 700,
32
"free_over_amount_cents": 9900,
33
"formatted_price_amount": "€7,00",
34
"formatted_free_over_amount": "€99,00"
35
},
36
"min": {
37
"hours": 72,
38
"days": 3
39
},
40
"max": {
41
"hours": 120,
42
"days": 5
43
}
44
},
45
{
46
"shipping_method": {
47
"name": "Express Delivery",
48
"reference": null,
49
"price_amount_cents": 1000,
50
"free_over_amount_cents": null,
51
"formatted_price_amount": "€10,00",
52
"formatted_free_over_amount": null
53
},
54
"min": {
55
"hours": 24,
56
"days": 1
57
},
58
"max": {
59
"hours": 48,
60
"days": 2
61
}
62
}
63
]
64
},
65
{
66
"quantity": 4,
67
"delivery_lead_times": [
68
{
69
"shipping_method": {
70
"name": "Standard Shipping",
71
"reference": null,
72
"price_amount_cents": 700,
73
"free_over_amount_cents": 9900,
74
"formatted_price_amount": "€7,00",
75
"formatted_free_over_amount": "€99,00"
76
},
77
"min": {
78
"hours": 72,
79
"days": 3
80
},
81
"max": {
82
"hours": 5,
83
"days": 120
84
}
85
},
86
{
87
"shipping_method": {
88
"name": "Express Delivery",
89
"reference": null,
90
"price_amount_cents": 1000,
91
"free_over_amount_cents": null,
92
"formatted_price_amount": "€10,00",
93
"formatted_free_over_amount": null
94
},
95
"min": {
96
"hours": 24,
97
"days": 1
98
},
99
"max": {
100
"hours": 48,
101
"days": 2
102
}
103
}
104
]
105
}
106
]
107
},
108
"created_at": "2018-01-01T12:00:00.000Z",
109
"updated_at": "2018-01-01T12:00:00.000Z",
110
"reference": null,
111
"metadata": {}
112
},
113
"relationships": {
114
"shipping_category": {
115
"links": {...}
116
},
117
"prices": {
118
"links": {...},
119
"data": [
120
{
121
"type": "prices",
122
"id": "pYwMUWJRzg"
123
}
124
]
125
},
126
"stock_items": {
127
"links": {...}
128
},
129
"delivery_lead_times": {
130
"links": {...}
131
},
132
"sku_options": {
133
"links": {...}
134
}
135
},
136
"meta": {
137
"mode": "test"
138
}
139
},
140
"included": [
141
{
142
"id": "pYwMUWJRzg",
143
"type": "prices",
144
"links": {
145
"self": "https://yourdomain.commercelayer.io/api/prices/pYwMUWJRzg"
146
},
147
"attributes": {
148
"currency_code": "EUR",
149
"sku_code": "TSHIRTB5B5B5XL",
150
"amount_cents": 12900,
151
"amount_float": 129.0,
152
"formatted_amount": "€129,00",
153
"compare_at_amount_cents": 15000,
154
"compare_at_amount_float": 150.0,
155
"formatted_compare_at_amount": "€150,00",
156
"created_at": "2018-01-01T12:00:00.000Z",
157
"updated_at": "2018-01-01T12:00:00.000Z",
158
"reference": null,
159
"metadata": {}
160
},
161
"relationships": {
162
"price_list": {
163
"links": {...}
164
},
165
"sku": {
166
"links": {...}
167
}
168
},
169
"meta": {
170
"mode": "test"
171
}
172
}
173
]
174
}
Copied!
For performance reasons, the inventory information is only returned when fetching a single SKU.
To implement the "add to cart" function, please refer to this guide:

Mapping

The image below shows the three main dynamic elements of the page (selling price, full price and inventory information) and how each of these are mapped to a specific field of the SKU or price objects.
A sample product page mapping

Additional notes

Getting the SKU ID from its code

If you have the SKU code and you want to retrieve the related ID, send a GET request to the /api/skus endpoint and filter it by code, taking care to request the ID field only.
Request
Response
The following request retrieves the ID of the SKU identified by the code "TSHIRTB5B5B5XL":
1
curl -X GET \
2
https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&fields[skus]=id \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning the single SKU object and showing only the ID field:
1
{
2
"data": [
3
{
4
"id": "aBmNkPQRst",
5
"type": "skus",
6
"links": {
7
"self": "https://yourdomain.commercelayer.io/api/skus/aBmNkPQRst"
8
},
9
"meta": {
10
"mode": "test"
11
}
12
}
13
],
14
"meta": {
15
"record_count": 1,
16
"page_count": 1
17
},
18
"links": {
19
"first": "https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&fields[skus]=id&page[number]=1&page[size]=10",
20
"last": "https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&&fields[skus]=id&page[number]=1&page[size]=10"
21
}
22
}
Copied!

Getting a list of SKU prices

If you need to display the prices associated with a list of SKUs (with no inventory information) send a GET request to the /api/skus endpoint, filter it by code, and include the SKU prices.
Request
Response
The following request retrieves the prices of the SKU identified by the "TSHIRTB5B5B5XL" code:
1
curl -X GET \
2
https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTB5B5B5XL&include=prices \
3
-H 'Accept: application/vnd.api+json' \
4
-H 'Authorization: Bearer your-access-token'
Copied!
On success, the API responds with a 200 OK status code, returning the SKU object with the price included:
1
{
2
"data": [
3
{
4
"id": "aBmNkPQRst",
5
"type": "skus",
6
"links": {
7
"self": "https://yourdomain.commercelayer.io/api/skus/aBmNkPQRst"
8
},
9
"attributes": {
10
"code": "TSHIRTB5B5B5XL",
11
"name": "Gray T-Shirt XL",
12
"description": "Gray cotton t-shirt. Short sleeves and a round neckline. Unique high quality design.",
13
"image_url": "https://img.yourdomain.com/skus/TSHIRTB5B5B5XL.png",
14
"tag_names": "",
15
"pieces_per_pack": null,
16
"weight": null,
17
"unit_of_weight": null,
18
"hs_tariff_number": null,
19
"do_not_ship": false,
20
"do_not_track": false,
21
"created_at": "2018-01-01T12:00:00.000Z",
22
"updated_at": "2018-01-01T12:00:00.000Z",
23
"reference": null,
24
"metadata": {}
25
},
26
"relationships": {
27
"shipping_category": {
28
"links": {...}
29
},
30
"prices": {
31
"links": {...},
32
"data": [
33
{
34
"type": "prices",
35
"id": "pYwMUWJRzg"
36
}
37
]
38
},
39
"stock_items": {
40
"links": {...}
41
},
42
"delivery_lead_times": {
43
"links": {...}
44
},
45
"sku_options": {
46
"links": {...}
47
}
48
},
49
"meta": {
50
"mode": "test"
51
}
52
}
53
],
54
"included": [
55
{
56
"id": "pYwMUWJRzg",
57
"type": "prices",
58
"links": {
59
"self": "https://yourdomain.commercelayer.io/api/prices/pYwMUWJRzg"
60
},
61
"attributes": {
62
"currency_code": "EUR",
63
"sku_code": "TSHIRTB5B5B5XL",
64
"amount_cents": 12900,
65
"amount_float": 129.0,
66
"formatted_amount": "€29,00",
67
"compare_at_amount_cents": 15000,
68
"compare_at_amount_float": 150.0,
69
"formatted_compare_at_amount": "€150,00",
70
"created_at": "2018-01-01T12:00:00.000Z",
71
"updated_at": "2018-01-01T12:00:00.000Z",
72
"reference": null,
73
"metadata": {}
74
},
75
"relationships": {
76
"price_list": {
77
"links": {...}
78
},
79
"sku": {
80
"links": {...}
81
}
82
},
83
"meta": {
84
"mode": "test"
85
}
86
}
87
],
88
"meta": {
89
"record_count": 1,
90
"page_count": 1
91
},
92
"links": {
93
"first": "https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTMM000000E63E74LXXX&include=prices&page[number]=1&page[size]=10",
94
"last": "https://yourdomain.commercelayer.io/api/skus?filter[q][code_eq]=TSHIRTMM000000E63E74LXXX&include=prices&page[number]=1&page[size]=10"
95
}
96
}
Copied!

More to read

See our API reference if you need more information on how to filter data, include associations, and manage sparse fieldsets or if you want to have an overview of all the specific permissions and restrictions set for a sales channel application.
Last modified 4mo ago