Skip to content

Instantly share code, notes, and snippets.

@JCTucker
Last active April 25, 2024 04:17
Show Gist options
  • Save JCTucker/9854ddd46a2ed4846b5b54bdc5f3c1bd to your computer and use it in GitHub Desktop.
Save JCTucker/9854ddd46a2ed4846b5b54bdc5f3c1bd to your computer and use it in GitHub Desktop.
OCAPI Demo: Postman and BM files
{
"_v": "21.3",
"clients": [{
"client_id": "<API Client ID from Account Manager>",
"resources": [{
"methods": ["get", "post", "put", "patch", "delete"],
"read_attributes": "(**)",
"write_attributes": "",
"resource_id": "/**"
}]
}]
}
{
"id": "3d776f1f-a37f-d348-6b1f-fb78a8753874",
"name": "OCAPI demo environment",
"values": [
{
"key": "host_server",
"value": "<your ODS host, i.e. zzpa-002.sandbox.us01.dx.commercecloud.salesforce.com>",
"enabled": true
},
{
"key": "clientid",
"value": "<clientid from Account Manager>",
"enabled": true
},
{
"key": "api_version",
"value": "v20_10",
"enabled": true
},
{
"key": "site_id",
"value": "RefArch",
"enabled": true
},
{
"key": "shop_url",
"value": "https://{{host_server}}/s/{{site_id}}/dw/shop/{{api_version}}",
"enabled": true
},
{
"key": "oauth2_url",
"value": "https://{{host_server}}/dw/oauth2/access_token",
"enabled": true
},
{
"key": "data_url",
"value": "https://{{host_server}}/s/Sites-Site/dw/data/{{api_version}}",
"enabled": true
},
{
"key": "Authorization_key",
"value": "<generated by script>",
"enabled": true
},
{
"key": "customer_id",
"value": "<generated by script>",
"enabled": true
},
{
"key": "bm_user",
"value": "<BM user in your instance>",
"enabled": true
},
{
"key": "bm_pwd:clientid_pwd",
"value": "<BM user pwd>:<pwd for AM clientid used above>",
"enabled": true
},
{
"key": "clientid_pwd",
"value": "<pwd for AM clientid used above>",
"enabled": true
},
{
"key": "productIds",
"value": "P0048M,682875540326M",
"enabled": true
}
],
"_postman_variable_scope": "environment",
"_postman_exported_at": "2020-10-13T18:22:17.763Z",
"_postman_exported_using": "Postman/7.34.0"
}
{
"info": {
"_postman_id": "ab089780-0e0c-ef26-a2ab-580856a38939",
"name": "OCAPI demo",
"description": "A few useful OCAPI calls",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Shop API Usage",
"item": [
{
"name": "1a. Get OAuth2 client token",
"event": [
{
"listen": "test",
"script": {
"id": "6b6b423b-8a97-4fec-952e-75116a942aac",
"exec": [
"var tokenData = JSON.parse(responseBody);",
"",
"if(tokenData.access_token!==null && tokenData.token_type!==null ){",
" postman.setEnvironmentVariable(\"Authorization_key\", tokenData.token_type + \" \" + tokenData.access_token);",
"}"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "{{clientid_pwd}}",
"type": "string"
},
{
"key": "username",
"value": "{{clientid}}",
"type": "string"
},
{
"key": "saveHelperData",
"value": true,
"type": "boolean"
},
{
"key": "showPassword",
"value": true,
"type": "boolean"
}
]
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/x-www-form-urlencoded",
"type": "text"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "client_credentials",
"type": "text"
}
]
},
"url": {
"raw": "https://account.demandware.com/dw/oauth2/access_token",
"protocol": "https",
"host": [
"account",
"demandware",
"com"
],
"path": [
"dw",
"oauth2",
"access_token"
]
}
},
"response": []
},
{
"name": "1b. Get Categories",
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
}
],
"url": {
"raw": "{{shop_url}}/categories/root?levels=2",
"host": [
"{{shop_url}}"
],
"path": [
"categories",
"root"
],
"query": [
{
"key": "levels",
"value": "2"
}
]
}
},
"response": []
},
{
"name": "2a. Get Guest customer auth",
"event": [
{
"listen": "test",
"script": {
"id": "5a13d28f-b4e3-4b40-b796-bd1834e001b2",
"exec": [
"postman.setEnvironmentVariable(\"Authorization_key\", postman.getResponseHeader(\"Authorization\"));",
""
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"type\" : \"guest\"\n}"
},
"url": {
"raw": "{{shop_url}}/customers/auth",
"host": [
"{{shop_url}}"
],
"path": [
"customers",
"auth"
]
}
},
"response": []
},
{
"name": "2b. Get Products",
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
}
],
"url": {
"raw": "{{shop_url}}/products/({{productIds}})",
"host": [
"{{shop_url}}"
],
"path": [
"products",
"({{productIds}})"
]
}
},
"response": []
},
{
"name": "3a. Get Registered customer auth",
"event": [
{
"listen": "test",
"script": {
"id": "575e4989-9c64-4a2a-ac04-f17cf5b97ab2",
"exec": [
"postman.setEnvironmentVariable(\"Authorization_key\", postman.getResponseHeader(\"Authorization\"));",
"",
"var customerData = JSON.parse(responseBody);",
"",
"if(customerData.customer_id!==null){",
" postman.setEnvironmentVariable(\"customer_id\", customerData.customer_id);",
"}"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "!20Summer",
"type": "string"
},
{
"key": "username",
"value": "legend@sleepyhollow.com",
"type": "string"
},
{
"key": "saveHelperData",
"value": true,
"type": "boolean"
},
{
"key": "showPassword",
"value": true,
"type": "boolean"
}
]
},
"method": "POST",
"header": [
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"type\" : \"credentials\"\n}"
},
"url": {
"raw": "{{shop_url}}/customers/auth",
"host": [
"{{shop_url}}"
],
"path": [
"customers",
"auth"
]
}
},
"response": []
},
{
"name": "3b. Get Registered customer address",
"event": [
{
"listen": "test",
"script": {
"id": "8683fca8-8668-4637-89af-21474bc6ee63",
"exec": [
"pm.test(\"Response Format: JSON\", function () {",
" var jsonData = pm.response.json();",
"});"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "Origin",
"value": "{{origin_url}}"
},
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
},
{
"key": "Content-Type",
"value": "application/json"
},
{
"key": "Authorization",
"value": "{{Authorization_key}}"
}
],
"url": {
"raw": "{{shop_url}}/customers/{{customer_id}}",
"host": [
"{{shop_url}}"
],
"path": [
"customers",
"{{customer_id}}"
]
}
},
"response": []
},
{
"name": "3c. Get Order (order must exist in BM)",
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "x-dw-client-id",
"value": "{{clientid}}"
},
{
"key": "Authorization",
"value": "{{Authorization_key}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": {
"raw": "{{shop_url}}/orders/00000001",
"host": [
"{{shop_url}}"
],
"path": [
"orders",
"00000001"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "24f2304f-2f87-4533-8f22-54b482601c1c",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "4691f912-9c62-45fb-8b21-60822c18eea2",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
},
{
"name": "Data API Usage",
"item": [
{
"name": "4a. Get BM OAuth2 token (need BM user for this)",
"event": [
{
"listen": "test",
"script": {
"id": "281d76d2-e377-430b-9fa8-b125248d295f",
"exec": [
"var tokenData = JSON.parse(responseBody);",
"",
"if(tokenData.access_token!==null && tokenData.token_type!==null ){",
" postman.setEnvironmentVariable(\"Authorization_key\", tokenData.token_type + \" \" + tokenData.access_token);",
"}"
],
"type": "text/javascript"
}
}
],
"request": {
"auth": {
"type": "basic",
"basic": [
{
"key": "password",
"value": "{{bm_pwd:clientid_pwd}}",
"type": "string"
},
{
"key": "username",
"value": "{{bm_user}}",
"type": "string"
},
{
"key": "saveHelperData",
"value": true,
"type": "boolean"
},
{
"key": "showPassword",
"value": true,
"type": "boolean"
}
]
},
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/x-www-form-urlencoded"
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "urn:demandware:params:oauth:grant-type:client-id:dwsid:dwsecuretoken",
"type": "text"
}
]
},
"url": {
"raw": "https://{{host_server}}/dw/oauth2/access_token?client_id={{clientid}}",
"protocol": "https",
"host": [
"{{host_server}}"
],
"path": [
"dw",
"oauth2",
"access_token"
],
"query": [
{
"key": "client_id",
"value": "{{clientid}}"
}
]
}
},
"response": []
},
{
"name": "4b. Get Site (RefArch expected in BM)",
"request": {
"auth": {
"type": "noauth"
},
"method": "GET",
"header": [
{
"key": "Authorization",
"value": "{{Authorization_key}}"
},
{
"key": "Origin",
"value": "{{origin_url}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"url": {
"raw": "{{data_url}}/sites/{{site_id}}",
"host": [
"{{data_url}}"
],
"path": [
"sites",
"{{site_id}}"
]
}
},
"response": []
},
{
"name": "4c. Search Custom Object (must have instances of NewsletterSubscription in BM)",
"request": {
"auth": {
"type": "noauth"
},
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "{{Authorization_key}}"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"query\" : {\n \"bool_query\":{\n \"must\":[\n { \"term_query\": { \"fields\": [\"site_id\"], \"operator\": \"is\", \"values\": [\"RefArch\"] } }\n ]\n }\n },\n \"select\" : \"(**)\"\n}"
},
"url": {
"raw": "{{data_url}}/custom_objects_search/NewsletterSubscription",
"host": [
"{{data_url}}"
],
"path": [
"custom_objects_search",
"NewsletterSubscription"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "7c3b8f81-9970-48dc-987a-5ea498048fc7",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "c8d0f762-0827-488b-9cbf-d688c4c6df40",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "79c7e511-2673-4ea7-ba3a-2382370ba1a3",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "b060be7c-99c5-4e00-8e3b-78a23a0dc019",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
}
This gist contains 4 files:
- Postman environment variables for OCAPI
- Postman collection for OCAPI
- Shop API settings for BM
- Data API settings for BM
{
"_v": "21.3",
"clients": [
{
"client_id": "<API Client ID from Account Manager>",
"resources": [
{
"resource_id": "/sessions",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*",
"methods": ["get", "patch", "delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/billing_address",
"methods": ["put"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/coupons",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/coupons/*",
"methods": ["delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/customer",
"methods": ["put"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/gift_certificate_items",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/gift_certificate_items/*",
"methods": ["delete", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/items",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/items/*",
"methods": ["delete", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/payment_instruments",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/payment_instruments/*",
"methods": ["delete", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/payment_methods",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/shipments",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/shipments/*",
"methods": ["delete", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/shipments/*/shipping_address",
"methods": ["put"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/shipments/*/shipping_method",
"methods": ["put"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/baskets/*/shipments/*/shipping_methods",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/orders",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/orders/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/orders/*/payment_instruments",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/orders/*/payment_instruments/*",
"methods": ["delete", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/orders/*/payment_methods",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/auth",
"methods": ["post", "delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/auth/trustedsystem",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*",
"methods": ["get", "patch"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/addresses",
"methods": ["get", "post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/addresses/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/baskets",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/orders",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/password",
"methods": ["put"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/payment_instruments",
"methods": ["get", "post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/payment_instruments/*",
"methods": ["get", "delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/product_lists",
"methods": ["get", "post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/product_lists/*",
"methods": ["get", "patch", "delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/product_lists/*/items",
"methods": ["get", "post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/customers/*/product_lists/*/items/*",
"methods": ["get", "patch", "delete"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/product_lists",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/product_lists/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/product_lists/*/items",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/product_lists/*/items/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/product_search",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/search_suggestion",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/products/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/categories/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/gift_certificate",
"methods": ["post"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/promotions",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/promotions/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/stores",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/stores/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/content/*",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
},
{
"resource_id": "/content_search",
"methods": ["get"],
"read_attributes": "(**)",
"write_attributes": "(**)"
}
]
}
]
}
@JCTucker
Copy link
Author

Use this collection in Postman to recreate some of the examples shown during the Fast Path to B2C Commerce.
This gist contains:

  • Postman collection
  • Postman environment
  • OCAPI SHOP API settings for BM
  • OCAPI DATA API settings for BM

Changes needed:

@IvanMollOSF
Copy link

On OCAPI DATA config there is a typo, on write_attributes you have to add (**)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment