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:
- Setting up the environment
- SDK generation instructions
- SDK installation instructions
- 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.
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.
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:
- 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.
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.
Then, install by typing in:
This will install all the library dependencies needed to run your code, as shown in Figure 4.
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.
This method takes the following parameters:
- An object "input" that contains an operation name (e.g. SUM), and two doubles.
- 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:
- Adding your library at the top of your class (line 1).
- Instantiating the SimpleCalculatorController (line 10).
- Invoking the getCalculate() method on it (line 10).
- Providing the two parameters; input (defined in lines 4-8) and callback function (defined in lines 11-27).
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.
then to run your main.js, type in
Running the program successfully, should print out the right result to the command line, as shown in Figure 7.
Please sign in to leave a comment.