SQL Server / Commercetools / Create Product
Back to Collection Items
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
-- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
-- Use this online tool to generate code from sample JSON: Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "productType": {
-- "id": "24f510c3-f334-4099-94e2-d6224a8eb919",
-- "typeId": "product-type"
-- },
-- "categories": [
-- {
-- "typeId": "category",
-- "id": "24f510c3-f334-4099-94e2-d6224a8eb919"
-- }
-- ],
-- "name": {
-- "en": "Some Product"
-- },
-- "slug": {
-- "en": "product_slug_<random-uuid>"
-- },
-- "masterVariant": {
-- "sku": "SKU-1",
-- "prices": [
-- {
-- "value": {
-- "currencyCode": "EUR",
-- "centAmount": 4200
-- }
-- }
-- ],
-- "images": [
-- {
-- "url": "http://my.custom.cdn.net/master.png",
-- "label": "Master Image",
-- "dimensions": {
-- "w": 303,
-- "h": 197
-- }
-- }
-- ]
-- },
-- "variants": [
-- {
-- "images": [
-- {
-- "url": "http://my.custom.cdn.net/variant.png",
-- "label": "Variant Image",
-- "dimensions": {
-- "w": 303,
-- "h": 197
-- }
-- }
-- ]
-- }
-- ]
-- }
DECLARE @json int
-- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'productType.id', '24f510c3-f334-4099-94e2-d6224a8eb919'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'productType.typeId', 'product-type'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'categories[0].typeId', 'category'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'categories[0].id', '24f510c3-f334-4099-94e2-d6224a8eb919'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name.en', 'Some Product'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'slug.en', 'product_slug_<random-uuid>'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'masterVariant.sku', 'SKU-1'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'masterVariant.prices[0].value.currencyCode', 'EUR'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'masterVariant.prices[0].value.centAmount', 4200
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'masterVariant.images[0].url', 'http://my.custom.cdn.net/master.png'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'masterVariant.images[0].label', 'Master Image'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'masterVariant.images[0].dimensions.w', 303
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'masterVariant.images[0].dimensions.h', 197
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'variants[0].images[0].url', 'http://my.custom.cdn.net/variant.png'
EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'variants[0].images[0].label', 'Variant Image'
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'variants[0].images[0].dimensions.w', 303
EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'variants[0].images[0].dimensions.h', 197
-- Adds the "Authorization: Bearer <access_token>" header.
EXEC sp_OASetProperty @http, 'AuthToken', '<access_token>'
EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'
DECLARE @sbRequestBody int
-- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbRequestBody OUT
EXEC sp_OAMethod @json, 'EmitSb', @success OUT, @sbRequestBody
DECLARE @resp int
EXEC sp_OAMethod @http, 'PTextSb', @resp OUT, 'post', 'https://domain.com/{{project-key}}/products', @sbRequestBody, 'utf-8', 'application/json', 0, 0
EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbRequestBody
RETURN
END
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT @iTmp0
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @json
EXEC @hr = sp_OADestroy @sbRequestBody
END
GO
Curl Command
curl -X post
-H "Authorization: Bearer <access_token>"
-H "Content-Type: application/json"
-d '{
"productType" : {
"id" : "24f510c3-f334-4099-94e2-d6224a8eb919",
"typeId" : "product-type"
},
"categories" : [ {
"typeId" : "category",
"id" : "24f510c3-f334-4099-94e2-d6224a8eb919"
} ],
"name" : {
"en" : "Some Product"
},
"slug" : {
"en" : "product_slug_<random-uuid>"
},
"masterVariant" : {
"sku" : "SKU-1",
"prices" : [ {
"value" : {
"currencyCode" : "EUR",
"centAmount" : 4200
}
} ],
"images" : [ {
"url" : "http://my.custom.cdn.net/master.png",
"label" : "Master Image",
"dimensions" : {
"w" : 303,
"h" : 197
}
} ]
},
"variants" : [ {
"images" : [ {
"url" : "http://my.custom.cdn.net/variant.png",
"label" : "Variant Image",
"dimensions" : {
"w" : 303,
"h" : 197
}
} ]
} ]
}'
https://domain.com/{{project-key}}/products
Postman Collection Item JSON
{
"name": "Create Product",
"event": [
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
"tests[\"Status code \" + responseCode.code] = responseCode.code === 200 || responseCode.code === 201;",
"var data = JSON.parse(responseBody);",
"if(data.results && data.results[0] && data.results[0].id && data.results[0].version){",
" pm.environment.set(\"product-id\", data.results[0].id); ",
" pm.environment.set(\"product-version\", data.results[0].version);",
"}",
"if(data.results && data.results[0] && data.results[0].key){",
" pm.environment.set(\"product-key\", data.results[0].key); ",
"}",
"if(data.version){",
" pm.environment.set(\"product-version\", data.version);",
"}",
"if(data.id){",
" pm.environment.set(\"product-id\", data.id); ",
"}",
"if(data.key){",
" pm.environment.set(\"product-key\", data.key);",
"}",
""
]
}
}
],
"request": {
"auth": {
"type": "oauth2",
"oauth2": {
"accessToken": "{{ctp_access_token}}",
"addTokenTo": "header",
"tokenType": "Bearer"
}
},
"method": "post",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": {
"raw": "{{host}}/{{project-key}}/products",
"host": [
"{{host}}"
],
"path": [
"{{project-key}}",
"products"
],
"query": [
{
"key": "priceCurrency",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "priceCountry",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "priceCustomerGroup",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "priceChannel",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "localeProjection",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "storeProjection",
"value": "",
"equals": true,
"disabled": true
},
{
"key": "expand",
"value": "",
"equals": true,
"disabled": true
}
]
},
"description": "To create a new product, send a representation that is going to become the initial staged representation\nof the new product in the master catalog. If price selection query parameters are provided,\nthe selected prices will be added to the response.\n",
"body": {
"mode": "raw",
"raw": "{\n \"productType\" : {\n \"id\" : \"24f510c3-f334-4099-94e2-d6224a8eb919\",\n \"typeId\" : \"product-type\"\n },\n \"categories\" : [ {\n \"typeId\" : \"category\",\n \"id\" : \"24f510c3-f334-4099-94e2-d6224a8eb919\"\n } ],\n \"name\" : {\n \"en\" : \"Some Product\"\n },\n \"slug\" : {\n \"en\" : \"product_slug_<random-uuid>\"\n },\n \"masterVariant\" : {\n \"sku\" : \"SKU-1\",\n \"prices\" : [ {\n \"value\" : {\n \"currencyCode\" : \"EUR\",\n \"centAmount\" : 4200\n }\n } ],\n \"images\" : [ {\n \"url\" : \"http:\/\/my.custom.cdn.net\/master.png\",\n \"label\" : \"Master Image\",\n \"dimensions\" : {\n \"w\" : 303,\n \"h\" : 197\n }\n } ]\n },\n \"variants\" : [ {\n \"images\" : [ {\n \"url\" : \"http:\/\/my.custom.cdn.net\/variant.png\",\n \"label\" : \"Variant Image\",\n \"dimensions\" : {\n \"w\" : 303,\n \"h\" : 197\n }\n } ]\n } ]\n}"
}
},
"response": [
]
}