Integrating third-party APIs into your systems can significantly enhance their functionality and efficiency. Postman, a popular API client testing toolset, offers a feature to generate code snippets in various languages and frameworks, simplifying the task of API integration.

In this guide, we'll walk you through using Postman's code generation feature to create an API call for SpreadsheetWeb, a powerful platform that enables you to transform Excel spreadsheets into web applications and APIs.

 

Step 1: Making a Postman API Call

 

Before generating code, you will first need to successfully make the API call within Postman. Follow these steps:

  1. Open Postman and create a new request by clicking the "New" button and selecting "Request."
  2. Configure your API request by entering the API URL, selecting the appropriate method (GET, POST, etc.), and setting up headers and body parameters as required by the API.
  3. Send the request to test it. Ensure that you receive the expected response, indicating that your API call is correctly configured.

You can visit our article about how to make API call to SpreadsheetWeb via Postman.

 

Step 2: Generating Code Snippets

 

Once your API call works as expected, follow these steps to generate code snippets:

  1. Click the "Code" button located under the "Save" button in Postman.

  1. A window will appear showing the code snippet for the Curl command by default. Select the language or framework you need from the list on the left-hand side. For this guide, we will focus on jQuery, Node.js, and C#.

  1. Copy the generated code snippet. Postman automatically generates the code based on your request setup. For example;
4.	var https = require('follow-redirects').https;
5.	var fs = require('fs');
6.	
7.	var options = {
8.	  'method': 'POST',
9.	  'hostname': 'api.spreadsheetweb.com',
10.	  'path': '/calculations/calculatesingle',
11.	  'headers': {
12.	    'Content-Type': 'application/json',
13.	    'Authorization': {{‘Your Bearer Token’}}
14.	},
15.	  'maxRedirects': 20
16.	};
17.	
18.	var req = https.request(options, function (res) {
19.	  var chunks = [];
20.	
21.	  res.on("data", function (chunk) {
22.	    chunks.push(chunk);
23.	  });
24.	
25.	  res.on("end", function (chunk) {
26.	    var body = Buffer.concat(chunks);
27.	    console.log(body.toString());
28.	  });
29.	
30.	  res.on("error", function (error) {
31.	    console.error(error);
32.	  });
33.	});
34.	
35.	var postData = JSON.stringify({
36.	  "request": {
37.	    "workspaceId": {{‘Your Workspace ID}}
38.	    "applicationId": {{‘Your Application ID}},
39.	    "request": {
40.	      "input": {
41.	        "calculation": {
42.	          "inputs": [
43.	            {
44.	              "reference": "loan_amount",
45.	              "value": [
46.	                [
47.	                  {
48.	                    "value": "500000"
49.	                  }
50.	                ]
51.	              ]
52.	            },
53.	            {
54.	              "reference": "interest_rate",
55.	              "value": [
56.	                [
57.	                  {
58.	                    "value": "0.05"
59.	                  }
60.	                ]
61.	              ]
62.	            },
63.	            {
64.	              "reference": "loan_period",
65.	              "value": [
66.	                [
67.	                  {
68.	                    "value": "30"
69.	                  }
70.	                ]
71.	              ]
72.	            }
73.	          ],
74.	          "outputs": [
75.	            "scheduled_payment_amt"
76.	          ]
77.	        }
78.	      }
79.	    }
80.	  }
81.	});
82.	
83.	req.write(postData);
84.	
85.	req.end();

 

Step 3: Integrating Postman Code into Your Project

After generating the code snippet, the next step is to integrate it into your project. This process varies slightly depending on the language or framework that you are working with.

 

jQuery (Client-Side)

 

Integrating functionality directly on the client side is often not recommended due to security concerns, particularly the risk of exposing sensitive access tokens to the client environment. Therefore, this method should be employed with caution and is primarily advised for scenarios where the APIs in question are designed to be accessible by the public. In these cases, the information exposed is intended to be available without stringent security measures, but even then, understanding the implications and ensuring the least privileged access is crucial for maintaining overall system security.

Integrating an API call into your application using jQuery is straightforward. Follow these steps to ensure smooth integration:

  1. Prepare Your Project Environment: Ensure your project is set up to use jQuery. If jQuery is not already included, you can add it by inserting the <script> tag linking to the jQuery CDN in the <head> section of your HTML file. 
  1. Identify the Integration Point: Decide where in your project the API call will be most effective. This could be in response to user actions like clicking a button, submitting a form, or upon loading the webpage.
  1. Incorporate the Postman-Generated jQuery Code: Open the JavaScript file where you plan to handle the API call. Paste the jQuery snippet generated by Postman directly into this file. This snippet is pre-configured to perform the API call, including any necessary headers, query parameters, and the request method (GET, POST, etc.).
