Using your generated Node JS SDK

This article is now deprecated. SDK usage instructions are now contained in the generated SDK.

See the new APIMATIC docs.

In this article, we will walk you through the generation of the SDK and how to use it.

The following covers the following aspects:

  1. Setting up the environment
  2. SDK generation instructions
  3. SDK installation instructions
  4. Creating a sample application that uses our SDK

Note: The code for this tutorial is provided as a zip file available for download here.

1. Setting up the environment:

Following this tutorial here, a library is already generated, downloaded, and ready for use.

You can use your favorite text editor.

2. SDK generation instructions:

After choosing Node.js as your code language, as shown in Figure 1, a zipped file will be downloaded. This file contains the code for the APIMATICCalculator library we will be using in our project. 

Image not found
Figure 1: Choosing Node.js as the language of generation.

 

3. SDK installation instructions:

In order to use your library, you have to unzip it first. 

Extract the unzipped file to a new folder, as shown in Figure 2. 

Image not found
Figure 2: Unzipped library contents.

 

4. Creating a sample application:

The following section explains how to create a new project that uses your generated APIMATICCalculator library.

 4.1 Starting a new project

On starting a new project, create a new folder with your project name "myCalculator".

Inside this folder, there should be:

  1. A new file with the file extension (.js). This file will have the JavaScript code that will use your library. 
  2. Another folder with the name "node_modules".

Place the apimaticcalculatorlib folder (from the generated library in Figure 2) in the "name_modules" folder. The reason behind this is that Node searches for libraries under this specific folder name. Figure 3 shows the correct folder hierarchy. 

Image not found
Figure 3: Folder hierarchy and contents.

 

4.2 Using your library in your project:

In order to use your library, first you have to download all its dependencies.

To do that, open your command prompt and navigate to your "apimaticCalculatorLib" directory. 

>cd \myCalculator\node_modules\apimaticcalculatorlib

Then, install by typing in:

>npm install 

This will install all the library dependencies needed to run your code, as shown in Figure 4.  

Image not found

Figure 4: Successfully running the program.

 

To successfully integrate your library to your code, you need to "require" it in your class. 

After that, you need to instantiate its objects and invoke their functions.

To get our calculator running, we need to invoke the asynchronous function getCalculate(), shown in Figure 5, on the controller object.

Image not found
Figure 5: getCalculate() function.

 

This method takes the following parameters:

  1. An object "input" that contains an operation name (e.g. SUM), and two doubles.  
  2. A callback function that executes when the getCalculate() function finishes. 

 

Figure 6 shows how your main class should look like after doing the following steps in order:

  1. Adding your library at the top of your class (line 1).
  2. Instantiating the SimpleCalculatorController (line 10).
  3. Invoking the getCalculate() method on it (line 10).
  4. Providing the two parameters; input (defined in lines 4-8) and callback function (defined in lines 11-27).

Image not found

Figure 6: Main class that uses the APIMATICCalculator library.

 

If there is no error, the result will be printed to the console.

If there is an error, the callback function will print an error message, followed by the actual error trace. 

 

You are ready to run the project.

 

4.3 Running your project:

To run your project, open your command prompt and navigate to your myCalculator project directory. 

>cd \myCalculator

 then to run your main.js, type in

>node main

Running the program successfully, should print out the right result to the command line, as shown in Figure 7.

Image not found

Figure 7: Successfully running the program.

Go to top

Download files below.

Have more questions? Submit a request

Comments