Open In App

Run the fast-api server using Pycharm

Last Updated : 04 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

FastAPI is a Python web framework that makes it easy to build APIs quickly and efficiently. Returning an image is a common requirement in web development, and FastAPI makes it straightforward to do. another famous framework for doing the same is Flask, which is also Python-based.

Pre-requisite

Steps to Run Fast-API Server using PyCharm

Below are the steps by which we can run the fast-API server using PyCharm:

Step 1: Installation

For running the server we need a uvicorn. so before starting our API coding part, we need to the installation of fastAPI and uvicorn. we can install them by running the below command

pip install fastapi
pip install uvicorn

Step 2: Import FastAPI

First of all, create a new PyCharm project and then one folder with the name you want, eg FastAPI, inside that folder create a file with the name ‘main.py’ and import the library.

Python3




# Import required Library
from fastapi import FastAPI


Step 3: Create a FastAPI App

Now to use fastAPI we need to create it’s app, it’s very simpe as we just need to call FastAPI(). Write it inside the main.py itself.

Python3




# creating fastAPI app
app = FastAPI()


Step 4: Define a Route to Serve a response to the user

Here we define a route ‘/’ using the @app.get decorator. This route will handle GET requests. In this step we need to make route to our fastapi app. Here when any API request will come to our fastapi app first of all it will try to match with the url we mentioned inside app.get(), here get because client want the image in response so, client will send the get request. Here we had created 2 API endpoint for testing purpose only.

On user hitting the “/” we will send user a response object {“Response”: “simple FastAPI response”}, while hitting on url “/data/” we send user a response object {“name”: “GeeksForGeeks”,”url”: “https://practice.geeksforgeeks.org/”}

Python3




# Define a route to serve a user
@app.get("/")
def read_root():
    return {"Response": "simple FastAPI response"}
 
 
# another route to serve a user
@app.get("/data/")
def read_data():
    return {"name": "GeeksForGeeks",
            "url": "https://practice.geeksforgeeks.org/"}


Complete main.py File

Python3




# Import required Library
from fastapi import FastAPI
 
# creating fastAPI app
app = FastAPI()
 
 
# Define a route to serve a user
@app.get("/")
def read_root():
    return {"Response": "simple FastAPI response"}
 
 
# another route to serve a user
@app.get("/data/")
def read_data():
    return {"name": "GeeksForGeeks",
            "url": "https://practice.geeksforgeeks.org/"}


Step 5: Running and Deployment of the project

We can run the FastAPI app using a web server such as uvicorn:

uvicorn main:app --reload

Now, we can acess this API in browser by access ‘http://localhost:8000/’ or ‘http://127.0.0.1:8000/’ in your web browser or make a GET request to that URL using a tool like postman, the FastAPI app will return the a response object as we mentioned above in code.

Output

ezgifcom-resize

There is another method also to test our API by Postman tool by just sending GET request on “http://127.0.0.1:8000/” or “http://127.0.0.1:8000/data/”



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads