Make Money From API [Part I – Building An API]

In this article, we will try to explain how to create an API and monetize it on RapidAPI.

We will break down this into 3 parts. The first part will contain how to build an API.

  • Part I: Building an API
  • Part II: Hosting the API
  • Part III: Connecting the API with RapidAPI

 

Let’s get started with Part I.

1. Installations and Setup:

First things first, we will be using Python to build our API. You can also use other languages (Like Node.js, Dart, Go, etc.). So make sure you’ve installed Python. In case you haven’t, download and install it from https://www.python.org/downloads/

There are many frameworks in Python for building API, such as Django, FastAPI, and Flask. Since we are making a simple API, FastAPI would be a better option (personal opinion) as it is easy and beginner friendly. We will also have to install a few packages.

    1. requests
    2. fastapi
    3. uvicorn

Open your command prompt and run the following command.

pip install requests fastapi uvicorn

and we are pretty much done with the installations.

2. Running FastAPI Application:

Here’s the boilerplate for FastAPI. You can copy-paste or try to code yourself. Create an app.py file in a directory and add the following code to it.


Now go to the directory and open the command prompt/terminal and run the following command

python app.py

You’ll see something similar to this:

Command Prompt Screenshot
Running FastAPI Script from Command Prompt

If you now open your browser and go to http://127.0.0.1:8000/ you’ll see something like this:

 

FastAPI server running
FastAPI server running

If you see this, you’re good to go to the next step which will be to add some functionalities to it. It still doesn’t do anything apart from showing an object.

3. Adding Functionalities to the API:

We will be using an open-source API just for the sake of explanation. You are allowed to be creative here. The more creative you get, the better for you. Let’s head over to, Github

And choose an API.

Github Public API
GitHub Public API

I’ll be choosing Game of Thrones Quotes. When you click on the API, you should get some sort of documentation on how to use them.

In my case, we can see the endpoint /v1/random will return an object with a quote and other information when a GET request is made.

Get a random GOT quote
Get a random GOT quote

 

So, the idea here is to generate a random quote from Game of Thrones every time user makes a GET request to our homepage.

We will return an object with a character and a quote. To get the quote and the character name all we need to do is make a get request to the endpoint using the requests package. It will return a Response object. Then we convert it into JSON so that we can access the keys and the value.

Back to Python basics, we access object (dictionary) values using the keys as the keys will remain constant but the values might change. We make the following changes to our boilerplate:

GOT quote generator API
GOT quote generator API

 

Code Snippet:

If you don’t see a GitHub embedded code, go to this GitHub Gist for the code.

Wrapping Up:

We are done with the coding part. Now run the script using the python app.py from your command prompt. If you go to http://127.0.0.1:8000/ and you’ll the server up and running.

GOT quote generator API server
GOT quote generator API server

Congratulations on building your first API!

In the next part, we will explain how to deploy the API for free.

If you have any query feel free to comment down below. See you in the next tutorial.

Leave a Reply