• Time to read 1 minute
// Load your service class
var service = require('~/cartridge/scripts/services/myService');

// Create an instance of your service 
var serviceInstance = service['sandbox']();

// Prepare request data
var requestData = {
    /* Optional parameters for the request */
};

// Get the result of the HTTP request
var result = serviceInstance.call(requestData);

// Response from your external API
// console.log(result);

In order to send external HTTP requests from a Salesforce Commerce Cloud cartridge, implementing the Services Framework is required. 

The Services Framework allows to send external calls in a safe way, while improving application maintainability and testability.

1 - Define services

In the services.xml file which is part of the metadata to be imported into your site, define your services. Example of services.xml file:

<!-- Service  -->
<service service-id="your.service.id">
    <service-type>HTTP</service-type>
    <enabled>true</enabled>
    <log-prefix/>
    <comm-log-enabled>false</comm-log-enabled>
    <profile-id>your.service.profile.id</profile-id>
    <credential-id>your.service.credential.id</credential-id>
</service>

<!-- Service credentials -->
<service-credential service-credential-id="your.service.credential.id">
    <url>https://url-to-your-api</url>
    <user-id>user_id</user-id>
    <password>user_password</password>
</service-credential>

<!-- Service profile -->
<service-profile service-profile-id="your.service.profile.id">
    <timeout-millis>10000</timeout-millis>
    <rate-limit-enabled>false</rate-limit-enabled>
    <rate-limit-calls>0</rate-limit-calls>
    <rate-limit-millis>0</rate-limit-millis>
    <cb-enabled>false</cb-enabled>
    <cb-calls>0</cb-calls>
    <cb-millis>0</cb-millis>
</service-profile>

 

2 - Register services

Create a services.js file somewhere in your project. For example, in scripts/services.js. Put the following content and adjust the internal logic to the context of your application:

'use strict';

/* API Includes */
var LocalServiceRegistry = require('dw/svc/LocalServiceRegistry');

/**
    * Your service wrapper.
    */
var wrapper = {
    /**
        * Initialize HTTP service.
        * @returns {Object} The service instance
        */
    initMyService: function() {
        return LocalServiceRegistry.createService('your.service.id', {
            createRequest: function(svc, args) {
                // Prepare the http service
                svc.addHeader('Authorization', 'your_authorization_header');
                svc.addHeader('User-Agent', 'your_authorization_header');
                svc.addHeader('Content-Type', 'application/json;charset=UTF-8');

                return (args) ? JSON.stringify(args) : null;
            },

            parseResponse: function(svc, resp) {
                return JSON.parse(resp.text);
            },
        });
    },
};

/**
    * Module exports
    */
module.exports = wrapper;

 

3 - Call services

In order to perform an external HTTP request with your new service, use code similar to the below:

// Load your service class
var service = require('~/cartridge/scripts/services/myService');

// Create an instance of your service 
var serviceInstance = service.initMyService();

// Prepare request data
var requestData = {
    /* Optional parameters for the request */
};

// Get the result of the HTTP request
var result = serviceInstance.call(requestData);

// Response from your external API
// console.log(result);

 

This is a short summary of the steps to follow. Have a look at the official Salesforce Commerce Cloud documentation for more details on the Services Framework. Happy trailblazing!