Building a full-stack application with React and FastAPI can be a powerful combination, allowing you to create a dynamic front-end interface with React and a robust back-end server with FastAPI. In this tutorial, we will guide you through the process of building a React + FastAPI application from scratch.
Prerequisites
Before getting started, make sure you have the following tools and technologies installed on your system:
- Node.js and npm (Node Package Manager) for building the front-end with React.
- Python and pip for building the back-end with FastAPI.
- Visual Studio Code or any other code editor of your choice.
- Git for version control.
Step 1: Set Up the Back-End with FastAPI
-
Create a new directory for your project and navigate to it in the terminal.
-
Run the following command to create a new virtual environment for your FastAPI project:
python -m venv venv
-
Activate the virtual environment with the following command:
source venv/bin/activate
-
Install FastAPI and Uvicorn (ASGI server) using pip:
pip install fastapi uvicorn
- Create a new Python file for your FastAPI application, e.g.,
main.py
, and import FastAPI:from fastapi import FastAPI
app = FastAPI()
6. Define a sample endpoint for testing:
```python
@app.get("/")
def read_root():
return {"message": "Hello World"}
-
Run the FastAPI application using Uvicorn:
uvicorn main:app --reload
- Visit
http://localhost:8000
in your browser to see the "Hello World" message.
Step 2: Set Up the Front-End with React
-
Create a new directory for your React project within the project directory.
-
Initialize a new React app using
create-react-app
:npx create-react-app client
-
Navigate to the
client
directory and start the React development server:cd client npm start
- Your React app should now be running at
http://localhost:3000
.
Step 3: Connect the Front-End to the Back-End
- Create a proxy configuration for the React app to communicate with the FastAPI server. Create a
setupProxy.js
file in theclient/src
directory and add the following code:const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function (app) {
app.use(‘/api’, createProxyMiddleware({ target: ‘http://localhost:8000‘, changeOrigin: true }));
};
2. Modify the `package.json` file in the `client` directory to add a `proxy` configuration:
```json
"proxy": "http://localhost:8000"
-
Use the
fetch
API or any HTTP library of your choice in the React components to make requests to the FastAPI back-end. For example:fetch('/api/') .then(response => response.json()) .then(data => console.log(data));
- Build and deploy your React app by running:
npm run build
Conclusion
In this tutorial, we have walked you through the process of building a full-stack application with React and FastAPI. You have learned how to set up the back-end with FastAPI, set up the front-end with React, and connect the two parts to communicate with each other. Feel free to extend this project with additional features or integrate databases, authentication, or other functionalities to create a more robust full-stack application. Happy coding!
Thanks, Eric!
Great tutorial, thank you for all your efforts!
Hey, nice tutorial, can we get a tutorial to put this in a production environment ? I never saw this on youtube
You are not really explaining anything at all. You are just copying from somewhere.
10:15
😀
What theme are you using here?
Thanks for this to the point and precise tutorial!
in background where the data store is it in sqlite or mongodb or mysql ?????
Great video
Is there any plans for react +faspi course +
Here is how you deploy FastAPI + React + PostgreSQL on AWS: https://www.udemy.com/course/deploy-fastapi-fullstack-amazon-cloud-aws/?referralCode=706861EF28A616B36FEC
Thanks a lot. The React part is totally new for me and didn't understand much, but it works haha
hello am doing thisone for my school project …….and i want to deploy it how do i do so please?
hello am doing this as my school project how do i deploy it? please
Thank you. Super helpful. Can you do NextJS/Fastapi next time? and/or Nextjs/fastapi using vercel usechat SDK?
Hi Eric, great tutorial. thank you. I am trying to build some prototype application using React+fastapi+celery+redis+mongodb, but it is going slow. by any chance, do you have some patreon channel that we can subscribe for paid membership where we can ask some technical questions? thank you. and have a nice day.
Just found this video and I haven't watched it yet. My focus is React and I tried implementing the backend with Django but it is so complicated with Django. Some tutorials which show how to create a React app with Django tends to be over 3hrs plus. I saw a couple of videos with fastAPI and I see the videos tend to be smaller in length for the tutorials. Is it easier to create a backend restful api with FastAPI? I am now starting the tutorial. How good is FastAPI for creating api endpoints? Thanks and I guess I will find out.
wait cors is included already with fastAPi.
This was a great Video! thanks!! by any change do you have a upcoming video with React + fast API with Jinja templating?
Thanks a lot Eric for this great course, excellent indeed, it's content, your broad knowledge and delivery really engage the audience, very clear and precise explanations, I highly recommend the instructor and the course, best wishes and God bless you.
Thank you for making such a great video! It was very helpful, the example was relevant and it explained everything you need. Great job!