ANSWERED

Extension > Calling Custom Functions from Retail or Web Office code

From Russell: Calling custom-created Extension functions from Retail and Web Office can be tricky, but it's not bad when you know what's happening. This interaction I think will help clear things up, and I've added a couple of notes below.  Please note that there are two proxy pathways used in the Retail/Web Office API space, "Proxy" (for logged-in users), and "AnonymousProxy" (for anonymous users). The "AnonymousProxy" you were calling required a status code of "0" (worked with non-Extension API calls from our previous Client DLL). There's a new one we added recently that allows you to call Extension APIs without requiring weird data structures. (the only difference is calling the "AnonymousProxySimple"). var request = {   Region: "us", }; var baseUrl =   "/AnonymousProxySimple/demo/get-enrollment-activation-kits?timeout=30000"; // "demo" is the client ID. Put yours here. $RestService   .V3Post(baseUrl, request, true, true)   .then(function (result) {     var response = JSON.parse(result.data.ResponseBody);     console.log(response); })   .catch(function (err) {     console.log(err); }); **Note to widget developers**: This same rule applies in the Back Office code. "ProxySimple" and "Proxy" behave differently. You can get rid of that "Status" ID and any other structures and just work directly with results if you use "Simple" **There are extensive notes on the topic. This is from a document in the DS internal help site**:   # Authenticated Customers There are two proxy endpoints that authenticated customer accounts can use: Simple and Standard. These are called by custom content pages and widgets in the back office and retail site. ### Simple Endpoint The simple endpoint is recommended over the standard endpoint because the requirements are less stringent for the Disco client API that is called. Let's walk through what happens when a call is made to the simple endpoint. 1. Suppose the following is posted to the simple API endpoint: Request URL: https://api.directscale.com/ProxySimple/Associates/GetEyeColor Request Body: { associateId: 4567 } Notes: A request to the proxy endpoint must be a POST. Even if you are not sending a request body to the proxy endpoint, you must still use a POST. The authentication token of the logged in customer account is required in the request header in the same manner as all other customer API endpoints. 2. The API proxy sends the following request to Disco: Request URL: https://{clientname}.corpadmin.directscale.com/Command/ClientAPI/Associates/GetEyeColor Request Body: { associateId: 4567, authenticatedAssociateId: 1234 } Notes: The proxy API will determine the correct clientname to use on the domain of the request to Disco by looking at the passed user auth token. It has embedded in it the client the user belongs to. You should not pass an authenticatedAssociateId parameter on the post to the proxy. The proxy will add the authenticatedAssociateId parameter when it prepares to call Disco. If a call to the proxy includes this parameter, it will be overwritten by the API proxy with the logged in user Id. If a Disco API does not need this parameter, it is ignored. All Disco custom client API endpoints begin with the root level path /Command/ClientAPI. This is automatically prepended to the passed path from the proxy call. So, do not add /Command/ClientAPI to your proxy call or it will be sent to Disco as /Command/ClientAPI/Command/ClientAPI. Note on Timeout: By default, the request to Disco will timeout after 10 seconds. If you need to adjust that timeout, an optional timeout query parameter timeout may be passed on the call to the proxy. The value is the number of seconds to use for the request timeout on the call to Disco. 3. Disco returns some kind of message back to the proxy like this: Response Status Code: 200 Response Body: { Color: 'Green' } Notes: Custom client Disco API endpoints should make use of the authenticatedAssociateId parameter when they need the logged in user Id. In the case of the example above, the Disco API should check that the associateId passed is in the downline of the authenticatedAssociateId or perform whatever other security checks are prudent for the endpoint.  4. The proxy returns the data from Disco like this: Response Body: { ResponseBody: { Color : 'Green' }, ResponseStatus: 200 } Notes: The ResponseStatus is set to whatever HTTP response status code the call to the Disco endpoint returned. The ResponseBody is set to whatever response body the Disco endpoint returned. If the call to the Disco API timed out or had a TCP error, the proxy will return a 500 HTTP response with an empty response body. The Intranet logs time outs and TCP errors on the Exception Logs page. ### Standard Endpoint The standard endpoint is the first version of the proxy. It expects Disco endpoint responses in a certain format. Let's walk through what happens when a call is made to the standard endpoint. Notes from the simple endpoint apply here too except where otherwise noted. 1. Suppose the following is posted to the standard API endpoint: Request URL: https://api.directscale.com/Proxy/Associates/GetEyeColor Request Body: {associateId: 4567 } 2. The API proxy sends the following request to Disco: Request URL: https://{clientname}.corpadmin.directscale.com/Command/ClientAPI/Associates/GetEyeColor Request Body: { associateId: 4567, authenticatedAssociateId: 1234 } 3. Disco returns some kind of message back to the proxy like this: Response Status Code: 200 Response Body: { Status: 0, Message: null, Data: { Color: 'Green' } } Notes: The API proxy is expecting that the Disco endpoint will return a JSON object in the response body with the properties Status, Data and Message. 4. The proxy returns the data from Disco like this: Response Body: { ErrorMessage: null, Response: { Color: 'Green' } } Notes: The response JSON object of the API1 proxy includes two properties: ErrorMessage and Response. ErrorMessage contains the value of the Message property returned in the Disco response when the Status property is set to a non-zero value. Otherwise it will be null. Response contains the value of the Data property returned in the Disco response when the Status property is set to zero. Otherwise it will be null. If the Disco call returned a non-zero value of Status, the Intranet will log the error and the proxy will return an empty 500 HTTP response in the same manner as the simple endpoint error handling. # Unauthenticated Customers The unauthenticated proxy endpoints are called by enrollment or other sites that do not have a logged in user auth token to pass. The endpoints behave in the same manner as the authenticated customer endpoints except: No authenticatedAssociateId parameter is added to the requests to Disco because the user is not logged in. The paths of the endpoints require the name of the client be passed. Simple Endpoint: https://api.directscale.com/AnoymousProxySimple/{client}/{the rest of the path} Standard Endpoint: https://api.directscale.com/AnonymousProxy/{client}/{the rest of the path} # Authenticated Admin Users There is only one authenticated admin user proxy endpoint. It is called by custom content admin pages. The admin proxy endpoint behaves in the same manner as the authenticated customer simple endpoint except: The authenticatedAssociateId that is passed to Disco by the proxy is, of course, the Id of the logged in admin user not the Id of a logged in customer. The path of the admin endpoint is: https://api.directscale.com/Admin/Proxy/{the rest of the path} Read more: [Client API Proxy: Corporate Office (Disco)](https://developers.directscale.com/docs/disco-client-api-proxy)