1.	var settings = {
2.	  "url": "https://api.spreadsheetweb.com/calculations/calculatesingle",
3.	  "method": "POST",
4.	  "timeout": 0,
5.	  "headers": {
6.	    "Content-Type": "application/json",
86.	    "Authorization": {{‘Your Bearer Token’}}
7.	
8.	  },
9.	  "data": JSON.stringify({
10.	    "request": {
87.	      "workspaceId": {{‘Your Workspace ID’}}
11.	
88.	      "applicationId": {{‘Your Application ID}}
12.	
13.	      "request": {
14.	        "input": {
15.	          "calculation": {
16.	            "inputs": [
17.	              {
18.	                "reference": "loan_amount",
19.	                "value": [
20.	                  [
21.	                    {
22.	                      "value": "500000"
23.	                    }
24.	                  ]
25.	                ]
26.	              },
27.	              {
28.	                "reference": "interest_rate",
29.	                "value": [
30.	                  [
31.	                    {
32.	                      "value": "0.05"
33.	                    }
34.	                  ]
35.	                ]
36.	              },
37.	              {
38.	                "reference": "loan_period",
39.	                "value": [
40.	                  [
41.	                    {
42.	                      "value": "30"
43.	                    }
44.	                  ]
45.	                ]
46.	              }
47.	            ],
48.	            "outputs": [
49.	              "scheduled_payment_amt"
50.	            ]
51.	          }
52.	        }
53.	      }
54.	    }
55.	  }),
56.	};
57.	
58.	$.ajax(settings).done(function (response) {
59.	  console.log(response);
60.	});
  1. Configure Event Triggers: Modify the pasted code to trigger based on specific events within your application. For instance, if you want the API call to run when a user clicks a button, wrap the Postman-generated code in a function and call that function using .click() event handler on the desired button element.
$('#yourButtonId').click(function() {

    // Postman-generated jQuery API call

});
  1. Test the Integration: After integrating the code, test it thoroughly in your project. Ensure that the API call is executed as expected and that the data retrieved from or sent to the external service is correctly handled by your application.

By folowing these steps, you can effectively integrate external API calls into your jQuery client-side project.

 

C# (Server-Side)

 

Integrating an API call on the server side of your application with C# is a powerful way to enhance your application's capabilities. Here's how to do it:

  1. Open Your Server-Side Code: Start by opening your server-side project in Visual Studio, or a similar Integrated Development Environment (IDE) that supports C#. This is where you'll integrate the API call.
  2. Locate or Create a test.cs File: You can create a test.cs file to test the code generated by Postman. This file will be used to add the C# code that makes the API call. The use of a specific file like test.cs is for example purposes; you may choose or have a different file name based on your project's structure and naming conventions.
  3. Insert the Postman-Generated C# Code: Paste the C# snippet you generated from Postman into the test.cs file. This code is specifically tailored to make the API call you've set up in Postman. When inserting the code, ensure it's placed within an appropriate method that suits your application's workflow, such as one that's triggered by a specific action on your application. As an example:
4.	var options = new RestClientOptions("")
5.	{
6.	  MaxTimeout = -1,
7.	};
8.	var client = new RestClient(options);
9.	var request = new RestRequest("https://api.spreadsheetweb.com/calculations/calculatesingle", Method.Post);
10.	request.AddHeader("Content-Type", "application/json");
11.	request.AddHeader("Authorization", "Your Bearer Token");
12.	var body = @"{
13.	" + "\n" +
14.	@"  ""request"": {
15.	" + "\n" +
16.	@"    ""workspaceId"": ""Your Workspace ID"",
17.	" + "\n" +
18.	@"    ""applicationId"": ""Your Application ID"",
19.	" + "\n" +
20.	@"       ""request"": {
21.	" + "\n" +
22.	@"      ""input"": {
23.	" + "\n" +
24.	@"        ""calculation"": {
25.	" + "\n" +
26.	@"          ""inputs"": [
27.	" + "\n" +
28.	@"            {
29.	" + "\n" +
30.	@"              ""reference"": ""loan_amount"",
31.	" + "\n" +
32.	@"              ""value"": [[{""value"": ""500000""}]]
33.	" + "\n" +
34.	@"            },
35.	" + "\n" +
36.	@"            {
37.	" + "\n" +
38.	@"              ""reference"": ""interest_rate"",
39.	" + "\n" +
40.	@"              ""value"": [[{""value"": ""0.05""}]]
41.	" + "\n" +
42.	@"            },
43.	" + "\n" +
44.	@"            {
45.	" + "\n" +
46.	@"              ""reference"": ""loan_period"",
47.	" + "\n" +
48.	@"              ""value"": [[{""value"": ""30""}]]
49.	" + "\n" +
50.	@"            }
51.	" + "\n" +
52.	@"          ],
53.	" + "\n" +
54.	@"          ""outputs"": [""scheduled_payment_amt""]
55.	" + "\n" +
56.	@"        }
57.	" + "\n" +
58.	@"      }
59.	" + "\n" +
60.	@"    }
61.	" + "\n" +
62.	@"  }
63.	" + "\n" +
64.	@"}";
65.	request.AddStringBody(body, DataFormat.Json);
66.	RestResponse response = await client.ExecuteAsync(request);
67.	Console.WriteLine(response.Content);
  1. Include Necessary Libraries or Dependencies: Check the Postman-generated code for any external libraries or dependencies it might use. Ensure these are included in your project references. These may include libraries for HTTP requests, JSON parsing, etc., which are crucial for the API call to work.
  1. Test Your Integration: After integrating the code, compile and run your application to test the API call's functionality within your environment. This step ensures that the API call works as expected and that any data it retrieves or sends is correctly handled by your application.

By following these steps, you can efficiently integrate external API calls into your application using C#.

 

Step 4: Testing and Validation of Postman Generated Code

 

After integrating the code:

  • Test the application thoroughly to ensure that the API call functions as expected within your environment or server-side implementation.
  • Validate the response data is correctly processed and displayed in your application.

 

Clients can streamline their integration of SpreadsheetWeb API calls into their projects by utilizing the code generation feature of Postman. By incorporating these Postman-generated snippets directly into their own projects, clients can easily execute SpreadsheetWeb API calls. This method simplifies the process, allowing for easy implementation and customization of API interactions according to their specific project needs, without the need for extensive manual coding.