Morgan-Phoenix
Posted on June 22, 2021
Hey there, Today I'm going to start this series of "Learn FastAPI With Me". So those how don't know what a api is, let me tell you:
According to Wikipedia:
An application programming interface(api) is a connection between computers or between computer programs. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how to build such a connection or interface is called an API specification.
But if you are just starting and don't care about definition and want things in simple and clear words,
An API is just a program that helps two programs to talk to each other. Imagine an API as the mouth and ears of a program, using which the program can talk and listen to other programs.
Want an API in your web app now?
Fine, Normally APIs are made using javascript, but it takes time to make one.
So let's take the easy path insted and use our beloved programming language that we all know, Python.
We will use FastAPI for now
You can use other API frameworks too (like flask), but we are using FastAPI for now
Why FastAPI?
- It's Fast
- It easy
- Supports async functions
And that should be it to convince you
Not convinced yet?
- It supports rendering of html pages
- File transfers
Let's continue now,
Installation:
It's very easy to install FastAPI just type this(↓) in your terminal
$ pip install fastapi
If you don't get an error, you're solid let's create out first api
The API:
Now let's create our first api
Create a file main.py
in any folder
Copy-Paste the code given below in the main.py
file
from fastapi import FastAPI
app = FastAPI()
@app.get('/')
def home():
return {'data':'Get This And Be Happy'}
And done, your first basic API is ready!
but, how do you test if this is working?
we use a server to check if it is working.
The Server
We will be using uvicorn
to test our api
Let's install it!
$ pip install uvicorn
now open a terminal and go to the folder where main.py
is and type the following
uvicorn main:app --reload
And hit enter
A URL will be provided to you in the terminal, Go to that URL
(It will mostly be http://127.0.0.1:8000)
If you see {'data':'Get This And Be Happy'}
, Then do what you see, be happy, your api is working!!
In the next post I will explain what that code is, until then be happy
See you in the next post.
Posted on June 22, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.