Breaking into the industry: My Journey to becoming a Software Engineer #8 — Working with APIs

M E
8 min readJul 21, 2021

Before I started to learn how to code, I have always come across the word “APIs.” Even searching for Software Engineering jobs (I was being curious), companies are always looking for applicants that have knowledge about APIs. I didn’t know what an API was before and how powerful it is until today.

What is an API?

API stands for Application Programming Interfaces. It is basically used to access data on the web. APIs are a set of commands, functions, protocols and objects that programmers can use to create software or interact with an external system. Essentially, API is an interface or a sort of barrier between two programs, and those programs have to talk to each other and that’s where API come into play.

To improve my API skills, I created the following projects: ISS Tracker, Habit Tracker, and Stock Alert.

ISS TRACKER

I have watched a lot of videos about astronauts in the International Space Station (ISS). I am always interested on how astronauts train to go to space, what their daily activities is like while living in the ISS and etc. Thus, wouldn’t it be nice to see the ISS floating above you? I created a project called the ISS Tracker. Since the ISS orbits Earth multiple times/day and can be best seen during sunrise and sunset. It is almost impossible to see the ISS during the day. Therefore, I would need the longitude and latitude of my current location, the exact location of the ISS by using ISS Location Now API (e.g. longitude and latitude) and check my location’s sunset time. Then those information will be used to check whether the ISS is near my current location, if so, then it would send me an email notification to give me a heads up (no pun intended) to look up!

Constant Variables
This functions checks if the ISS is near my current location, if so, then return True.

By using sunrise-sunset(https://sunrise-sunset.org/api) API, it requires two parameters: longitude and latitude. I passed in my current location’s longitude and latitude to get the sunrise and sunset data. However, there is an extra parameter in the PARAMETERS -{ “formatted:” 0), which basically means that the data returned is in UNIX time (12-hr time). Moreover, we also need to access the current time in my current location. We can do it by importing datetime.

This function access the sunrise and sunset API data and returns True if the current time is either sunrise or sunset.

So now we can run a while loop. While True, check if the ISS is near my location and if it is night time (sunrise or sunset), if one of the conditions is false, then try checking again every 60 seconds; however, if the both conditions are true, then by using smtplib, send a notification through email. Bam! Don’t forget take a picture! :)

Every 60 seconds, the while loop checks if the ISS is near my current location and whether it is sunrise or sunset.

HABIT TRACKER

Have you ever heard of the saying “it takes 21 days to create/break a habit?”

“[It takes] a minimum of 21 days for an old mental image to dissolve and a new one to jell.” Dr. Maxwell Maltz

Wouldn’t it be nice to be able to track our habits to see our progress? Since it’s continuous, it makes you really want to continue your streak and not break the line. One of my favorite quotes is from my favorite basketball player of all time: Kobe Bryant.

“It’s to constantly try to be the best version of yourself. It’s a constant quest to try to be better today than you were yesterday.” -Kobe Bryant

So in order to speak this into existent, I created a Habit Tracker using Pixela API. Using API, Pixela allow users to track their habit, but not just display which days the user tracked their habit but also the intensity of the activity/habit. For instance, if a user entered that he/she read 30 pages of book yesterday while today, the user read 60 pages. Then it would show a lighter color yesterday and a darker color today.

First off, I needed to create a user account via HTTP POST request in order to set up Token and a username. The username and token are required to be passed as JSON parameters for the Pixela API. Additionally, the user must add ({“agreeTermsOfService”: “yes”, “notMinor”: “yes”}) to the parameters along with the username and token. Otherwise, the request would not work.

Secondly, the user must create a Pixela Graph. The graph endpoint that needs to be called by HTTP POST is /v1/users/<username>/graphs. The user must create a graph configuration to be passed on as JSON when making a POST request to Pixela. The graph configuration must include the “id” which the user could create uniquely, “name” of the specific activity or habit the user wants to track, “unit” for measurement (e.g. amount of time in minutes or amount of pages the user read), “type” of the input (e.g. integer), and “color” which is the display color of the pixel of the graph. Moreover, the user must pass the token as the header in order to access the Pixela Graphs. Then voila! You have created a Pixela Graph!

Posting a value or an activity to the graph

When tracking an activity, I used the datetime module to set up the date that the activity occurred. Additionally, I added the quantity which is the pages of book that I read on that day specified. What if at the end of the day, I was able to read more pages? I can easily edit my Pixela Graph by HTTP PUT request.

STOCK ALERT

“Do you want to go to the moon?” This phrase is commonly used on Reddit nowadays that attract investors to go “YOLO” on of the meme stocks and hope that it would skyrocket to the “moon.” It could be daunting to try to constantly check with how the stocks performed during market. I am guilty of that. I feel like I waste too much time staring at the graphs and searching for news that could be a catalyst to why the stock is performing a certain way. So why not create a Stock Alert that would notify me through text message if a specific stock is either 5% up or down (your preference), and includes top three latest news about the stock.

Firstly, I used Alpha Advantage to get an API key in order to access the Stock Market Data APIs. After securing an API Key, I set up my parameters and the stock I chose to track is TSLA (Tesla). Since I am tracking the percent difference between the stock’s yesterday’s closing price and the day before closing price, therefore, I used “TIME_SERIES_DAILY_ADJUSTED.” By using a list comprehension, I can get the “Time Series (Daily)” key to access the data that I need and save yesterday’s data and the day before data by using their designated indices (index 0 for yesterday’s data and index 1 for the day before data) and the “[‘4. close’]” key to get the closing price.

Calculate the Percent Difference

As I have previously mentioned, I want to be notified if the stock that I am tracking has a percent change of 5% (up or down) from yesterday’s closing price and the day before closing price. In order to calculate the positive percent difference, I used Python’s abs function and divide the day_before_data to yesterdays_data and multiply by 100 to get the percentage. Then I created a variable named “is_up” that returns a Boolean to compare whether yesterday’s closing price is greater than the day before.

Getting the News

By going to the News API website, we can request for a free API key that we need in order to access the news data. One thing to take note while setting up the News Parameters, some companies might not have a lot of news about them. The most recent news might be from a month ago, therefore by using the datetime module, I created a variable that contains a date 25 days from date.today(). Now we can set up our News Parameters that contains the “qInTitle” which is basically company’s name (not the stock symbol), “from” which the date, “sortBy” which in this case I used “publishedAt” since I want the most recent news about this company to be at the top and most importantly, the “apikey” that I got from signing up.

By submitting a GET HTTP Request, I got a JSON data from the News API. I passed in the “article” key to get the list of news and used slicing [:3] to get the 3 most recent news about the company. Now, I need to set up what my notification content would be.

I set up three new variables that contain the content of my notification which includes the three articles that I gathered from the JSON data.

Notify using Twilio

Since I used the Python’s abs function, no matter what, the percent difference will always return a positive number. Therefore, I must check if the percent_difference is greater than 5% (user preference). If so, then it will first check if yesterdays_data is greater than the day_before_data, if so, then that means the stock is up and would send me a notification via Twilio including the percent difference and the three news articles about the company. On the other hand, the stock is down by 5% (user preference), then it would also notify me along with the three news articles.

Please check out my GitHub account for the full access of the ISS TRACKER, HABIT TRACKER and STOCK ALERT.

--

--