IBM Sterling products unofficial blog

IBM Sterling B2B Integrator, IBM Sterling Filegateway, Node.js, REST API

Very Simple Node.js REST API client for IBM Sterling B2B Integrator

In this very basic REST API Client example you will use Node.js (JavaScript runtime Environment) to make REST API calls.

Node.js and npm should be installed.

This code sample requires node-rest-client.

To install this module run:

npm install node-rest-client

Below is a REST GET Client Code sample to list trading partners:

//restClient.js
var Client = require('node-rest-client').Client;
//Define URI constants
//TODO: Change
var address = "http://0.0.0.0";
var port = "5074"
var apiUri = "/B2BAPIs/svc/"
var baseUri = address + ":" + port + apiUri;
//Setup HTTP auth info, B2Bi/SFG user with "APIUser" permission
//TODO: Change
var auth_info = {user:"APIUser", password:"*******"};
//Create client instance with auth info
client = new Client(auth_info);

//Define required headers
var headers = {
 "Content-Type": "application/json",
 "Accept": "application/json"
};
//Argument set with only required headers
var basicArgs = {
 "headers": headers
};

//REST GET to list trading partners
client.get(baseUri + "tradingpartners/", function (data, response) {
 // parsed response body as js object
 console.log(data);
 // raw response
 //console.log(response);
});

To run the program: save the code into a file and run:

node restClient.js

tradingPartnersGet

In the first code sample, we used the GET method.

Now we will use the POST method to create resources using REST API calls.

With the POST we need to pass the JSON data as an argument in the method call.

In the following example, we will use the Community REST API to create a Community called SFG:

var Client = require('node-rest-client').Client;
//Define URI constants
//TODO: Change
var address = "http://0.0.0.0";
var port = "5074"
var apiUri = "/B2BAPIs/svc/"
var baseUri = address + ":" + port + apiUri;
//Setup HTTP auth info, B2Bi/SFG user with "APIUser" permission
//TODO: Change
var auth_info = {user:"APIUser", password:"*******"};
//Create client instance with auth info
client = new Client(auth_info);

//Define required headers
var headers = {
 "Content-Type": "application/json",
 "Accept": "application/json"
};


var jsonData=
{
 "cdListening": false,
 "customProtocols": null,
 "ftpListening": true,
 "name": "SFG",
 "partnerNotificationsEnabled": false,
 "partnersInitiateConnections": true,
 "partnersListenForConnections": true,
 "sshListening": false,
 "wsListening": false
};

// set content-type header and data as json in args parameter
var args = {
 data: jsonData,
 headers: headers
};

//POST REST call to create a Community
client.post(baseUri + "communities/",args, function (data, response) {
if(Buffer.isBuffer(data)){
 data = data.toString('utf8');
}
console.log(data);
  
});

To run the program: save the code into a file and run with node.js:

node createCommunity.js

The community will be created in SFG (IBM Sterling Filegateway):

SFGCommunity

1 Comment

  1. Leonardo

    Hello Admin, for starters thanks for your examples have helped me a lot. Now I can already do a lot of things in bulk, however now I don’t have the following problem:

    – I am making a script that should do the following:

    1. Bulk insert partner records using an excel file (Done)
    2. Make a get after each insert to be able to compare with the record in memory if it matches, if negative delete the record
    (Here I have a problem, I don’t know why I always get the last but not the last record). I would like to see if you could support me if I share the code?

    Thank you very much,

Leave a Reply

We’ve detected that you’re using an ad blocker. Please disable it to support our website.