user21067592
user21067592

Reputation: 11

How can I successfully integrate a service with netsuite using a restlet instead of suitetalk?

I have a very (embarassingly basic) restlet built in netsuite that calls an outside API that our company uses to sell digital items. Right now it's just deployed in a demo account, but eventually it will be used in our prod. But now I'm lost and I don't see a link to deploy it anywhere. I know I will have to create a client script as well, but i'm not sure why, so if someone could explain that to me, that would be helpful. Basically, we don't think that it's necessary to use suitetalk because we are only using one call from this company's api. I'm also new to javascript so I really could use some help. I need to shove the external company's API key in there. This external company will only accept XML as the format for the post request, but i don't think NS allows this in restlets, as it says either json or plain text. Could someone tell me if it's possible to do this, and how to improve this code? NS documentation seems like it's almost intentionally vague, and i'm not getting very far by using it.

/**
 * @NApiVersion 2.0
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/https'],function(https){
      function postRequest(params){
        var headersObj = {
          name:'Content-Type',
          value:'application/json',
          sku: 'abcde',
          'license-type': 'xxxxx',
          'num-codes': 1,
          'online-license-type': 'numdays',
          'online-num-days': 365
        };


        const headerArray = ["X-[outsidecompany]-API-Key: ABCDEFG"];

        var apiResponse = https.post({
          url:'https://api.[outsidecompany].com/v3/codes.xml',
          headers:headersObj
          //body:headerArray
        });
        log.debug('apiResponse',JSON.stringify(apiResponse));
        return apiResponse;
      }

      return {
        'post':postRequest
      }
      });

I tried to convert everything to XML, but NS would not recognize it for some reason - apparently it can only be used for GET, but the API only accepts XML for POST. I easily could have been doing something wrong. I want to implement an api call that we can use when a customer orders specific digital items from our ecommerce site, so that we can have a code created and ready in the item record when they check out.

Upvotes: 1

Views: 764

Answers (1)

Steffen Andersland
Steffen Andersland

Reputation: 609

You can do this from a RESTlet

Firstly, you would want to use the https.createSecureString method in tandem with the API Secrets feature in NetSuite to store these secret credentials and pass them into your API Call.

Secondly RESTlets can work with XML, there is even a handy 'N/xml' library you can use to help parse it.

/**
 * @NApiVersion 2.0
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/https'],function(https){
      function postRequest(params){
        var headersObj = {
          name:'Content-Type',
          value:'application/json',
          sku: 'abcde',
          'license-type': 'xxxxx',
          'num-codes': 1,
          'online-license-type': 'numdays',
          'online-num-days': 365,
          // custsecret_apikey is defined in an API secret and referenced here securely
          'X-[outsidecompany]-API-Key': https.createSecureString({input: '{custsecret_apikey}'}),
        };

        // XML is defined as a string here
        var myXMLData = "";

        var apiResponse = https.post({
          url:'https://api.[outsidecompany].com/v3/codes.xml',
          headers: headersObj
          body: myXMLData // this must be a string
        });
        
        if (apiResponse.code == 200) {
          log.debug({title: "response", details: apiResponse.body});
        } else {
          // do something else
        }

        return JSON.stringify(apiResponse);
      }

      return {
        post: postRequest
      };
});

Upvotes: 1

Related Questions