Run the fast-api server using Pycharm
Last Updated :
04 Nov, 2023
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

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/"
Similar Reads
Python | Build a REST API using Flask
Prerequisite: Introduction to Rest API REST stands for REpresentational State Transfer and is an architectural style used in modern web development. It defines a set or rules/constraints for a web application to send and receive data. In this article, we will build a REST API in Python using the Fla
3 min read
Microservice in Python using FastAPI
Microservices architecture is the approach to software development where the large application is composed of smaller, independent services that communicate over well-defined APIs. Each service can be focused on a specific business function and it can be developed, deployed, and scaled independently
5 min read
How to Fix "fast.ai Not Using the GPU"?
Fast.ai is one of the leading deep-learning frameworks built on top of PyTorch.e. The library provides high-level components that make it easy to build and train neural networks. This library simplifies the deep learning process making the process easy in high-level API building for building and tra
4 min read
Setting up a simple HTTP server using Python
In this article, we are going to learn how to set up a simple and local HTTP server using Python. An HTTP server can be very useful for testing Android, PC or Web apps locally during development. It can also be used to share files between two devices connected over the same LAN or WLAN network. Inst
2 min read
Python Falcon - API Testing
Python Falcon is a lightweight and fast web framework designed for building RESTful APIs. When it comes to API testing, Falcon provides a straightforward and efficient way to interact with your API endpoints. In this article, we'll explore three simple examples using Python Falcon: a basic API endpo
2 min read
How to Run a Python Script using Docker?
Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the a
8 min read
Create API Tester using Python Requests Module
Prerequisites: Python Requests module, API In this article, we will discuss the work process of the Python Requests module by creating an API tester. API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a syst
3 min read
How to Deploy a Flask Web Server in Docker Container using AWS?
In the current digitized era, web servers are essential as they stand for various online applications and sites. Web servers run stealthily the scenes to give the devices needed information and functional facilities irrespective of whether surfing on the internet, using a mobile app, or cloud facili
10 min read
Setup Pandas on PyCharm
If you're working with data analysis or data science in Python, you'll often find yourself using the Pandas library, which provides powerful data structures and data analysis tools. In this article, we'll walk you through the process of setting up Pandas on PyCharm so you can start working with data
1 min read
Build an AI Chatbot in Python using Cohere API
A chatbot is a technology that is made to mimic human-user communication. It makes use of machine learning, natural language processing (NLP), and artificial intelligence (AI) techniques to comprehend and react in a conversational way to user inquiries or cues. In this article, we will be developing
4 min read