Software as a Story: API
Tags: saas, api, saastools, postman
Published: 2022-01-31 23:40:52

## TLDR
In this edition of Software as a Story, we’ll be looking at a SaaS tool used for building and testing APIs.

## Introduction
Welcome to the 1st edition of Software as a Story! I’m Nathaniel at Mage, and today, we’ll be taking a look at SaaS (Software as a Service) tools out there that help us ship quality software.

## Background
I’ve been a software engineer working in full-stack development for about 2 years, and have experienced how APIs are crucial when developing any shared software. When first developing, you start off with either the frontend or backend. The frontend contains our web application or client processing user interaction. While the backend has databases, schemas, or servers to store your data. Once you’ve built them, an inexperienced developer may try to link them directly. This is extremely dangerous as secrets such as credentials could be leaked, and telling anyone who uses “inspect element” where you store your software, along with any credentials used to access it.

## API
An API, or Application Programming Interface, is a fancy word for the middle man that will handle fetching from the database using queries, and handling requests from the frontend. There’s also an added layer of security that you may add called API keys to verify that the request came from the client that owns the data, and battle tested APIs help split the traffic between multiple client server connections to speed up response times.

## curl command
The first way to access an API is through curl requests, these follow the HTTP Protocol, which is a standard that helps developers design APIs. First let’s take a look at this sample curl command used to fetch real-time predictions for a ranking ML model.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca2bm1x58breg6bk2hty.png)

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5iljkun3o4fjb8ixmi9p.png)Find the correlation between a cabin on the Titanic and the passenger’s odds of survival._</center>

The command is broken down into 3 important parts. The request, header, and body. As you can see typing this into a console is not very friendly. It’s long and a single mistake means you need to start over. This is where more modern tools come in.

## Postman
Postman has a simple easy to use UI that lets you save, store, and share your endpoints for collaboration. Starting with the request, [Postman](https://www.postman.com/) knows all the HTTP methods, so you can select it from a dropdown, then type in your endpoint url.

## API Request

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hh5mlgvcigf9fx11nuh9.png)We want to send a POST request to [https://api.mage.ai/v1/predict](https://api.mage.ai/v1/predict)_</center>

When using curl you have to manually add each header to the response, this is another pain point, but Postman is modern so it automatically sends commonly used headers. If you don’t want these you can toggle them.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wf1s3pdmfbkei11thund.png)Postman lets you configure exactly what you want and don’t want to send._</center>

Finally, we get to the body section located next to the headers tab. To match our “data” parameter, we’ll want to have “raw” selected using the radio buttons, then paste our body. Since our content-type was application/json, we’ll also need to specify that by choosing JSON as the raw datatype.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1vnhmwfe81i1ycpjr9l.png)Choose the data type that matches your data_</center>

## API Response
Once we’re done, we click the send button and get a monster of a response back. On Postman, we can save this response as a file or as another example or tab.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqw48e5ivtls08997k9v.png)
Notice that the response took 7 seconds, this is often really bad as our goal is 2.5 seconds per request so we can hover over it and look at a breakdown of why.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7xe8i1nomdmh433nlswr.png)The 8 second response is far too long._</center>

From these events, we can see that the DNS Lookup took up the most time, a whopping 6.28 seconds. This is often because the subdomain of app.mage.ai requires multiple lookups and redirects. Seeing a breakdown of events helps identify bottlenecks early and the ability to save it in a collection for our backend developers to replicate and test.

## API Testing
Another cool feature Postman has is caching. Since everything is cached, there are no extra DNS Lookups and security checks, meaning that subsequent calls will be faster. This is great for when you’re writing tests to verify the output and server response.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lganznvd7ri2fx0hpw5p.png)Caching means faster testing_</center>

Postman has a testing suite programmed using JavaScript with guides to self-learn the library’s commands and functions. Here, I’ll show off one of the most basic tests, the HTTP status, but for more information on testing check the reference [manual](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/). Most of the time, you want to make sure your server returns a 2XX response, so we’ll create a script to test for it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ru1g58hwxgove0adog82.png)
Running the request again shows, under the test results tab, that it passed the test.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddseunuaz7vop4jw1vwv.png)

Finally, once you have a ton of tests you can export the collection and integrate them in your CI/CD pipeline to automatically run the tests every time changes to the code are made. This helps you keep the code clean and find what you may have broken.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvbx8hwggfvxlhdrd4c6.png)You know that song? (Source: [99 little bugs in the code](https://me.me/i/99-little-bugs-in-the-code-99-little-bugs-in-21272125))_</center>

## Mentions
Before we end, I’d like to give some honorable mentions, as Postman is not the only viable tool for APIs. Another SaaS tool for APIs that is used at Mage is [Insomnia](https://insomnia.rest/). It functions similarly to Postman where you can create requests and collaborate with a team. In the end, I chose Postman because it was more inclusive with its testing features, documentation, and guides. If you liked this edition of Software as a Story (SaaStory), comment down below about what you’d like to see next! We’re looking for content creators with a developer background and stories to tell! Join us by visiting our [Discord](http://mage.ai/chat) or [careers](https://mageai.notion.site/mageai/Careers-62f439a652a44303ab96d578a4ce4893) page.

_<center>![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cpjulk51a9zpueh1wng8.gif)Enter the [portal](http://discord/)_</center>